LanyaAITools.jsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. // 按钮点击事件处理程序
  57. button1.onClick = function () {
  58. if (ScriptUI.environment.keyboardState.altKey) {
  59. // Alt 键被按下
  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(-1, -1);");
  87. } else if (ScriptUI.environment.keyboardState.altKey) {
  88. buildMsg("modify_size(1, 1);");
  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>微调距离, <Alt>最小化窗口";
  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. // 按钮点击事件处理程序
  188. button1.onClick = function () {
  189. if (ScriptUI.environment.keyboardState.altKey) {
  190. // Alt 键被按下
  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.altKey) {
  246. ResizeToSize();
  247. } else {
  248. auto_group();
  249. }
  250. };
  251. button7.onClick = function () {
  252. if (ScriptUI.environment.keyboardState.altKey) {
  253. buildMsg("size_by_controlBounds();");
  254. } else {
  255. buildMsg("size_by_width_height();");
  256. }
  257. };
  258. button8.onClick = function () {
  259. if (ScriptUI.environment.keyboardState.ctrlKey) {
  260. micro_distance = prompt("设置微调距离(mm): ", micro_distance);
  261. } else if (ScriptUI.environment.keyboardState.altKey) {
  262. mini_panel();
  263. panel.close();
  264. } else {
  265. main_panel();
  266. panel.close();
  267. }
  268. };
  269. // 显示面板
  270. panel.show();
  271. // 恢复窗口位置
  272. restoreWindowPosition(panel);
  273. }
  274. function mini_panel() {
  275. var panel = new Window("palette", "");
  276. panel.spacing = 0;
  277. panel.margins = 0;
  278. var icon = IconsFolder + "/repeat.png";
  279. var button_mini = panel.add("iconbutton", undefined, icon);
  280. button_mini.preferredSize = [40, 40];
  281. button_mini.spacing = 0;
  282. button_mini.onClick = function () {
  283. icon_panel();
  284. panel.close();
  285. };
  286. panel.show();
  287. restoreWindowPosition(panel);
  288. panel.bounds.height = 42;
  289. panel.bounds.width = 50;
  290. }
  291. // 保存窗口位置
  292. function saveWindowPosition(window) {
  293. var position = window.bounds;
  294. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  295. settingsFile.open("w");
  296. settingsFile.write(position.left + "," + position.top + "," + position.right + "," + position.bottom);
  297. settingsFile.close();
  298. }
  299. // 恢复窗口位置
  300. function restoreWindowPosition(window) {
  301. var settingsFile = new File(IconsFolder + "/windowSettings.ini");
  302. if (settingsFile.exists) {
  303. settingsFile.open("r");
  304. var position = settingsFile.read().split(",");
  305. settingsFile.close();
  306. window.bounds.left = parseInt(position[0]);
  307. window.bounds.top = parseInt(position[1]);
  308. window.bounds.right = parseInt(position[2]);
  309. window.bounds.bottom = parseInt(position[3]);
  310. }
  311. }
  312. //==================================================================================//
  313. // 蘭雅 Adobe Illustrator 工具箱© 2023.11.11 各个按钮功能模块
  314. //==================================================================================//
  315. var mm = 25.4 / 72; // pt 和 mm 转换系数
  316. // 格式化尺寸为 mm 取整数
  317. function formatSize(size) {
  318. return Math.round(size * mm).toFixed(0);
  319. }
  320. // 获得选择对象的边界框
  321. function get_Sel_Bounds() {
  322. var totalBounds = null;
  323. var sr = app.activeDocument.selection;
  324. for (var i = 0; i < sr.length; i++) {
  325. var item = sr[i];
  326. // 获取对象的边界框
  327. var bounds = item.geometricBounds;
  328. // 更新总范围
  329. if (totalBounds === null) {
  330. totalBounds = bounds.slice(); // 创建边界框的副本
  331. } else {
  332. totalBounds[0] = Math.min(totalBounds[0], bounds[0]); // 左边界
  333. totalBounds[1] = Math.max(totalBounds[1], bounds[1]); // 上边界
  334. totalBounds[2] = Math.max(totalBounds[2], bounds[2]); // 右边界
  335. totalBounds[3] = Math.min(totalBounds[3], bounds[3]); // 下边界
  336. }
  337. }
  338. return totalBounds;
  339. }
  340. // 标注尺寸
  341. function make_size() {
  342. // 定义当前激活文档
  343. var docRef = activeDocument;
  344. var mm = 25.4 / 72; // pt 和 mm 转换系数
  345. var myFont = textFonts.getByName("MicrosoftYaHei");
  346. var myFontSize = 24;
  347. var x, y;
  348. // 格式化尺寸为 mm 取整数
  349. function formatSize(size) {
  350. return Math.round(size * mm).toFixed(0);
  351. }
  352. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  353. var cmykRed = new CMYKColor();
  354. cmykRed.cyan = 0;
  355. cmykRed.magenta = 100;
  356. cmykRed.yellow = 100;
  357. cmykRed.black = 0;
  358. function writeText(text) {
  359. var textRef = docRef.textFrames.add(); // 建立文本
  360. textRef.contents = text;
  361. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  362. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  363. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  364. textRef.top = y + 15 / mm;
  365. textRef.left = x + 10 / mm;
  366. }
  367. // 遍历选择的物件标注尺寸
  368. if (docRef.selection.length > 0) {
  369. var mySelection = docRef.selection;
  370. for (var i = 0; i < mySelection.length; i++) {
  371. var s = mySelection[i]
  372. x = s.left; y = s.top
  373. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  374. writeText(str)
  375. }
  376. }
  377. }
  378. // 文件名日期
  379. function filename_date() {
  380. // 获取当前时间
  381. function getdate() {
  382. var d = new Date(), month = '' + (d.getMonth() + 1),
  383. day = '' + d.getDate(), year = d.getFullYear();
  384. if (month.length < 2) month = '0' + month;
  385. if (day.length < 2) day = '0' + day;
  386. return [year, month, day].join('-');
  387. }
  388. // 获取 AI文档名称
  389. var docRef = activeDocument;
  390. var str = docRef.name;
  391. str = str + " " + getdate();
  392. var base = new Array();
  393. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  394. // 默认使用文档页面作为范围
  395. var pw = docRef.width; // 文档宽
  396. var ph = docRef.height; // 文档高
  397. var x = base[0]; // 画板左下角 x 坐标
  398. var y = - base[1]; // 画板左下角 y 坐标
  399. x = pw / 2 - x; // 转换x坐标: 画板中下x
  400. // 如果选择物件,使用物件范围
  401. if (app.activeDocument.selection.length > 0) {
  402. var bounds = new Array();
  403. bounds = get_Sel_Bounds();
  404. x = (bounds[0] + bounds[2]) / 2;
  405. y = bounds[3];
  406. }
  407. var myFont = textFonts.getByName("MicrosoftYaHei");
  408. var myFontSize = 8;
  409. function writeText() {
  410. var textRef = docRef.textFrames.add(); // 建立文本
  411. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  412. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  413. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  414. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  415. textRef.top = y + 7.4; // 画板底向上偏移
  416. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  417. textRef.selected = true;
  418. }
  419. writeText();
  420. }
  421. // 借咬口5mm
  422. function mark_5mm() {
  423. // 获取 AI文档名称
  424. var docRef = activeDocument;
  425. var str = docRef.name;
  426. str = "借咬口5mm"
  427. var base = new Array();
  428. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  429. // 默认使用文档页面作为范围
  430. var x = base[0]; // 画板左下角 x 坐标
  431. var y = - base[1]; // 画板左下角 y 坐标
  432. x = docRef.width / 2 - x; // 转换x坐标: 画板中下x
  433. // 如果选择物件,使用物件范围
  434. if (app.activeDocument.selection.length > 0) {
  435. var bounds = new Array();
  436. bounds = get_Sel_Bounds();
  437. x = (bounds[0] + bounds[2]) / 2;
  438. y = bounds[3];
  439. }
  440. var myFont = textFonts.getByName("MicrosoftYaHei");
  441. var myFontSize = 144;
  442. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  443. var cmykRed = new CMYKColor();
  444. cmykRed.cyan = 0;
  445. cmykRed.magenta = 100;
  446. cmykRed.yellow = 100;
  447. cmykRed.black = 0;
  448. function writeText() {
  449. var textRef = docRef.textFrames.add(); // 建立文本
  450. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  451. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  452. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  453. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  454. textRef.top = y - 144;
  455. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  456. textRef.selected = true;
  457. }
  458. writeText();
  459. }
  460. // 批量修改尺寸
  461. function set_size(x, y) {
  462. var sr = app.activeDocument.selection;
  463. for (var i = 0; i < sr.length; i++) {
  464. var s = sr[i];
  465. var scale_x = x / mm / s.width * 100;
  466. var scale_y = y / mm / s.height * 100;
  467. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  468. s.resize(scale_x, scale_y, true, true, true, true, 100);
  469. }
  470. }
  471. // 批量增加减少尺寸
  472. function modify_size(x, y) {
  473. var sr = app.activeDocument.selection;
  474. for (var i = 0; i < sr.length; i++) {
  475. var s = sr[i];
  476. var scale_x = (formatSize(s.width) / mm + x / mm) / s.width * 100;
  477. var scale_y = (formatSize(s.height) / mm + y / mm) / s.height * 100;
  478. s.resize(scale_x, scale_y);
  479. }
  480. }
  481. // 遍历选择的物件尺寸取整
  482. function size_to_integer() {
  483. var sr = app.activeDocument.selection;
  484. for (var i = 0; i < sr.length; i++) {
  485. var s = sr[i];
  486. var scale_x = formatSize(s.width) / mm / s.width * 100;
  487. var scale_y = formatSize(s.height) / mm / s.height * 100;
  488. s.resize(scale_x, scale_y);
  489. }
  490. }
  491. // 批量物件旋转角度
  492. function shapes_rotate(angle) {
  493. var sr = app.activeDocument.selection;
  494. for (var i = 0; i < sr.length; i++)
  495. sr[i].rotate(angle);
  496. }
  497. // 物件轮廓边界
  498. function size_by_controlBounds() {
  499. var docRef = activeDocument;
  500. // 判断选择物件2个以上
  501. if (docRef.selection.length > 1) {
  502. // 定义选择物件
  503. mySelection = docRef.selection;
  504. // 最上层物件为替换源
  505. var sourceObj = docRef.selection[0];
  506. // 定义数组保存选择物件controlBounds
  507. var BoundsArray = new Array();
  508. for (var i = 0; i < mySelection.length; i++) {
  509. // PageItem.position 获得物件群组左上角坐标
  510. var sel_Bounds = mySelection[i].controlBounds;
  511. BoundsArray.push(sel_Bounds);
  512. }
  513. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  514. var newGroup = sourceObj.parent.groupItems.add();
  515. for (var i = 1; i < BoundsArray.length; i++) {
  516. var width = BoundsArray[i][2] - BoundsArray[i][0];
  517. var height = BoundsArray[i][1] - BoundsArray[i][3];
  518. sourceObj.width = width;
  519. sourceObj.height = height;
  520. // 移动源文件到目的物件左上角对齐
  521. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  522. sourceObj.position = sel_xy;
  523. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  524. }
  525. sourceObj.remove();
  526. }
  527. }
  528. // 物件尺寸大小
  529. function size_by_width_height() {
  530. var doc = activeDocument;
  531. // 判断选择物件2个以上
  532. if (doc.selection.length > 1) {
  533. // 定义选择物件
  534. src = doc.selection;
  535. var taget = doc.selection[0];
  536. // PageItem.position 获得物件群组左上角坐标
  537. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  538. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  539. var newGroup = taget.parent.groupItems.add();
  540. for (var i = 1; i < src.length; i++) {
  541. var sel_xy = src[i].position;
  542. taget.width = src[i].width;
  543. taget.height = src[i].height;
  544. taget.position = sel_xy;
  545. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  546. }
  547. taget.remove();
  548. }
  549. }
  550. // 拼版左上对齐
  551. function replace_align_position() {
  552. var docRef = activeDocument;
  553. // 判断选择物件2个以上
  554. if (docRef.selection.length > 1) {
  555. // 定义选择物件
  556. mySelection = docRef.selection;
  557. // 最上层物件为替换源
  558. var sourceObj = docRef.selection[0];
  559. // 定义数组用来保存选择物件的左上角坐标
  560. var alterObjectArray = new Array();
  561. for (var i = 0; i < mySelection.length; i++) {
  562. // PageItem.position 获得物件群组左上角坐标
  563. var sel_xy = mySelection[i].position
  564. alterObjectArray.push(sel_xy);
  565. }
  566. // 删除用来定位的下层物件
  567. for (var i = 1; i < mySelection.length; i++) {
  568. mySelection[i].remove();
  569. }
  570. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  571. var newGroup = sourceObj.parent.groupItems.add();
  572. for (var i = 1; i < alterObjectArray.length; i++) {
  573. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  574. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  575. }
  576. sourceObj.remove();
  577. }
  578. }
  579. // 读取加载jsxbin文件,传递给AI软件
  580. function load_jsxbin(file) {
  581. var file = new File(file);
  582. if (file.open('r')) {
  583. var fileContent = file.read();
  584. file.close();
  585. buildMsg(fileContent);
  586. } else {
  587. alert('文件打开失败: ' + file);
  588. }
  589. }
  590. //========== 以下插件引用使用互联网各位大大的插件 =================//
  591. // 标注尺寸增强版 V2.1
  592. function make_size_plus() { load_jsxbin(IconsFolder + "/makesize.dat"); }
  593. // 自动群组
  594. function auto_group() { load_jsxbin(IconsFolder + "/autogroup.dat"); }
  595. // 调整尺寸
  596. function ResizeToSize() { load_jsxbin(IconsFolder + "/resize.dat"); }
  597. // 打包链接图片
  598. function img_pack_links() { load_jsxbin(IconsFolder + "/packlinks.dat"); }