LanyaAITools.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #target illustrator
  2. #targetengine main
  3. var vs = "illustrator-" + app.version.substr(0, 2);
  4. // 实际代码建立 buildMsg(code) 函数传送代码
  5. function buildMsg(code) {
  6. try {
  7. var bt = new BridgeTalk;
  8. bt.target = vs;
  9. var msg = code;
  10. bt.body = msg;
  11. bt.send();
  12. } catch (e) { }
  13. }
  14. // 创建面板 使用 new Window("palette") ,需要 BridgeTalk, 它是 Adobe 应用程序之间进行通信的一种机制。
  15. // 它允许不同的 Adobe 应用程序在同一台计算机上进行交互和数据共享。
  16. // var bt = new BridgeTalk();
  17. // bt.target = "photoshop"; // 目标应用程序名称
  18. // bt.body = "alert('Hello from Illustrator!')"; // 要发送的消息或脚本
  19. // bt.send();
  20. main_panel();
  21. function main_panel() {
  22. var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.11.11");
  23. panel.alignChildren = ["left", "top"];
  24. panel.spacing = 2;
  25. panel.margins = 3;
  26. // 创建按钮组
  27. var BtGroup1 = panel.add("group");
  28. var BtGroup2 = panel.add("group");
  29. // 设置按钮组为水平布局
  30. BtGroup1.orientation = "row";
  31. BtGroup2.orientation = "row";
  32. // 添加按钮
  33. var button1 = BtGroup1.add("button", undefined, "标注尺寸");
  34. var button2 = BtGroup1.add("button", undefined, "文件名日期");
  35. var button3 = BtGroup1.add("button", undefined, "借咬口5mm");
  36. var button4 = BtGroup1.add("button", undefined, "尺寸取整");
  37. var button5 = BtGroup2.add("button", undefined, "拼版左上对齐");
  38. var button6 = BtGroup2.add("button", undefined, "物件尺寸大小");
  39. var button7 = BtGroup2.add("button", undefined, "物件轮廓边界");
  40. var button8 = BtGroup2.add("button", undefined, "▲");
  41. button8.preferredSize = [26, 26];
  42. // 设置按钮组的边缘间距 设置按钮间距
  43. BtGroup1.spacing = 2; // 调整按钮之间的间距
  44. BtGroup2.spacing = 2;
  45. // 按钮点击事件处理程序
  46. button1.onClick = function () {
  47. buildMsg("make_size();");
  48. };
  49. button2.onClick = function () {
  50. buildMsg("filename_date();");
  51. };
  52. button3.onClick = function () {
  53. buildMsg("mark_5mm();");
  54. };
  55. button4.onClick = function () {
  56. buildMsg("size_to_integer();");
  57. };
  58. button5.onClick = function () {
  59. buildMsg("replace_align_position();");
  60. };
  61. button6.onClick = function () {
  62. buildMsg("size_by_width_height();");
  63. };
  64. button7.onClick = function () {
  65. buildMsg("size_by_controlBounds();");
  66. };
  67. button8.onClick = function () {
  68. icon_panel();
  69. panel.close();
  70. };
  71. // 显示面板
  72. panel.show();
  73. }
  74. function icon_panel() {
  75. var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.11.11");
  76. panel.alignChildren = ["left", "top"];
  77. panel.spacing = 2;
  78. panel.margins = 3;
  79. // 创建按钮组
  80. var BtGroup1 = panel.add("group");
  81. var BtGroup2 = panel.add("group");
  82. // 设置按钮组为水平布局
  83. BtGroup1.orientation = "row";
  84. BtGroup2.orientation = "row";
  85. // 设置按钮组的边缘间距
  86. BtGroup1.spacing = 2; // 调整按钮之间的间距
  87. BtGroup2.spacing = 2;
  88. scriptFile = new File($.fileName); // 获取当前脚本文件的路径
  89. var iconFile = new File(scriptFile.path + "/icon/icon.png"); // 拼接图标文件的完整路径
  90. // 添加图标按钮
  91. var button1 = BtGroup1.add("iconbutton", undefined, iconFile);
  92. var button2 = BtGroup1.add("iconbutton", undefined, iconFile);
  93. var button3 = BtGroup1.add("iconbutton", undefined, iconFile);
  94. var button4 = BtGroup1.add("iconbutton", undefined, iconFile);
  95. var button5 = BtGroup2.add("iconbutton", undefined, iconFile);
  96. var button6 = BtGroup2.add("iconbutton", undefined, iconFile);
  97. var button7 = BtGroup2.add("iconbutton", undefined, iconFile);
  98. var button8 = BtGroup2.add("iconbutton", undefined, iconFile);
  99. // 设置按钮大小与图片大小相同
  100. button1.preferredSize = [48, 48];
  101. button2.preferredSize = [48, 48];
  102. button3.preferredSize = [48, 48];
  103. button4.preferredSize = [48, 48];
  104. button5.preferredSize = [48, 48];
  105. button6.preferredSize = [48, 48];
  106. button7.preferredSize = [48, 48];
  107. button8.preferredSize = [48, 48];
  108. // 按钮点击事件处理程序
  109. button1.onClick = function () {
  110. buildMsg("make_size();");
  111. };
  112. button2.onClick = function () {
  113. buildMsg("filename_date();");
  114. };
  115. button3.onClick = function () {
  116. buildMsg("mark_5mm();");
  117. };
  118. button4.onClick = function () {
  119. buildMsg("size_to_integer();");
  120. };
  121. button5.onClick = function () {
  122. buildMsg("replace_align_position();");
  123. };
  124. button6.onClick = function () {
  125. buildMsg("size_by_width_height();");
  126. };
  127. button7.onClick = function () {
  128. buildMsg("size_by_controlBounds();");
  129. };
  130. button8.onClick = function () {
  131. main_panel();
  132. panel.close();
  133. };
  134. // 显示面板
  135. panel.show();
  136. }
  137. //==================================================================================//
  138. // 蘭雅 Adobe Illustrator 工具箱© 2023.11.11 各个按钮功能模块
  139. //==================================================================================//
  140. // 标注尺寸
  141. function make_size() {
  142. // 定义当前激活文档
  143. var docRef = activeDocument;
  144. var mm = 25.4 / 72; // pt 和 mm 转换系数
  145. var myFont = textFonts.getByName("MicrosoftYaHei");
  146. var myFontSize = 24;
  147. var x, y;
  148. // 格式化尺寸为 mm 取整数
  149. function formatSize(size) {
  150. return Math.round(size * mm).toFixed(0);
  151. }
  152. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  153. var cmykRed = new CMYKColor();
  154. cmykRed.cyan = 0;
  155. cmykRed.magenta = 100;
  156. cmykRed.yellow = 100;
  157. cmykRed.black = 0;
  158. function writeText(text) {
  159. var textRef = docRef.textFrames.add(); // 建立文本
  160. textRef.contents = text;
  161. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  162. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  163. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  164. textRef.top = y + 15 / mm;
  165. textRef.left = x + 10 / mm;
  166. }
  167. // 遍历选择的物件标注尺寸
  168. if (docRef.selection.length > 0) {
  169. var mySelection = docRef.selection;
  170. for (var i = 0; i < mySelection.length; i++) {
  171. var s = mySelection[i]
  172. x = s.left; y = s.top
  173. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  174. writeText(str)
  175. }
  176. }
  177. }
  178. // 文件名日期
  179. function filename_date() {
  180. // 获取当前时间
  181. function getdate() {
  182. var d = new Date(), month = '' + (d.getMonth() + 1),
  183. day = '' + d.getDate(), year = d.getFullYear();
  184. if (month.length < 2) month = '0' + month;
  185. if (day.length < 2) day = '0' + day;
  186. return [year, month, day].join('-');
  187. }
  188. // 获取 AI文档名称
  189. var docRef = activeDocument;
  190. var str = docRef.name;
  191. str = str + " " + getdate();
  192. // alert("本脚本建立一个文本:\n" + str);
  193. // 文档中建立一个新文本
  194. // var textRef = docRef.textFrames.add();
  195. // textRef.top = 100;
  196. // textRef.left = 200;
  197. // textRef.contents = str;
  198. var mm = 25.4 / 72; // pt 和 mm 转换系数
  199. var base = new Array();
  200. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  201. // alert("画板标尺原点mm x:" + base[0] * mm +" y:" + base[1] * mm + "\n画板大小mm 宽:" + docRef.width * mm +" 高:" + docRef.height * mm);
  202. var pw = 0;
  203. var ph = 0;
  204. var x = base[0]; // 画板左下角 x 坐标
  205. var y = - base[1]; // 画板左下角 y 坐标
  206. var myFont = textFonts.getByName("MicrosoftYaHei");
  207. var myFontSize = 8;
  208. pw = docRef.width; // 文档宽
  209. ph = docRef.height; // 文档高
  210. x = pw / 2 - x; // 转换x坐标: 画板中下x
  211. function filenameDate() {
  212. var textRef = docRef.textFrames.add(); // 建立文本
  213. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  214. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  215. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  216. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  217. textRef.top = y + 7.4; // 画板底向上偏移
  218. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  219. }
  220. filenameDate();
  221. }
  222. // 借咬口5mm
  223. function mark_5mm() {
  224. // 获取 AI文档名称
  225. var docRef = activeDocument;
  226. var str = docRef.name;
  227. str = "借咬口5mm"
  228. var mm = 25.4 / 72; // pt 和 mm 转换系数
  229. var base = new Array();
  230. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  231. var pw = 0;
  232. var ph = 0;
  233. var x = base[0]; // 画板左下角 x 坐标
  234. var y = - base[1]; // 画板左下角 y 坐标
  235. var myFont = textFonts.getByName("MicrosoftYaHei");
  236. var myFontSize = 64;
  237. pw = docRef.width; // 文档宽
  238. ph = docRef.height; // 文档高
  239. x = pw / 2 - x; // 转换x坐标: 画板中下x
  240. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  241. var cmykRed = new CMYKColor();
  242. cmykRed.cyan = 0;
  243. cmykRed.magenta = 100;
  244. cmykRed.yellow = 100;
  245. cmykRed.black = 0;
  246. function writeText() {
  247. var textRef = docRef.textFrames.add(); // 建立文本
  248. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  249. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  250. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  251. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  252. textRef.top = y - 15; // 画板底向上偏移
  253. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  254. }
  255. writeText();
  256. }
  257. // 尺寸取整
  258. function size_to_integer() {
  259. // 定义当前激活文档
  260. var doc = activeDocument;
  261. var mm = 25.4 / 72; // pt 和 mm 转换系数
  262. // 格式化尺寸为 mm 取整数
  263. function formatSize(size) {
  264. return Math.round(size * mm).toFixed(0);
  265. }
  266. // 遍历选择的物件尺寸取整
  267. if (doc.selection.length > 0) {
  268. var src = doc.selection;
  269. for (var i = 0; i < src.length; i++) {
  270. var s = src[i]
  271. s.width = formatSize(s.width) / mm
  272. s.height = formatSize(s.height) / mm
  273. }
  274. }
  275. }
  276. // 物件轮廓边界
  277. function size_by_controlBounds() {
  278. var docRef = activeDocument;
  279. // 判断选择物件2个以上
  280. if (docRef.selection.length > 1) {
  281. // 定义选择物件
  282. mySelection = docRef.selection;
  283. // 最上层物件为替换源
  284. var sourceObj = docRef.selection[0];
  285. // 定义数组保存选择物件controlBounds
  286. var BoundsArray = new Array();
  287. for (var i = 0; i < mySelection.length; i++) {
  288. // PageItem.position 获得物件群组左上角坐标
  289. var sel_Bounds = mySelection[i].controlBounds;
  290. BoundsArray.push(sel_Bounds);
  291. }
  292. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  293. var newGroup = sourceObj.parent.groupItems.add();
  294. for (var i = 1; i < BoundsArray.length; i++) {
  295. var width = BoundsArray[i][2] - BoundsArray[i][0];
  296. var height = BoundsArray[i][1] - BoundsArray[i][3];
  297. sourceObj.width = width;
  298. sourceObj.height = height;
  299. // 移动源文件到目的物件左上角对齐
  300. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  301. sourceObj.position = sel_xy;
  302. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  303. }
  304. sourceObj.remove();
  305. }
  306. }
  307. // 物件尺寸大小
  308. function size_by_width_height() {
  309. var doc = activeDocument;
  310. // 判断选择物件2个以上
  311. if (doc.selection.length > 1) {
  312. // 定义选择物件
  313. src = doc.selection;
  314. var taget = doc.selection[0];
  315. // PageItem.position 获得物件群组左上角坐标
  316. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  317. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  318. var newGroup = taget.parent.groupItems.add();
  319. for (var i = 1; i < src.length; i++) {
  320. var sel_xy = src[i].position;
  321. taget.width = src[i].width;
  322. taget.height = src[i].height;
  323. taget.position = sel_xy;
  324. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  325. }
  326. taget.remove();
  327. }
  328. }
  329. // 拼版左上对齐
  330. function replace_align_position() {
  331. var docRef = activeDocument;
  332. // 判断选择物件2个以上
  333. if (docRef.selection.length > 1) {
  334. // 定义选择物件
  335. mySelection = docRef.selection;
  336. // 最上层物件为替换源
  337. var sourceObj = docRef.selection[0];
  338. // 定义数组用来保存选择物件的左上角坐标
  339. var alterObjectArray = new Array();
  340. for (var i = 0; i < mySelection.length; i++) {
  341. // PageItem.position 获得物件群组左上角坐标
  342. var sel_xy = mySelection[i].position
  343. alterObjectArray.push(sel_xy);
  344. }
  345. // 删除用来定位的下层物件
  346. for (var i = 1; i < mySelection.length; i++) {
  347. mySelection[i].remove();
  348. }
  349. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  350. var newGroup = sourceObj.parent.groupItems.add();
  351. for (var i = 1; i < alterObjectArray.length; i++) {
  352. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  353. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  354. }
  355. sourceObj.remove();
  356. }
  357. }