LanyaAITools.jsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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.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. main_panel();
  263. panel.close();
  264. } else {
  265. mini_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, 0, 0, 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 shapes_info() {
  380. var sr = app.activeDocument.selection;
  381. var str = "选择物件总数:" + sr.length + "\n";
  382. for (var i = 0; i < sr.length; i++) {
  383. var s = sr[i];
  384. var size = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  385. if (i < 5) str += "第" + (i + 1) + "个尺寸: " + size + "\n";
  386. }
  387. alert(str);
  388. }
  389. // 文件名日期
  390. function filename_date() {
  391. // 获取当前时间
  392. function getdate() {
  393. var d = new Date(), month = '' + (d.getMonth() + 1),
  394. day = '' + d.getDate(), year = d.getFullYear();
  395. if (month.length < 2) month = '0' + month;
  396. if (day.length < 2) day = '0' + day;
  397. return [year, month, day].join('-');
  398. }
  399. // 获取 AI文档名称
  400. var docRef = activeDocument;
  401. var str = docRef.name;
  402. str = str + " " + getdate();
  403. var base = new Array();
  404. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  405. // 默认使用文档页面作为范围
  406. var pw = docRef.width; // 文档宽
  407. var ph = docRef.height; // 文档高
  408. var x = base[0]; // 画板左下角 x 坐标
  409. var y = - base[1]; // 画板左下角 y 坐标
  410. x = pw / 2 - x; // 转换x坐标: 画板中下x
  411. // 如果选择物件,使用物件范围
  412. if (app.activeDocument.selection.length > 0) {
  413. var bounds = new Array();
  414. bounds = get_Sel_Bounds();
  415. x = (bounds[0] + bounds[2]) / 2;
  416. y = bounds[3];
  417. }
  418. var myFont = textFonts.getByName("MicrosoftYaHei");
  419. var myFontSize = 8;
  420. function writeText() {
  421. var textRef = docRef.textFrames.add(); // 建立文本
  422. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  423. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  424. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  425. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  426. textRef.top = y + 7.4; // 画板底向上偏移
  427. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  428. textRef.selected = true;
  429. }
  430. writeText();
  431. }
  432. // 借咬口5mm
  433. function mark_5mm() {
  434. // 获取 AI文档名称
  435. var docRef = activeDocument;
  436. var str = docRef.name;
  437. str = "借咬口5mm"
  438. var base = new Array();
  439. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  440. // 默认使用文档页面作为范围
  441. var x = base[0]; // 画板左下角 x 坐标
  442. var y = - base[1]; // 画板左下角 y 坐标
  443. x = docRef.width / 2 - x; // 转换x坐标: 画板中下x
  444. // 如果选择物件,使用物件范围
  445. if (app.activeDocument.selection.length > 0) {
  446. var bounds = new Array();
  447. bounds = get_Sel_Bounds();
  448. x = (bounds[0] + bounds[2]) / 2;
  449. y = bounds[3];
  450. }
  451. var myFont = textFonts.getByName("MicrosoftYaHei");
  452. var myFontSize = 144;
  453. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  454. var cmykRed = new CMYKColor();
  455. cmykRed.cyan = 0;
  456. cmykRed.magenta = 100;
  457. cmykRed.yellow = 100;
  458. cmykRed.black = 0;
  459. function writeText() {
  460. var textRef = docRef.textFrames.add(); // 建立文本
  461. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  462. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  463. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  464. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  465. textRef.top = y - 144;
  466. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  467. textRef.selected = true;
  468. }
  469. writeText();
  470. }
  471. // 批量修改尺寸
  472. function set_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 = x / mm / s.width * 100;
  477. var scale_y = y / mm / s.height * 100;
  478. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  479. s.resize(scale_x, scale_y, true, true, true, true, 100);
  480. }
  481. }
  482. // 批量增加减少尺寸
  483. function modify_size(x, y) {
  484. var sr = app.activeDocument.selection;
  485. for (var i = 0; i < sr.length; i++) {
  486. var s = sr[i];
  487. var scale_x = (formatSize(s.width) / mm + x / mm) / s.width * 100;
  488. var scale_y = (formatSize(s.height) / mm + y / mm) / s.height * 100;
  489. s.resize(scale_x, scale_y);
  490. }
  491. }
  492. // 遍历选择的物件尺寸取整
  493. function size_to_integer() {
  494. var sr = app.activeDocument.selection;
  495. for (var i = 0; i < sr.length; i++) {
  496. var s = sr[i];
  497. var scale_x = formatSize(s.width) / mm / s.width * 100;
  498. var scale_y = formatSize(s.height) / mm / s.height * 100;
  499. s.resize(scale_x, scale_y);
  500. }
  501. }
  502. // 批量物件旋转角度
  503. function shapes_rotate(angle) {
  504. var sr = app.activeDocument.selection;
  505. for (var i = 0; i < sr.length; i++)
  506. sr[i].rotate(angle);
  507. }
  508. // 物件轮廓边界
  509. function size_by_controlBounds() {
  510. var docRef = activeDocument;
  511. // 判断选择物件2个以上
  512. if (docRef.selection.length > 1) {
  513. // 定义选择物件
  514. mySelection = docRef.selection;
  515. // 最上层物件为替换源
  516. var sourceObj = docRef.selection[0];
  517. // 定义数组保存选择物件controlBounds
  518. var BoundsArray = new Array();
  519. for (var i = 0; i < mySelection.length; i++) {
  520. // PageItem.position 获得物件群组左上角坐标
  521. var sel_Bounds = mySelection[i].controlBounds;
  522. BoundsArray.push(sel_Bounds);
  523. }
  524. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  525. var newGroup = sourceObj.parent.groupItems.add();
  526. for (var i = 1; i < BoundsArray.length; i++) {
  527. var width = BoundsArray[i][2] - BoundsArray[i][0];
  528. var height = BoundsArray[i][1] - BoundsArray[i][3];
  529. sourceObj.width = width;
  530. sourceObj.height = height;
  531. // 移动源文件到目的物件左上角对齐
  532. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  533. sourceObj.position = sel_xy;
  534. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  535. }
  536. sourceObj.remove();
  537. }
  538. }
  539. // 物件尺寸大小
  540. function size_by_width_height() {
  541. var doc = activeDocument;
  542. // 判断选择物件2个以上
  543. if (doc.selection.length > 1) {
  544. // 定义选择物件
  545. src = doc.selection;
  546. var taget = doc.selection[0];
  547. // PageItem.position 获得物件群组左上角坐标
  548. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  549. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  550. var newGroup = taget.parent.groupItems.add();
  551. for (var i = 1; i < src.length; i++) {
  552. var sel_xy = src[i].position;
  553. taget.width = src[i].width;
  554. taget.height = src[i].height;
  555. taget.position = sel_xy;
  556. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  557. }
  558. taget.remove();
  559. }
  560. }
  561. // 拼版左上对齐
  562. function replace_align_position() {
  563. var docRef = activeDocument;
  564. // 判断选择物件2个以上
  565. if (docRef.selection.length > 1) {
  566. // 定义选择物件
  567. mySelection = docRef.selection;
  568. // 最上层物件为替换源
  569. var sourceObj = docRef.selection[0];
  570. // 定义数组用来保存选择物件的左上角坐标
  571. var alterObjectArray = new Array();
  572. for (var i = 0; i < mySelection.length; i++) {
  573. // PageItem.position 获得物件群组左上角坐标
  574. var sel_xy = mySelection[i].position
  575. alterObjectArray.push(sel_xy);
  576. }
  577. // 删除用来定位的下层物件
  578. for (var i = 1; i < mySelection.length; i++) {
  579. mySelection[i].remove();
  580. }
  581. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  582. var newGroup = sourceObj.parent.groupItems.add();
  583. for (var i = 1; i < alterObjectArray.length; i++) {
  584. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  585. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  586. }
  587. sourceObj.remove();
  588. }
  589. }
  590. // 读取加载jsxbin文件,传递给AI软件
  591. function load_jsxbin(file) {
  592. var file = new File(file);
  593. if (file.open('r')) {
  594. var fileContent = file.read();
  595. file.close();
  596. buildMsg(fileContent);
  597. } else {
  598. alert('文件打开失败: ' + file);
  599. }
  600. }
  601. //========== 以下插件引用使用互联网各位大大的插件 =================//
  602. // 标注尺寸增强版 V2.1
  603. function make_size_plus() { load_jsxbin(IconsFolder + "/makesize.dat"); }
  604. // 自动群组
  605. function auto_group() { load_jsxbin(IconsFolder + "/autogroup.dat"); }
  606. // 调整尺寸
  607. function ResizeToSize() { load_jsxbin(IconsFolder + "/resize.dat"); }
  608. // 打包链接图片
  609. function img_pack_links() { load_jsxbin(IconsFolder + "/packlinks.dat"); }