LanyaAITools.jsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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.11.11");
  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. // 设置按钮大小与图片大小相同
  55. button8.preferredSize = [26, 26];
  56. button1.onClick = function () {
  57. if (ScriptUI.environment.keyboardState.ctrlKey) {
  58. buildMsg("shapes_info();");
  59. } else if (ScriptUI.environment.keyboardState.altKey) {
  60. make_size_plus();
  61. } else {
  62. buildMsg("make_size();");
  63. }
  64. };
  65. button2.onClick = function () {
  66. if (ScriptUI.environment.keyboardState.ctrlKey) {
  67. // Ctrl 加鼠标左键,自定义数字
  68. var input = prompt("请输入角度数字:", "45");
  69. if (!isNaN(parseFloat(input)))
  70. buildMsg("shapes_rotate(" + input + ");");
  71. } else if (ScriptUI.environment.keyboardState.altKey) {
  72. buildMsg("shapes_rotate(180);");
  73. } else {
  74. buildMsg("shapes_rotate(90);");
  75. }
  76. };
  77. button3.onClick = function () {
  78. if (ScriptUI.environment.keyboardState.altKey) {
  79. buildMsg("mark_5mm();");
  80. } else {
  81. buildMsg("filename_date();");
  82. }
  83. };
  84. button4.onClick = function () {
  85. if (ScriptUI.environment.keyboardState.ctrlKey) {
  86. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  87. } else if (ScriptUI.environment.keyboardState.altKey) {
  88. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  89. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  90. // alert("ScriptUI.environment.keyboardState.shiftKey");
  91. var input = prompt("请输如宽和高两个数字(例如: 100 80):", "100 80");
  92. // 使用正则表达式匹配数字
  93. var regex = /(\d+)\s*(\d+)/;
  94. var match = input.match(regex);
  95. if (match) {
  96. var number1 = parseInt(match[1]);
  97. var number2 = parseInt(match[2]);
  98. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  99. } else {
  100. alert("输入格式不正确!");
  101. }
  102. } else {
  103. buildMsg("size_to_integer();");
  104. }
  105. };
  106. button5.onClick = function () {
  107. if (ScriptUI.environment.keyboardState.altKey) {
  108. buildMsg("img_pack_links();");
  109. } else {
  110. buildMsg("replace_align_position();");
  111. }
  112. };
  113. button6.onClick = function () {
  114. if (ScriptUI.environment.keyboardState.altKey) {
  115. ResizeToSize();
  116. } else {
  117. auto_group();
  118. }
  119. };
  120. button7.onClick = function () {
  121. if (ScriptUI.environment.keyboardState.altKey) {
  122. buildMsg("size_by_controlBounds();");
  123. } else {
  124. buildMsg("size_by_width_height();");
  125. }
  126. };
  127. button8.onClick = function () {
  128. icon_panel();
  129. panel.close();
  130. };
  131. // 显示面板
  132. panel.show();
  133. }
  134. function icon_panel() {
  135. var panel = new Window("palette", "©蘭雅 Adobe Illustrator 工具箱");
  136. panel.onClose = function () {
  137. saveWindowPosition(panel);
  138. };
  139. panel.alignChildren = ["left", "top"];
  140. panel.spacing = 2;
  141. panel.margins = 3;
  142. // 创建按钮组
  143. var BtGroup1 = panel.add("group");
  144. var BtGroup2 = panel.add("group");
  145. // 设置按钮组为水平布局
  146. BtGroup1.orientation = "row";
  147. BtGroup2.orientation = "row";
  148. // 设置按钮组的边缘间距
  149. BtGroup1.spacing = 2; // 调整按钮之间的间距
  150. BtGroup2.spacing = 2;
  151. // scriptFile = new File($.fileName); // 获取当前脚本文件的路径
  152. // var iconFile = new File(scriptFile.path + "/icon/icon.png"); // 拼接图标文件的完整路径
  153. var iconF1 = IconsFolder + "/size.png";
  154. var iconF2 = IconsFolder + "/icon.png";
  155. var iconF3 = IconsFolder + "/mark.png";
  156. var iconF4 = IconsFolder + "/debug.png";
  157. var iconF5 = IconsFolder + "/replace.png";
  158. var iconF6 = IconsFolder + "/gpucard.png";
  159. var iconF7 = IconsFolder + "/byBounds.png";
  160. var iconF8 = IconsFolder + "/repeat.png";
  161. // 添加图标按钮
  162. var button1 = BtGroup1.add("iconbutton", undefined, iconF1);
  163. var button2 = BtGroup1.add("iconbutton", undefined, iconF2);
  164. var button3 = BtGroup1.add("iconbutton", undefined, iconF3);
  165. var button4 = BtGroup1.add("iconbutton", undefined, iconF4);
  166. var button5 = BtGroup2.add("iconbutton", undefined, iconF5);
  167. var button6 = BtGroup2.add("iconbutton", undefined, iconF6);
  168. var button7 = BtGroup2.add("iconbutton", undefined, iconF7);
  169. var button8 = BtGroup2.add("iconbutton", undefined, iconF8);
  170. button1.helpTip = "标注尺寸, <Alt>增强标注";
  171. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  172. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  173. button4.helpTip = "尺寸取整, <Alt-Ctrl-Shift>微调统一尺寸";
  174. button5.helpTip = "左上对齐快速替换, <Alt>打包连接图";
  175. button6.helpTip = "自动群组, <Alt>调整尺寸";
  176. button7.helpTip = "尺寸复制, <Alt>包括轮廓";
  177. button8.helpTip = "<Ctrl>微调距离, 最小化窗口";
  178. // 设置按钮大小与图片大小相同
  179. button1.preferredSize = [48, 48];
  180. button2.preferredSize = [48, 48];
  181. button3.preferredSize = [48, 48];
  182. button4.preferredSize = [48, 48];
  183. button5.preferredSize = [48, 48];
  184. button6.preferredSize = [48, 48];
  185. button7.preferredSize = [48, 48];
  186. button8.preferredSize = [48, 48];
  187. button1.onClick = function () {
  188. if (ScriptUI.environment.keyboardState.ctrlKey) {
  189. buildMsg("shapes_info();");
  190. } else if (ScriptUI.environment.keyboardState.altKey) {
  191. make_size_plus();
  192. } else {
  193. buildMsg("make_size();");
  194. }
  195. };
  196. button2.onClick = function () {
  197. if (ScriptUI.environment.keyboardState.ctrlKey) {
  198. // Ctrl 加鼠标左键,自定义数字
  199. var input = prompt("请输入角度数字:", "45");
  200. if (!isNaN(parseFloat(input)))
  201. buildMsg("shapes_rotate(" + input + ");");
  202. } else if (ScriptUI.environment.keyboardState.altKey) {
  203. buildMsg("shapes_rotate(180);");
  204. } else {
  205. buildMsg("shapes_rotate(90);");
  206. }
  207. };
  208. button3.onClick = function () {
  209. if (ScriptUI.environment.keyboardState.altKey) {
  210. buildMsg("mark_5mm();");
  211. } else {
  212. buildMsg("filename_date();");
  213. }
  214. };
  215. button4.onClick = function () {
  216. if (ScriptUI.environment.keyboardState.ctrlKey) {
  217. buildMsg("modify_size(-" + micro_distance + ", -" + micro_distance + ");");
  218. } else if (ScriptUI.environment.keyboardState.altKey) {
  219. buildMsg("modify_size(" + micro_distance + ", " + micro_distance + ");");
  220. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  221. // alert("ScriptUI.environment.keyboardState.shiftKey");
  222. var input = prompt("请输如宽和高两个数字(例如: 100 80):", "100 80");
  223. // 使用正则表达式匹配数字
  224. var regex = /(\d+)\s*(\d+)/;
  225. var match = input.match(regex);
  226. if (match) {
  227. var number1 = parseInt(match[1]);
  228. var number2 = parseInt(match[2]);
  229. buildMsg("set_size(" + number1 + ", " + number2 + ");");
  230. } else {
  231. alert("输入格式不正确!");
  232. }
  233. } else {
  234. buildMsg("size_to_integer();");
  235. }
  236. };
  237. button5.onClick = function () {
  238. if (ScriptUI.environment.keyboardState.altKey) {
  239. buildMsg("img_pack_links();");
  240. } else {
  241. buildMsg("replace_align_position();");
  242. }
  243. };
  244. button6.onClick = function () {
  245. if (ScriptUI.environment.keyboardState.ctrlKey) {
  246. alert("Ctrl信息; Alt 调整尺寸; Shift重新加载脚本; 默认自动群组");
  247. } else if (ScriptUI.environment.keyboardState.altKey) {
  248. ResizeToSize();
  249. } else if (ScriptUI.environment.keyboardState.shiftKey) {
  250. reload_aia();
  251. } {
  252. auto_group();
  253. }
  254. };
  255. button7.onClick = function () {
  256. if (ScriptUI.environment.keyboardState.altKey) {
  257. buildMsg("size_by_controlBounds();");
  258. } else {
  259. buildMsg("size_by_width_height();");
  260. }
  261. };
  262. button8.onClick = function () {
  263. if (ScriptUI.environment.keyboardState.ctrlKey) {
  264. micro_distance = prompt("设置微调距离(mm): ", micro_distance);
  265. } else if (ScriptUI.environment.keyboardState.altKey) {
  266. main_panel();
  267. panel.close();
  268. } else {
  269. mini_panel();
  270. panel.close();
  271. }
  272. };
  273. // 显示面板
  274. panel.show();
  275. // 恢复窗口位置
  276. restoreWindowPosition(panel);
  277. }
  278. function mini_panel() {
  279. var panel = new Window("palette", "");
  280. panel.spacing = 0;
  281. panel.margins = [0, 0, 0, 0];;
  282. var icon = IconsFolder + "/repeat.png";
  283. var button_mini = panel.add("iconbutton", undefined, icon);
  284. button_mini.preferredSize = [40, 40];
  285. button_mini.spacing = 0;
  286. button_mini.onClick = function () {
  287. icon_panel();
  288. panel.close();
  289. };
  290. panel.show();
  291. restoreWindowPosition(panel);
  292. panel.bounds.height = 42;
  293. panel.bounds.width = 50;
  294. }
  295. // 保存窗口位置
  296. function saveWindowPosition(window) {
  297. var position = window.bounds;
  298. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  299. settingsFile.open("w");
  300. settingsFile.write(position.left + "," + position.top + "," + position.right + "," + position.bottom);
  301. settingsFile.close();
  302. }
  303. // 恢复窗口位置
  304. function restoreWindowPosition(window) {
  305. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  306. if (settingsFile.exists) {
  307. settingsFile.open("r");
  308. var position = settingsFile.read().split(",");
  309. settingsFile.close();
  310. window.bounds.left = parseInt(position[0]);
  311. window.bounds.top = parseInt(position[1]);
  312. window.bounds.right = parseInt(position[2]);
  313. window.bounds.bottom = parseInt(position[3]);
  314. }
  315. }
  316. //==================================================================================//
  317. // 蘭雅 Adobe Illustrator 工具箱© 2023.11.11 各个按钮功能模块
  318. //==================================================================================//
  319. var mm = 25.4 / 72; // pt 和 mm 转换系数
  320. // 格式化尺寸为 mm 取整数
  321. function formatSize(size) {
  322. return Math.round(size * mm).toFixed(0);
  323. }
  324. // 获得选择对象的边界框
  325. function get_Sel_Bounds() {
  326. var totalBounds = null;
  327. var sr = app.activeDocument.selection;
  328. for (var i = 0; i < sr.length; i++) {
  329. var item = sr[i];
  330. // 获取对象的边界框
  331. var bounds = item.geometricBounds;
  332. // 更新总范围
  333. if (totalBounds === null) {
  334. totalBounds = bounds.slice(); // 创建边界框的副本
  335. } else {
  336. totalBounds[0] = Math.min(totalBounds[0], bounds[0]); // 左边界
  337. totalBounds[1] = Math.max(totalBounds[1], bounds[1]); // 上边界
  338. totalBounds[2] = Math.max(totalBounds[2], bounds[2]); // 右边界
  339. totalBounds[3] = Math.min(totalBounds[3], bounds[3]); // 下边界
  340. }
  341. }
  342. return totalBounds;
  343. }
  344. // 标注尺寸
  345. function make_size() {
  346. // 定义当前激活文档
  347. var docRef = activeDocument;
  348. var mm = 25.4 / 72; // pt 和 mm 转换系数
  349. var myFont = textFonts.getByName("MicrosoftYaHei");
  350. var myFontSize = 24;
  351. var x, y;
  352. // 格式化尺寸为 mm 取整数
  353. function formatSize(size) {
  354. return Math.round(size * mm).toFixed(0);
  355. }
  356. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  357. var cmykRed = new CMYKColor();
  358. cmykRed.cyan = 0;
  359. cmykRed.magenta = 100;
  360. cmykRed.yellow = 100;
  361. cmykRed.black = 0;
  362. function writeText(text) {
  363. var textRef = docRef.textFrames.add(); // 建立文本
  364. textRef.contents = text;
  365. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  366. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  367. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  368. textRef.top = y + 15 / mm;
  369. textRef.left = x + 10 / mm;
  370. }
  371. // 遍历选择的物件标注尺寸
  372. if (docRef.selection.length > 0) {
  373. var mySelection = docRef.selection;
  374. for (var i = 0; i < mySelection.length; i++) {
  375. var s = mySelection[i]
  376. x = s.left; y = s.top
  377. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  378. writeText(str)
  379. }
  380. }
  381. }
  382. // 统计物件信息
  383. function shapes_info() {
  384. var sr = app.activeDocument.selection;
  385. var str = "选择物件总数:" + sr.length + "\n";
  386. var text ="";
  387. for (var i = 0; i < sr.length; i++) {
  388. var s = sr[i];
  389. var size = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  390. if (i < 5) str += "第" + (i + 1) + "个尺寸: " + size + "\n";
  391. text += size + " ";
  392. }
  393. alert(str);
  394. // clear the current selection 清除当前选择
  395. app.activeDocument.selection = null;
  396. // add temp objects to hold text for copying 添加临时对象来保存用于复制的文本
  397. var tempObj = app.activeDocument.pathItems.add();
  398. var myText = app.activeDocument.textFrames.add();
  399. myText.contents = text;
  400. tempObj.selected = true;
  401. myText.selected = true;
  402. // copy the text 复制文本 // app.paste(); 粘贴
  403. app.copy();
  404. // remove the two temp object made for copying 删除用于复制的两个临时对象
  405. tempObj.remove();
  406. myText.remove();
  407. }
  408. // 文件名日期
  409. function filename_date() {
  410. // 获取当前时间
  411. function getdate() {
  412. var d = new Date(), month = '' + (d.getMonth() + 1),
  413. day = '' + d.getDate(), year = d.getFullYear();
  414. if (month.length < 2) month = '0' + month;
  415. if (day.length < 2) day = '0' + day;
  416. return [year, month, day].join('-');
  417. }
  418. // 获取 AI文档名称
  419. var docRef = activeDocument;
  420. var str = docRef.name;
  421. str = str + " " + getdate();
  422. var base = new Array();
  423. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  424. // 默认使用文档页面作为范围
  425. var pw = docRef.width; // 文档宽
  426. var ph = docRef.height; // 文档高
  427. var x = base[0]; // 画板左下角 x 坐标
  428. var y = - base[1]; // 画板左下角 y 坐标
  429. x = pw / 2 - x; // 转换x坐标: 画板中下x
  430. // 如果选择物件,使用物件范围
  431. if (app.activeDocument.selection.length > 0) {
  432. var bounds = new Array();
  433. bounds = get_Sel_Bounds();
  434. x = (bounds[0] + bounds[2]) / 2;
  435. y = bounds[3];
  436. }
  437. var myFont = textFonts.getByName("MicrosoftYaHei");
  438. var myFontSize = 8;
  439. function writeText() {
  440. var textRef = docRef.textFrames.add(); // 建立文本
  441. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  442. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  443. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  444. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  445. textRef.top = y + 7.4; // 画板底向上偏移
  446. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  447. textRef.selected = true;
  448. }
  449. writeText();
  450. }
  451. // 借咬口5mm
  452. function mark_5mm() {
  453. // 获取 AI文档名称
  454. var docRef = activeDocument;
  455. var str = docRef.name;
  456. str = "借咬口5mm"
  457. var base = new Array();
  458. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  459. // 默认使用文档页面作为范围
  460. var x = base[0]; // 画板左下角 x 坐标
  461. var y = - base[1]; // 画板左下角 y 坐标
  462. x = docRef.width / 2 - x; // 转换x坐标: 画板中下x
  463. // 如果选择物件,使用物件范围
  464. if (app.activeDocument.selection.length > 0) {
  465. var bounds = new Array();
  466. bounds = get_Sel_Bounds();
  467. x = (bounds[0] + bounds[2]) / 2;
  468. y = bounds[3];
  469. }
  470. var myFont = textFonts.getByName("MicrosoftYaHei");
  471. var myFontSize = 144;
  472. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  473. var cmykRed = new CMYKColor();
  474. cmykRed.cyan = 0;
  475. cmykRed.magenta = 100;
  476. cmykRed.yellow = 100;
  477. cmykRed.black = 0;
  478. function writeText() {
  479. var textRef = docRef.textFrames.add(); // 建立文本
  480. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  481. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  482. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  483. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  484. textRef.top = y - 144;
  485. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  486. textRef.selected = true;
  487. }
  488. writeText();
  489. }
  490. // 批量修改尺寸
  491. function set_size(x, y) {
  492. var sr = app.activeDocument.selection;
  493. for (var i = 0; i < sr.length; i++) {
  494. var s = sr[i];
  495. var scale_x = x / mm / s.width * 100;
  496. var scale_y = y / mm / s.height * 100;
  497. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  498. s.resize(scale_x, scale_y, true, true, true, true, 100);
  499. }
  500. }
  501. // 批量增加减少尺寸
  502. function modify_size(x, y) {
  503. var sr = app.activeDocument.selection;
  504. for (var i = 0; i < sr.length; i++) {
  505. var s = sr[i];
  506. var scale_x = (formatSize(s.width) / mm + x / mm) / s.width * 100;
  507. var scale_y = (formatSize(s.height) / mm + y / mm) / s.height * 100;
  508. s.resize(scale_x, scale_y);
  509. }
  510. }
  511. // 遍历选择的物件尺寸取整
  512. function size_to_integer() {
  513. var sr = app.activeDocument.selection;
  514. for (var i = 0; i < sr.length; i++) {
  515. var s = sr[i];
  516. var scale_x = formatSize(s.width) / mm / s.width * 100;
  517. var scale_y = formatSize(s.height) / mm / s.height * 100;
  518. s.resize(scale_x, scale_y);
  519. }
  520. }
  521. // 批量物件旋转角度
  522. function shapes_rotate(angle) {
  523. var sr = app.activeDocument.selection;
  524. for (var i = 0; i < sr.length; i++)
  525. sr[i].rotate(angle);
  526. }
  527. // 物件轮廓边界
  528. function size_by_controlBounds() {
  529. var docRef = activeDocument;
  530. // 判断选择物件2个以上
  531. if (docRef.selection.length > 1) {
  532. // 定义选择物件
  533. mySelection = docRef.selection;
  534. // 最上层物件为替换源
  535. var sourceObj = docRef.selection[0];
  536. // 定义数组保存选择物件controlBounds
  537. var BoundsArray = new Array();
  538. for (var i = 0; i < mySelection.length; i++) {
  539. // PageItem.position 获得物件群组左上角坐标
  540. var sel_Bounds = mySelection[i].controlBounds;
  541. BoundsArray.push(sel_Bounds);
  542. }
  543. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  544. var newGroup = sourceObj.parent.groupItems.add();
  545. for (var i = 1; i < BoundsArray.length; i++) {
  546. var width = BoundsArray[i][2] - BoundsArray[i][0];
  547. var height = BoundsArray[i][1] - BoundsArray[i][3];
  548. sourceObj.width = width;
  549. sourceObj.height = height;
  550. // 移动源文件到目的物件左上角对齐
  551. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  552. sourceObj.position = sel_xy;
  553. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  554. }
  555. sourceObj.remove();
  556. }
  557. }
  558. // 物件尺寸大小
  559. function size_by_width_height() {
  560. var doc = activeDocument;
  561. // 判断选择物件2个以上
  562. if (doc.selection.length > 1) {
  563. // 定义选择物件
  564. src = doc.selection;
  565. var taget = doc.selection[0];
  566. // PageItem.position 获得物件群组左上角坐标
  567. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  568. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  569. var newGroup = taget.parent.groupItems.add();
  570. for (var i = 1; i < src.length; i++) {
  571. var sel_xy = src[i].position;
  572. taget.width = src[i].width;
  573. taget.height = src[i].height;
  574. taget.position = sel_xy;
  575. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  576. }
  577. taget.remove();
  578. }
  579. }
  580. function make_rectangle(){
  581. // 创建一个新的矩形对象
  582. var rect = app.activeDocument.pathItems.rectangle(0, 0, 100 * mm, 100 * mm);
  583. // 设置矩形的位置
  584. rect.position = [0, 0];
  585. // 设置矩形的填充颜色
  586. rect.fillColor = new RGBColor(255, 0, 0); // 这里使用红色作为示例
  587. // 将矩形添加到文档中
  588. app.activeDocument.layers[0].pathItems.add(rect);
  589. }
  590. // 拼版左上对齐
  591. function replace_align_position() {
  592. var docRef = activeDocument;
  593. // 判断选择物件2个以上
  594. if (docRef.selection.length > 1) {
  595. // 定义选择物件
  596. mySelection = docRef.selection;
  597. // 最上层物件为替换源
  598. var sourceObj = docRef.selection[0];
  599. // 定义数组用来保存选择物件的左上角坐标
  600. var alterObjectArray = new Array();
  601. for (var i = 0; i < mySelection.length; i++) {
  602. // PageItem.position 获得物件群组左上角坐标
  603. var sel_xy = mySelection[i].position
  604. alterObjectArray.push(sel_xy);
  605. }
  606. // 删除用来定位的下层物件
  607. for (var i = 1; i < mySelection.length; i++) {
  608. mySelection[i].remove();
  609. }
  610. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  611. var newGroup = sourceObj.parent.groupItems.add();
  612. for (var i = 1; i < alterObjectArray.length; i++) {
  613. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  614. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  615. }
  616. sourceObj.remove();
  617. }
  618. }
  619. // 读取加载jsxbin文件,传递给AI软件
  620. function load_jsxbin(file) {
  621. var file = new File(file);
  622. if (file.open('r')) {
  623. var fileContent = file.read();
  624. file.close();
  625. buildMsg(fileContent);
  626. } else {
  627. alert('文件打开失败: ' + file);
  628. }
  629. }
  630. //========== 以下插件引用使用互联网各位大大的插件 =================//
  631. // 标注尺寸增强版 V2.1
  632. function make_size_plus() { load_jsxbin(IconsFolder + "/makesize.dat"); }
  633. // 自动群组
  634. function auto_group() { load_jsxbin(IconsFolder + "/autogroup.dat"); }
  635. // 调整尺寸
  636. function ResizeToSize() { load_jsxbin(IconsFolder + "/resize.dat"); }
  637. // 打包链接图片
  638. function img_pack_links() { load_jsxbin(IconsFolder + "/packlinks.dat"); }
  639. // 重新加载aia脚本文件
  640. function reload_aia() { load_jsxbin(IconsFolder + "/reloadaia.dat"); }