文章目录
1.算法程序2.文本转执行3.作者答疑1.算法程序
illustrator是矢量编辑软件,画板是绘制处理的重要容器,开发一个插入页码功能,源代码如下所示:
if (app.documents.length > 0) {var win = new Window("dialog", "插入日期文件名变量页码");var panelMargins = win.add("panel", undefined, "边缘");var lblMargins = panelMargins.add("statictext", undefined, "距离边缘:");var txtMargins = panelMargins.add("edittext", undefined, 0.25);var lblUnits = panelMargins.add("statictext", undefined, "inches");var panelLocation = win.add("panel", undefined, "位置");var radTop = panelLocation.add("radiobutton", undefined, "顶");var radBottom = panelLocation.add("radiobutton", undefined, "底");var panelAlignment = win.add("panel", undefined, "对齐");var radLeft = panelAlignment.add("radiobutton", undefined, "左");var radCenter = panelAlignment.add("radiobutton", undefined, "中");var radRight = panelAlignment.add("radiobutton", undefined, "右");var panelFooter = win.add("panel", undefined, "插入的类型");var grpPages = panelFooter.add("group");var btnPage = grpPages.add("button", undefined, "页码");var btnPages = grpPages.add("button", undefined, "总页");var btnDate = grpPages.add("button", undefined, "日期");var btnTime = grpPages.add("button", undefined, "时间");var btnFullName = grpPages.add("button", undefined, "地址");var btnFile = grpPages.add("button", undefined, "文件名");var txtFooter = panelFooter.add("edittext");var btnClear = panelFooter.add("button", undefined, "取消");btnPage.size = btnPages.size = btnDate.size = btnTime.size = btnFullName.size = btnFile.size = btnClear.size = [31, 24];var btnOk = win.add("button", undefined, "Ok");radRight.value = radBottom.value = true;win.alignChildren = panelFooter.alignChildren = "fill";btnClear.alignment = "left";panelMargins.spacing = 3;panelMargins.orientation = panelLocation.orientation = panelAlignment.orientation = "row";win.helpTip = "© Carlos Canto";btnOk.helpTip = "Press Esc to Close";btnPage.helpTip = "Adds *page* keyword, it represents a single page";btnPages.helpTip = "Adds *pages* keyword, it represents total number of pages";btnDate.helpTip = "Adds *date* keyword, it represents today's date";btnTime.helpTip = "Adds *time* keyword, it represents current time";btnFullName.helpTip = "Adds *fname* keyword, it represents Full File Name (including path)";btnFile.helpTip = "Adds *file* keyword, it represents File Name";btnClear.helpTip = "Clears input text area";txtFooter.helpTip = "Type \r\t'Page *page* of *pages*' \rto get \r\t'Page 1 of 3' \rfor example";var wgx = win.graphics;var grayPen = wgx.newPen(wgx.PenType.SOLID_COLOR, [0.67, 0.67, 0.67], 1);txtFooter.onDraw = function() {var gx = this.graphics;gx.drawOSControl();this.text || this.active || gx.drawString("[Type text to insert here] Press Esc to close", grayPen, 0, 0);};btnOk.onClick = function() {if (txtFooter.text != "") {doSomething()}};btnClear.onClick = function() {txtFooter.text = "";};btnPage.onClick = function() {footer("*page*");};btnPages.onClick = function() {footer("*pages*");};btnDate.onClick = function() {footer("*date*");};btnTime.onClick = function() {footer("*time*");};btnFullName.onClick = function() {footer("*fname*");};btnFile.onClick = function() {footer("*file*");};win.center();win.show();function footer(page) {txtFooter.text = txtFooter.text + page;}function doSomething() {var idoc = app.activeDocument;try {var ilayer = idoc.layers["Page Numbers"];} catch (e) {var ilayer = idoc.layers.add();ilayer.name = "Page Numbers";}var pages = idoc.artboards.length;var datee = getdate();var timee = gettime();var fname = idoc.path == "" ? "Full Name: <unsaved document>" : idoc.fullName;var file = idoc.name;var footerPages = txtFooter.text.replace("*pages*", pages);footerPages = footerPages.replace("*pages*", pages);footerPages = footerPages.replace("*date*", datee);footerPages = footerPages.replace("*time*", timee);footerPages = footerPages.replace("*fname*", fname);footerPages = footerPages.replace("*file*", file);var margins = Number(txtMargins.text) * 72;for (var i = 0; i < idoc.artboards.length; i += 1) {footerPage = footerPages.replace("*page*", i + 1);var itext = ilayer.textFrames.add();itext.contents = footerPage;var fontSize = itext.textRange.characterAttributes.size;var activeAB = idoc.artboards[i];var iartBounds = activeAB.artboardRect;var ableft = iartBounds[0] + margins;var abtop = iartBounds[1] - margins;var abright = iartBounds[2] - margins;var abbottom = iartBounds[3] + margins + fontSize;var abcenter = ableft + ((abright - ableft) / 2);if (radRight.value == true) {itext.left = abright;itext.textRange.paragraphAttributes.justification = Justification.RIGHT;} else if (radCenter.value == true) {itext.left = abcenter;itext.textRange.paragraphAttributes.justification = Justification.CENTER;} else {itext.left = ableft;itext.textRange.paragraphAttributes.justification = Justification.LEFT;}if (radTop.value == true) {itext.top = abtop;} else {itext.top = abbottom;}}app.redraw();}} else {alert("请打开一个文档");}function getdate() {var date = new Date();var m = date.getMonth() + 1;var d = date.getDate();var y = date.getFullYear();var datemdy = m + "/" + d + "/" + y;return datemdy;}function gettime() {var time = new Date();var hours = time.getHours();var minutes = time.getMinutes();if (minutes < 10) {minutes = "0" + minutes;}if (hours > 11) {ampm = "PM";} else {ampm = "AM";}var curtime = hours + ":" + minutes + " " + ampm;return curtime;}
2.文本转执行
使用记事本或者notepad++等文本编辑器,将代码拷贝至文本文件,然后修改后缀名为js或者jsx,最后再illustrator软件菜单文件》脚本》其它脚本,选择脚本文件,点执行即可,也可以将脚本放在预设文件夹内,会显示再脚本下的二级子菜单。如下图所示:
3.作者答疑
如有疑问,请留言。
如果觉得《illustrator插件-画板功能开发-插入页码-js脚本开发-ai插件》对你有帮助,请点赞、收藏,并留下你的观点哦!