LanyaAITools.jsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #target illustrator
  2. #targetengine main
  3. var vs = "illustrator-" + app.version.substr(0, 2);
  4. var IconsFolder = "C:/TSP/icon";
  5. var micro_distance = "1";
  6. // 实际代码建立 buildMsg(code) 函数传送代码
  7. function buildMsg(code) {
  8. try {
  9. var bt = new BridgeTalk;
  10. bt.target = vs;
  11. var msg = code;
  12. bt.body = msg;
  13. bt.send();
  14. } catch (e) { }
  15. }
  16. // 创建面板 使用 new Window("palette") ,需要 BridgeTalk, 它是 Adobe 应用程序之间进行通信的一种机制。
  17. // 它允许不同的 Adobe 应用程序在同一台计算机上进行交互和数据共享。
  18. // var bt = new BridgeTalk();
  19. // bt.target = "photoshop"; // 目标应用程序名称
  20. // bt.body = "alert('Hello from Illustrator!')"; // 要发送的消息或脚本
  21. // bt.send();
  22. icon_panel(); // main_panel();
  23. function main_panel() {
  24. var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.12.12");
  25. panel.alignChildren = ["left", "top"];
  26. panel.spacing = 2;
  27. panel.margins = 3;
  28. // 创建按钮组
  29. var BtGroup1 = panel.add("group");
  30. var BtGroup2 = panel.add("group");
  31. // 设置按钮组为水平布局
  32. BtGroup1.orientation = "row";
  33. BtGroup2.orientation = "row";
  34. // 设置按钮组的边缘间距
  35. BtGroup1.spacing = 2; // 调整按钮之间的间距
  36. BtGroup2.spacing = 2;
  37. // 添加按钮
  38. var button1 = BtGroup1.add("button", undefined, "标注尺寸");
  39. var button2 = BtGroup1.add("button", undefined, "批量旋转");
  40. var button3 = BtGroup1.add("button", undefined, "文件日期");
  41. var button4 = BtGroup1.add("button", undefined, "尺寸取整-微调-统一");
  42. var button5 = BtGroup2.add("button", undefined, "替换对齐-打包图片");
  43. var button6 = BtGroup2.add("button", undefined, "自动群组-调整尺寸");
  44. var button7 = BtGroup2.add("button", undefined, "尺寸复制");
  45. var button8 = BtGroup2.add("button", undefined, "▲");
  46. button8.preferredSize = [26, 26];
  47. button1.helpTip = "标注尺寸, <Alt>增强标注";
  48. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  49. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  50. button4.helpTip = "尺寸取整, <Alt-Ctrl-Shift>微调统一尺寸";
  51. button5.helpTip = "左上对齐快速替换, <Alt>打包连接图";
  52. button6.helpTip = "自动群组, <Alt>调整尺寸";
  53. button7.helpTip = "尺寸复制, <Alt>包括轮廓";
  54. button8.helpTip = "切换图标界面窗口";
  55. // 设置按钮大小与图片大小相同
  56. button8.preferredSize = [26, 26];
  57. button1.onClick = function () {
  58. if (ScriptUI.environment.keyboardState.ctrlKey) {
  59. buildMsg("shapes_info();");
  60. } else if (ScriptUI.environment.keyboardState.altKey) {
  61. make_size_plus();
  62. } else {
  63. buildMsg("make_size();");
  64. }
  65. };
  66. button2.onClick = function () {
  67. if (ScriptUI.environment.keyboardState.ctrlKey) {
  68. // Ctrl 加鼠标左键,自定义数字
  69. var input = prompt("请输入角度数字:", "45");
  70. if (!isNaN(parseFloat(input)))
  71. buildMsg("shapes_rotate(" + input + ");");
  72. } else if (ScriptUI.environment.keyboardState.altKey) {
  73. buildMsg("shapes_rotate(180);");
  74. } else {
  75. buildMsg("shapes_rotate(90);");
  76. }
  77. };
  78. button3.onClick = function () {
  79. if (ScriptUI.environment.keyboardState.altKey) {
  80. buildMsg("mark_5mm();");
  81. } else {
  82. buildMsg("filename_date();");
  83. }
  84. };
  85. button4.onClick = function () {
  86. if (ScriptUI.environment.keyboardState.ctrlKey) {
  87. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  88. } else if (ScriptUI.environment.keyboardState.altKey) {
  89. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  90. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  91. // alert("ScriptUI.environment.keyboardState.shiftKey");
  92. var input = prompt("请输如宽和高两个数字(例如: 100 80): (或者:99.5 55.5)", "100 80");
  93. // 使用正则表达式匹配数字
  94. var regex = /(\d+(?:\.\d+)?)\s*(\d+(?:\.\d+)?)/;
  95. var match = input.match(regex);
  96. if (match) {
  97. var number1 = Number(match[1]);
  98. var number2 = Number(match[2]);
  99. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  100. } else {
  101. alert("输入格式不正确!");
  102. }
  103. } else {
  104. buildMsg("size_to_integer();");
  105. }
  106. };
  107. button5.onClick = function () {
  108. if (ScriptUI.environment.keyboardState.altKey) {
  109. buildMsg("img_pack_links();");
  110. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  111. // Shift + 鼠标左键 中心对齐替换
  112. buildMsg("replace_align_center(true);");
  113. } else {
  114. buildMsg("replace_align_position();");
  115. }
  116. };
  117. button6.onClick = function () {
  118. if (ScriptUI.environment.keyboardState.ctrlKey) {
  119. alert("Ctrl信息; Alt 调整尺寸; Shift重新加载脚本; 默认自动群组");
  120. } else if (ScriptUI.environment.keyboardState.altKey) {
  121. ResizeToSize();
  122. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  123. reload_aia();
  124. } else {
  125. auto_group();
  126. }
  127. };
  128. button7.onClick = function () {
  129. if (ScriptUI.environment.keyboardState.altKey) {
  130. buildMsg("size_by_controlBounds();");
  131. }else if (ScriptUI.environment.keyboardState.shiftKey) {
  132. // Shift + 鼠标左键 中心对齐
  133. buildMsg("replace_align_center(false);");
  134. } else {
  135. buildMsg("size_by_width_height();");
  136. }
  137. };
  138. button8.onClick = function () {
  139. icon_panel();
  140. panel.close();
  141. };
  142. // 显示面板
  143. panel.show();
  144. }
  145. function icon_panel() {
  146. var panel = new Window("palette", "©蘭雅 Adobe Illustrator 工具箱");
  147. panel.onClose = function () {
  148. saveWindowPosition(panel);
  149. };
  150. panel.alignChildren = ["left", "top"];
  151. panel.spacing = 2;
  152. panel.margins = 3;
  153. // 创建按钮组
  154. var BtGroup1 = panel.add("group");
  155. var BtGroup2 = panel.add("group");
  156. // 设置按钮组为水平布局
  157. BtGroup1.orientation = "row";
  158. BtGroup2.orientation = "row";
  159. // 设置按钮组的边缘间距
  160. BtGroup1.spacing = 2; // 调整按钮之间的间距
  161. BtGroup2.spacing = 2;
  162. // scriptFile = new File($.fileName); // 获取当前脚本文件的路径
  163. // var iconFile = new File(scriptFile.path + "/icon/icon.png"); // 拼接图标文件的完整路径
  164. var iconF1 = IconsFolder + "/size.png";
  165. var iconF2 = IconsFolder + "/icon.png";
  166. var iconF3 = IconsFolder + "/mark.png";
  167. var iconF4 = IconsFolder + "/debug.png";
  168. var iconF5 = IconsFolder + "/replace.png";
  169. var iconF6 = IconsFolder + "/gpucard.png";
  170. var iconF7 = IconsFolder + "/byBounds.png";
  171. var iconF8 = IconsFolder + "/repeat.png";
  172. // 添加图标按钮
  173. var button1 = BtGroup1.add("iconbutton", undefined, iconF1);
  174. var button2 = BtGroup1.add("iconbutton", undefined, iconF2);
  175. var button3 = BtGroup1.add("iconbutton", undefined, iconF3);
  176. var button4 = BtGroup1.add("iconbutton", undefined, iconF4);
  177. var button5 = BtGroup2.add("iconbutton", undefined, iconF5);
  178. var button6 = BtGroup2.add("iconbutton", undefined, iconF6);
  179. var button7 = BtGroup2.add("iconbutton", undefined, iconF7);
  180. var button8 = BtGroup2.add("iconbutton", undefined, iconF8);
  181. button1.helpTip = "标注尺寸, <Alt>增强标注";
  182. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  183. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  184. button4.helpTip = "尺寸取整, <Alt-Ctrl-Shift>微调统一尺寸";
  185. button5.helpTip = "左上对齐快速替换, <Alt>打包连接图";
  186. button6.helpTip = "自动群组, <Alt>调整尺寸";
  187. button7.helpTip = "尺寸复制, <Alt>包括轮廓";
  188. button8.helpTip = "<Ctrl>微调距离, 最小化窗口";
  189. // 设置按钮大小与图片大小相同
  190. button1.preferredSize = [48, 48];
  191. button2.preferredSize = [48, 48];
  192. button3.preferredSize = [48, 48];
  193. button4.preferredSize = [48, 48];
  194. button5.preferredSize = [48, 48];
  195. button6.preferredSize = [48, 48];
  196. button7.preferredSize = [48, 48];
  197. button8.preferredSize = [48, 48];
  198. button1.onClick = function () {
  199. if (ScriptUI.environment.keyboardState.ctrlKey) {
  200. buildMsg("shapes_info();");
  201. } else if (ScriptUI.environment.keyboardState.altKey) {
  202. make_size_plus();
  203. } else {
  204. buildMsg("make_size();");
  205. }
  206. };
  207. button2.onClick = function () {
  208. if (ScriptUI.environment.keyboardState.ctrlKey) {
  209. // Ctrl 加鼠标左键,自定义数字
  210. var input = prompt("请输入角度数字:", "45");
  211. if (!isNaN(parseFloat(input)))
  212. buildMsg("shapes_rotate(" + input + ");");
  213. } else if (ScriptUI.environment.keyboardState.altKey) {
  214. buildMsg("shapes_rotate(180);");
  215. } else {
  216. buildMsg("shapes_rotate(90);");
  217. }
  218. };
  219. button3.onClick = function () {
  220. if (ScriptUI.environment.keyboardState.altKey) {
  221. buildMsg("mark_5mm();");
  222. } else {
  223. buildMsg("filename_date();");
  224. }
  225. };
  226. button4.onClick = function () {
  227. if (ScriptUI.environment.keyboardState.ctrlKey) {
  228. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  229. } else if (ScriptUI.environment.keyboardState.altKey) {
  230. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  231. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  232. // alert("ScriptUI.environment.keyboardState.shiftKey");
  233. var input = prompt("请输如宽和高两个数字(例如: 100 80): (或者:99.5 55.5)", "100 80");
  234. // 使用正则表达式匹配数字
  235. var regex = /(\d+(?:\.\d+)?)\s*(\d+(?:\.\d+)?)/;
  236. var match = input.match(regex);
  237. if (match) {
  238. var number1 = Number(match[1]);
  239. var number2 = Number(match[2]);
  240. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  241. } else {
  242. alert("输入格式不正确!");
  243. }
  244. } else {
  245. buildMsg("size_to_integer();");
  246. }
  247. };
  248. button5.onClick = function () {
  249. if (ScriptUI.environment.keyboardState.altKey) {
  250. buildMsg("img_pack_links();");
  251. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  252. // Shift + 鼠标左键 中心对齐替换
  253. buildMsg("replace_align_center(true);");
  254. } else {
  255. buildMsg("replace_align_position();");
  256. }
  257. };
  258. button6.onClick = function () {
  259. if (ScriptUI.environment.keyboardState.ctrlKey) {
  260. alert("Ctrl信息; Alt 调整尺寸; Shift重新加载脚本; 默认自动群组");
  261. } else if (ScriptUI.environment.keyboardState.altKey) {
  262. ResizeToSize();
  263. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  264. reload_aia();
  265. } else {
  266. auto_group();
  267. }
  268. };
  269. button7.onClick = function () {
  270. if (ScriptUI.environment.keyboardState.altKey) {
  271. buildMsg("size_by_controlBounds();");
  272. }else if (ScriptUI.environment.keyboardState.shiftKey) {
  273. // Shift + 鼠标左键 中心对齐
  274. buildMsg("replace_align_center(false);");
  275. } else {
  276. buildMsg("size_by_width_height();");
  277. }
  278. };
  279. button8.onClick = function () {
  280. if (ScriptUI.environment.keyboardState.ctrlKey) {
  281. micro_distance = prompt("设置微调距离(mm): ", micro_distance);
  282. } else if (ScriptUI.environment.keyboardState.altKey) {
  283. main_panel();
  284. panel.close();
  285. } else {
  286. mini_panel();
  287. panel.close();
  288. }
  289. };
  290. // 显示面板
  291. panel.show();
  292. // 恢复窗口位置
  293. restoreWindowPosition(panel);
  294. }
  295. function mini_panel() {
  296. var panel = new Window("palette", "");
  297. panel.spacing = 0;
  298. panel.margins = [0, 0, 0, 0];;
  299. var icon = IconsFolder + "/repeat.png";
  300. var button_mini = panel.add("iconbutton", undefined, icon);
  301. button_mini.preferredSize = [40, 40];
  302. button_mini.spacing = 0;
  303. button_mini.onClick = function () {
  304. icon_panel();
  305. panel.close();
  306. };
  307. panel.show();
  308. restoreWindowPosition(panel);
  309. panel.bounds.height = 42;
  310. panel.bounds.width = 50;
  311. }
  312. // 保存窗口位置
  313. function saveWindowPosition(window) {
  314. var position = window.bounds;
  315. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  316. settingsFile.open("w");
  317. settingsFile.write(position.left + "," + position.top + "," + position.right + "," + position.bottom);
  318. settingsFile.close();
  319. }
  320. // 恢复窗口位置
  321. function restoreWindowPosition(window) {
  322. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  323. if (settingsFile.exists) {
  324. settingsFile.open("r");
  325. var position = settingsFile.read().split(",");
  326. settingsFile.close();
  327. window.bounds.left = parseInt(position[0]);
  328. window.bounds.top = parseInt(position[1]);
  329. window.bounds.right = parseInt(position[2]);
  330. window.bounds.bottom = parseInt(position[3]);
  331. }
  332. }
  333. //==================================================================================//
  334. // 蘭雅 Adobe Illustrator 工具箱© 2023.12.12 各个按钮功能模块
  335. //==================================================================================//
  336. var mm = 25.4 / 72; // pt 和 mm 转换系数
  337. // 格式化尺寸为 mm 取整数
  338. function formatSize(size) {
  339. return Math.round(size * mm).toFixed(0);
  340. }
  341. // 获得选择对象的边界框
  342. function get_Sel_Bounds() {
  343. var totalBounds = null;
  344. var sr = app.activeDocument.selection;
  345. for (var i = 0; i < sr.length; i++) {
  346. var item = sr[i];
  347. // 获取对象的边界框
  348. var bounds = item.geometricBounds;
  349. // 更新总范围
  350. if (totalBounds === null) {
  351. totalBounds = bounds.slice(); // 创建边界框的副本
  352. } else {
  353. totalBounds[0] = Math.min(totalBounds[0], bounds[0]); // 左边界
  354. totalBounds[1] = Math.max(totalBounds[1], bounds[1]); // 上边界
  355. totalBounds[2] = Math.max(totalBounds[2], bounds[2]); // 右边界
  356. totalBounds[3] = Math.min(totalBounds[3], bounds[3]); // 下边界
  357. }
  358. }
  359. return totalBounds;
  360. }
  361. // 标注尺寸
  362. function make_size() {
  363. // 定义当前激活文档
  364. var docRef = activeDocument;
  365. var mm = 25.4 / 72; // pt 和 mm 转换系数
  366. var myFont = textFonts.getByName("MicrosoftYaHei");
  367. var myFontSize = 24;
  368. var x, y;
  369. // 格式化尺寸为 mm 取整数
  370. function formatSize(size) {
  371. return Math.round(size * mm).toFixed(0);
  372. }
  373. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  374. var cmykRed = new CMYKColor();
  375. cmykRed.cyan = 0;
  376. cmykRed.magenta = 100;
  377. cmykRed.yellow = 100;
  378. cmykRed.black = 0;
  379. function writeText(text) {
  380. var textRef = docRef.textFrames.add(); // 建立文本
  381. textRef.contents = text;
  382. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  383. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  384. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  385. textRef.top = y + 15 / mm;
  386. textRef.left = x + 10 / mm;
  387. }
  388. // 遍历选择的物件标注尺寸
  389. if (docRef.selection.length > 0) {
  390. var mySelection = docRef.selection;
  391. for (var i = 0; i < mySelection.length; i++) {
  392. var s = mySelection[i]
  393. x = s.left; y = s.top
  394. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  395. writeText(str)
  396. }
  397. }
  398. }
  399. // 统计物件信息
  400. function shapes_info() {
  401. var sr = app.activeDocument.selection;
  402. var str = "选择物件总数:" + sr.length + "\n";
  403. var text = "";
  404. for (var i = 0; i < sr.length; i++) {
  405. var s = sr[i];
  406. var size = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  407. if (i < 5) str += "第" + (i + 1) + "个尺寸: " + size + "\n";
  408. text += size + " ";
  409. }
  410. alert(str);
  411. // clear the current selection 清除当前选择
  412. app.activeDocument.selection = null;
  413. // add temp objects to hold text for copying 添加临时对象来保存用于复制的文本
  414. var tempObj = app.activeDocument.pathItems.add();
  415. var myText = app.activeDocument.textFrames.add();
  416. myText.contents = text;
  417. tempObj.selected = true;
  418. myText.selected = true;
  419. // copy the text 复制文本 // app.paste(); 粘贴
  420. app.copy();
  421. // remove the two temp object made for copying 删除用于复制的两个临时对象
  422. tempObj.remove();
  423. myText.remove();
  424. }
  425. // 文件名日期
  426. function filename_date() {
  427. // 获取当前时间
  428. function getdate() {
  429. var d = new Date(), month = '' + (d.getMonth() + 1),
  430. day = '' + d.getDate(), year = d.getFullYear();
  431. if (month.length < 2) month = '0' + month;
  432. if (day.length < 2) day = '0' + day;
  433. return [year, month, day].join('-');
  434. }
  435. // 获取 AI文档名称
  436. var docRef = activeDocument;
  437. var str = docRef.name;
  438. str = str + " " + getdate();
  439. var base = new Array();
  440. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  441. // 默认使用文档页面作为范围
  442. var pw = docRef.width; // 文档宽
  443. var ph = docRef.height; // 文档高
  444. var x = base[0]; // 画板左下角 x 坐标
  445. var y = - base[1]; // 画板左下角 y 坐标
  446. x = pw / 2 - x; // 转换x坐标: 画板中下x
  447. // 如果选择物件,使用物件范围
  448. if (app.activeDocument.selection.length > 0) {
  449. var bounds = new Array();
  450. bounds = get_Sel_Bounds();
  451. x = (bounds[0] + bounds[2]) / 2;
  452. y = bounds[3];
  453. }
  454. var myFont = textFonts.getByName("MicrosoftYaHei");
  455. var myFontSize = 8;
  456. function writeText() {
  457. var textRef = docRef.textFrames.add(); // 建立文本
  458. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  459. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  460. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  461. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  462. textRef.top = y + 7.4; // 画板底向上偏移
  463. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  464. textRef.selected = true;
  465. }
  466. writeText();
  467. }
  468. // 借咬口5mm
  469. function mark_5mm() {
  470. // 获取 AI文档名称
  471. var docRef = activeDocument;
  472. var str = docRef.name;
  473. str = "借咬口5mm"
  474. var base = new Array();
  475. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  476. // 默认使用文档页面作为范围
  477. var x = base[0]; // 画板左下角 x 坐标
  478. var y = - base[1]; // 画板左下角 y 坐标
  479. x = docRef.width / 2 - x; // 转换x坐标: 画板中下x
  480. // 如果选择物件,使用物件范围
  481. if (app.activeDocument.selection.length > 0) {
  482. var bounds = new Array();
  483. bounds = get_Sel_Bounds();
  484. x = (bounds[0] + bounds[2]) / 2;
  485. y = bounds[3];
  486. }
  487. var myFont = textFonts.getByName("MicrosoftYaHei");
  488. var myFontSize = 144;
  489. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  490. var cmykRed = new CMYKColor();
  491. cmykRed.cyan = 0;
  492. cmykRed.magenta = 100;
  493. cmykRed.yellow = 100;
  494. cmykRed.black = 0;
  495. function writeText() {
  496. var textRef = docRef.textFrames.add(); // 建立文本
  497. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  498. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  499. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  500. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  501. textRef.top = y - 144;
  502. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  503. textRef.selected = true;
  504. }
  505. writeText();
  506. }
  507. // 批量修改尺寸 使用蒙板几何边界尺寸
  508. function set_size(x, y) {
  509. var sr = app.activeDocument.selection;
  510. for (var i = 0; i < sr.length; i++) {
  511. var s = sr[i];
  512. var bound = NO_CLIP_BOUNDS(s);
  513. // var left = bound[0]; var top = bound[1];
  514. var width = bound[2] - bound[0];
  515. var height = bound[1] - bound[3];
  516. var scale_x = x / mm / width * 100;
  517. var scale_y = y / mm / height * 100;
  518. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  519. s.resize(scale_x, scale_y, true, true, true, true, 100);
  520. }
  521. }
  522. // 批量增加减少尺寸
  523. function modify_size(x, y) {
  524. var sr = app.activeDocument.selection;
  525. for (var i = 0; i < sr.length; i++) {
  526. var s = sr[i];
  527. var bound = NO_CLIP_BOUNDS(s);
  528. var width = bound[2] - bound[0];
  529. var height = bound[1] - bound[3];
  530. var scale_x = (formatSize(width) / mm + x / mm) / width * 100;
  531. var scale_y = (formatSize(height) / mm + y / mm) / height * 100;
  532. s.resize(scale_x, scale_y);
  533. }
  534. }
  535. // 遍历选择的物件尺寸取整
  536. function size_to_integer() {
  537. var sr = app.activeDocument.selection;
  538. for (var i = 0; i < sr.length; i++) {
  539. var s = sr[i];
  540. var bound = NO_CLIP_BOUNDS(s);
  541. var width = bound[2] - bound[0];
  542. var height = bound[1] - bound[3];
  543. var scale_x = formatSize(width) / mm / width * 100;
  544. var scale_y = formatSize(height) / mm / height * 100;
  545. s.resize(scale_x, scale_y);
  546. }
  547. }
  548. // 批量物件旋转角度
  549. function shapes_rotate(angle) {
  550. var sr = app.activeDocument.selection;
  551. for (var i = 0; i < sr.length; i++)
  552. sr[i].rotate(angle);
  553. }
  554. // 物件轮廓边界
  555. function size_by_controlBounds() {
  556. var docRef = activeDocument;
  557. // 判断选择物件2个以上
  558. if (docRef.selection.length > 1) {
  559. // 定义选择物件
  560. mySelection = docRef.selection;
  561. // 最上层物件为替换源
  562. var sourceObj = docRef.selection[0];
  563. // 定义数组保存选择物件controlBounds
  564. var BoundsArray = new Array();
  565. for (var i = 0; i < mySelection.length; i++) {
  566. // PageItem.position 获得物件群组左上角坐标
  567. var sel_Bounds = mySelection[i].controlBounds;
  568. BoundsArray.push(sel_Bounds);
  569. }
  570. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  571. var newGroup = sourceObj.parent.groupItems.add();
  572. for (var i = 1; i < BoundsArray.length; i++) {
  573. var width = BoundsArray[i][2] - BoundsArray[i][0];
  574. var height = BoundsArray[i][1] - BoundsArray[i][3];
  575. sourceObj.width = width;
  576. sourceObj.height = height;
  577. // 移动源文件到目的物件左上角对齐
  578. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  579. sourceObj.position = sel_xy;
  580. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  581. }
  582. sourceObj.remove();
  583. }
  584. }
  585. // 物件几何边界尺寸大小
  586. function size_by_width_height() {
  587. var doc = activeDocument;
  588. // 判断选择物件2个以上
  589. if (doc.selection.length > 1) {
  590. // 定义选择物件
  591. src = doc.selection;
  592. var taget = doc.selection[0];
  593. // PageItem.position 获得物件群组左上角坐标
  594. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  595. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  596. var newGroup = taget.parent.groupItems.add();
  597. for (var i = 1; i < src.length; i++) {
  598. var bound = NO_CLIP_BOUNDS(src[i]);
  599. var left = bound[0]; var top = bound[1];
  600. var width = bound[2] - bound[0];
  601. var height = bound[1] - bound[3];
  602. var sel_xy = new Array(left, top);
  603. taget.width = width;
  604. taget.height = height;
  605. taget.position = sel_xy;
  606. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  607. }
  608. taget.remove();
  609. } else {
  610. make_rectangle();
  611. }
  612. }
  613. function draw_rect(x, y, w, h) {
  614. // 创建一个新的矩形对象
  615. var rect = app.activeDocument.pathItems.rectangle(0, 0, w / mm, h / mm);
  616. // // 设置矩形的位置
  617. rect.position = [x / mm, y / mm];
  618. var M100 = new CMYKColor();
  619. M100.magenta = 100;
  620. // 设置矩形的填充颜色 轮廓颜色和宽度
  621. // rect.fillColor = M100;
  622. rect.filled = false; // 不填充颜色
  623. rect.strokeColor = M100;
  624. rect.strokeWidth = 0.3 / mm;
  625. rect.stroked = true;
  626. // 将矩形添加到文档中
  627. app.activeDocument.pathItems.add(rect);
  628. }
  629. // 从剪贴板数字建立矩形
  630. function make_rectangle() {
  631. // 从剪贴版获得字符串
  632. app.activeDocument.selection = null;
  633. app.paste();
  634. var sr = app.activeDocument.selection;
  635. var str = sr[0].contents;
  636. for (var i = 0; i != sr.length; i++)
  637. sr[i].remove();
  638. // 从字符串中提取储存数字对
  639. // var str = '100x100mm 200x200mm 300x300mm';
  640. var regex = /(\d+)/g;
  641. var match;
  642. var numbers = [];
  643. while ((match = regex.exec(str)) !== null) {
  644. var number = parseInt(match[1]);
  645. numbers.push(number);
  646. }
  647. // 两个数字组成一对尺寸,每个尺寸绘制一个矩形
  648. var sum = 0;
  649. for (var i = 0; i < numbers.length / 2; i++) {
  650. draw_rect(sum, 0, numbers[2 * i], numbers[2 * i + 1]);
  651. sum += numbers[2 * i] + 30;
  652. }
  653. }
  654. // 拼版左上对齐
  655. function replace_align_position() {
  656. var docRef = activeDocument;
  657. // 判断选择物件2个以上
  658. if (docRef.selection.length > 1) {
  659. // 定义选择物件
  660. mySelection = docRef.selection;
  661. // 最上层物件为替换源
  662. var sourceObj = docRef.selection[0];
  663. // 定义数组用来保存选择物件的左上角坐标
  664. var alterObjectArray = new Array();
  665. for (var i = 0; i < mySelection.length; i++) {
  666. // PageItem.position 获得物件群组左上角坐标
  667. var sel_xy = mySelection[i].position
  668. alterObjectArray.push(sel_xy);
  669. }
  670. // 删除用来定位的下层物件
  671. for (var i = 1; i < mySelection.length; i++) {
  672. mySelection[i].remove();
  673. }
  674. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  675. var newGroup = sourceObj.parent.groupItems.add();
  676. for (var i = 1; i < alterObjectArray.length; i++) {
  677. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  678. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  679. }
  680. sourceObj.remove();
  681. }
  682. }
  683. // 拼版中心对齐
  684. function replace_align_center(objremove) {
  685. var docRef = activeDocument;
  686. // 判断选择物件2个以上
  687. if (docRef.selection.length > 1) {
  688. // 定义选择物件
  689. mySelection = docRef.selection;
  690. // 最上层物件为替换源
  691. var sourceObj = docRef.selection[0];
  692. // 定义数组用来保存选择物件的中心坐标 # 计算中心坐标
  693. // x = (left + right) / 2 // y = (top + bottom) / 2
  694. var alterObjectArray = new Array();
  695. for (var i = 0; i < mySelection.length; i++) {
  696. var bound = NO_CLIP_BOUNDS(mySelection[i]);
  697. var sel_xy = new Array((bound[0] + bound[2]) / 2, (bound[1] + bound[3]) / 2);
  698. alterObjectArray.push(sel_xy);
  699. }
  700. // 删除用来定位的下层物件
  701. for (var i = 1; (i < mySelection.length) && objremove; i++) {
  702. mySelection[i].remove();
  703. }
  704. // 获得替换源物件中心坐标
  705. var bound = NO_CLIP_BOUNDS(sourceObj);
  706. var src_xy = new Array((bound[0] + bound[2]) / 2, (bound[1] + bound[3]) / 2);
  707. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  708. var newGroup = sourceObj.parent.groupItems.add();
  709. for (var i = 1; i < alterObjectArray.length; i++) {
  710. var newobj = sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  711. var move_xy = new Array(alterObjectArray[i][0] - src_xy[0], alterObjectArray[i][1] - src_xy[1]);
  712. newobj.translate(move_xy[0], move_xy[1]);
  713. }
  714. sourceObj.remove();
  715. }
  716. }
  717. // 读取加载jsxbin文件,传递给AI软件
  718. function load_jsxbin(file) {
  719. var file = new File(file);
  720. if (file.open('r')) {
  721. var fileContent = file.read();
  722. file.close();
  723. buildMsg(fileContent);
  724. } else {
  725. alert('文件打开失败: ' + file);
  726. }
  727. }
  728. //========== 以下插件引用使用互联网各位大大的插件 =================//
  729. // 标注尺寸增强版 V2.1
  730. function make_size_plus() { load_jsxbin(IconsFolder + "/makesize.dat"); }
  731. // 自动群组
  732. function auto_group() { load_jsxbin(IconsFolder + "/autogroup.dat"); }
  733. // 调整尺寸
  734. function ResizeToSize() { load_jsxbin(IconsFolder + "/resize.dat"); }
  735. // 打包链接图片
  736. function img_pack_links() { load_jsxbin(IconsFolder + "/packlinks.dat"); }
  737. // 重新加载aia脚本文件
  738. function reload_aia() { load_jsxbin(IconsFolder + "/reloadaia.dat"); }
  739. // 群组物件几何边界 // 左 上 宽 高
  740. // var bound = NO_CLIP_BOUNDS(s);
  741. // var left = bound[0]; var top = bound[1];
  742. // var width = bound[2] - bound[0]; var height = bound[1] - bound[3];
  743. function NO_CLIP_BOUNDS(the_obj) {
  744. var NO_CLIP_OBJECTS_AND_MASKS = new Array();
  745. GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj);
  746. var g_left = [];
  747. var g_top = [];
  748. var g_right = [];
  749. var g_bottom = [];
  750. for (var i = 0; i < NO_CLIP_OBJECTS_AND_MASKS.length; i += 1) {
  751. g_left[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[0];
  752. g_top[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[1];
  753. g_right[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[2];
  754. g_bottom[i] = NO_CLIP_OBJECTS_AND_MASKS[i].geometricBounds[3];
  755. }
  756. var g_L = MIN_IN_ARRAY(g_left);
  757. var g_T = MAX_IN_ARRAY(g_top);
  758. var g_R = MAX_IN_ARRAY(g_right);
  759. var g_B = MIN_IN_ARRAY(g_bottom);
  760. return [g_L, g_T, g_R, g_B];
  761. function GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj) {
  762. if (IS_CLIP(the_obj)) {
  763. NO_CLIP_OBJECTS_AND_MASKS.push(the_obj.pageItems[0]);
  764. return;
  765. }
  766. if (the_obj.constructor.name == "GroupItem") {
  767. try {
  768. var N_sub_obj = the_obj.pageItems.length;
  769. for (var i = 0; i < N_sub_obj; i += 1) {
  770. GET_NO_CLIP_OBJECTS_AND_MASKS(the_obj.pageItems[i]);
  771. }
  772. } catch (error) {
  773. }
  774. return;
  775. }
  776. NO_CLIP_OBJECTS_AND_MASKS.push(the_obj);
  777. return;
  778. }
  779. }
  780. function IS_CLIP(the_obj) {
  781. try {
  782. if (the_obj.constructor.name == "GroupItem") {
  783. if (the_obj.clipped) {
  784. return true;
  785. }
  786. }
  787. } catch (error) {
  788. }
  789. return false;
  790. }
  791. function MAX_IN_ARRAY(the_array) {
  792. var MAX = the_array[0];
  793. for (var i = 0; i < the_array.length; i += 1) {
  794. if (the_array[i] > MAX) {
  795. MAX = the_array[i]
  796. }
  797. }
  798. return MAX;
  799. }
  800. function MIN_IN_ARRAY(the_array) {
  801. var MIN = the_array[0];
  802. for (var i = 0; i < the_array.length; i += 1) {
  803. if (the_array[i] < MIN) {
  804. MIN = the_array[i]
  805. }
  806. }
  807. return MIN;
  808. }