LanyaAITools.jsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 make_size() {
  322. // 定义当前激活文档
  323. var docRef = activeDocument;
  324. var mm = 25.4 / 72; // pt 和 mm 转换系数
  325. var myFont = textFonts.getByName("MicrosoftYaHei");
  326. var myFontSize = 24;
  327. var x, y;
  328. // 格式化尺寸为 mm 取整数
  329. function formatSize(size) {
  330. return Math.round(size * mm).toFixed(0);
  331. }
  332. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  333. var cmykRed = new CMYKColor();
  334. cmykRed.cyan = 0;
  335. cmykRed.magenta = 100;
  336. cmykRed.yellow = 100;
  337. cmykRed.black = 0;
  338. function writeText(text) {
  339. var textRef = docRef.textFrames.add(); // 建立文本
  340. textRef.contents = text;
  341. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  342. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  343. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  344. textRef.top = y + 15 / mm;
  345. textRef.left = x + 10 / mm;
  346. }
  347. // 遍历选择的物件标注尺寸
  348. if (docRef.selection.length > 0) {
  349. var mySelection = docRef.selection;
  350. for (var i = 0; i < mySelection.length; i++) {
  351. var s = mySelection[i]
  352. x = s.left; y = s.top
  353. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  354. writeText(str)
  355. }
  356. }
  357. }
  358. // 文件名日期
  359. function filename_date() {
  360. // 获取当前时间
  361. function getdate() {
  362. var d = new Date(), month = '' + (d.getMonth() + 1),
  363. day = '' + d.getDate(), year = d.getFullYear();
  364. if (month.length < 2) month = '0' + month;
  365. if (day.length < 2) day = '0' + day;
  366. return [year, month, day].join('-');
  367. }
  368. // 获取 AI文档名称
  369. var docRef = activeDocument;
  370. var str = docRef.name;
  371. str = str + " " + getdate();
  372. var base = new Array();
  373. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  374. // alert("画板标尺原点mm x:" + base[0] * mm +" y:" + base[1] * mm + "\n画板大小mm 宽:" + docRef.width * mm +" 高:" + docRef.height * mm);
  375. var pw = 0;
  376. var ph = 0;
  377. var x = base[0]; // 画板左下角 x 坐标
  378. var y = - base[1]; // 画板左下角 y 坐标
  379. var myFont = textFonts.getByName("MicrosoftYaHei");
  380. var myFontSize = 8;
  381. pw = docRef.width; // 文档宽
  382. ph = docRef.height; // 文档高
  383. x = pw / 2 - x; // 转换x坐标: 画板中下x
  384. function filenameDate() {
  385. var textRef = docRef.textFrames.add(); // 建立文本
  386. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  387. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  388. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  389. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  390. textRef.top = y + 7.4; // 画板底向上偏移
  391. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  392. textRef.selected = true;
  393. }
  394. filenameDate();
  395. }
  396. // 借咬口5mm
  397. function mark_5mm() {
  398. // 获取 AI文档名称
  399. var docRef = activeDocument;
  400. var str = docRef.name;
  401. str = "借咬口5mm"
  402. var base = new Array();
  403. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  404. var pw = 0;
  405. var ph = 0;
  406. var x = base[0]; // 画板左下角 x 坐标
  407. var y = - base[1]; // 画板左下角 y 坐标
  408. var myFont = textFonts.getByName("MicrosoftYaHei");
  409. var myFontSize = 64;
  410. pw = docRef.width; // 文档宽
  411. ph = docRef.height; // 文档高
  412. x = pw / 2 - x; // 转换x坐标: 画板中下x
  413. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  414. var cmykRed = new CMYKColor();
  415. cmykRed.cyan = 0;
  416. cmykRed.magenta = 100;
  417. cmykRed.yellow = 100;
  418. cmykRed.black = 0;
  419. function writeText() {
  420. var textRef = docRef.textFrames.add(); // 建立文本
  421. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  422. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  423. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  424. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  425. textRef.top = y - 15; // 画板底向上偏移
  426. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  427. textRef.selected = true;
  428. }
  429. writeText();
  430. }
  431. // 批量修改尺寸
  432. function set_size(x, y) {
  433. var sr = app.activeDocument.selection;
  434. for (var i = 0; i < sr.length; i++) {
  435. var s = sr[i];
  436. var scale_x = x / mm / s.width * 100;
  437. var scale_y = y / mm / s.height * 100;
  438. // X, Y, Positions, FillPatterns, FillGradients, StrokePattern, LineWidths
  439. s.resize(scale_x, scale_y, true, true, true, true, 100);
  440. }
  441. }
  442. // 批量增加减少尺寸
  443. function modify_size(x, y) {
  444. var sr = app.activeDocument.selection;
  445. for (var i = 0; i < sr.length; i++) {
  446. var s = sr[i];
  447. var scale_x = (formatSize(s.width) / mm + x / mm) / s.width * 100;
  448. var scale_y = (formatSize(s.height) / mm + y / mm) / s.height * 100;
  449. s.resize(scale_x, scale_y);
  450. }
  451. }
  452. // 遍历选择的物件尺寸取整
  453. function size_to_integer() {
  454. var sr = app.activeDocument.selection;
  455. for (var i = 0; i < sr.length; i++) {
  456. var s = sr[i];
  457. var scale_x = formatSize(s.width) / mm / s.width * 100;
  458. var scale_y = formatSize(s.height) / mm / s.height * 100;
  459. s.resize(scale_x, scale_y);
  460. }
  461. }
  462. // 批量物件旋转角度
  463. function shapes_rotate(angle) {
  464. var sr = app.activeDocument.selection;
  465. for (var i = 0; i < sr.length; i++)
  466. sr[i].rotate(angle);
  467. }
  468. // 物件轮廓边界
  469. function size_by_controlBounds() {
  470. var docRef = activeDocument;
  471. // 判断选择物件2个以上
  472. if (docRef.selection.length > 1) {
  473. // 定义选择物件
  474. mySelection = docRef.selection;
  475. // 最上层物件为替换源
  476. var sourceObj = docRef.selection[0];
  477. // 定义数组保存选择物件controlBounds
  478. var BoundsArray = new Array();
  479. for (var i = 0; i < mySelection.length; i++) {
  480. // PageItem.position 获得物件群组左上角坐标
  481. var sel_Bounds = mySelection[i].controlBounds;
  482. BoundsArray.push(sel_Bounds);
  483. }
  484. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  485. var newGroup = sourceObj.parent.groupItems.add();
  486. for (var i = 1; i < BoundsArray.length; i++) {
  487. var width = BoundsArray[i][2] - BoundsArray[i][0];
  488. var height = BoundsArray[i][1] - BoundsArray[i][3];
  489. sourceObj.width = width;
  490. sourceObj.height = height;
  491. // 移动源文件到目的物件左上角对齐
  492. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  493. sourceObj.position = sel_xy;
  494. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  495. }
  496. sourceObj.remove();
  497. }
  498. }
  499. // 物件尺寸大小
  500. function size_by_width_height() {
  501. var doc = activeDocument;
  502. // 判断选择物件2个以上
  503. if (doc.selection.length > 1) {
  504. // 定义选择物件
  505. src = doc.selection;
  506. var taget = doc.selection[0];
  507. // PageItem.position 获得物件群组左上角坐标
  508. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  509. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  510. var newGroup = taget.parent.groupItems.add();
  511. for (var i = 1; i < src.length; i++) {
  512. var sel_xy = src[i].position;
  513. taget.width = src[i].width;
  514. taget.height = src[i].height;
  515. taget.position = sel_xy;
  516. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  517. }
  518. taget.remove();
  519. }
  520. }
  521. // 拼版左上对齐
  522. function replace_align_position() {
  523. var docRef = activeDocument;
  524. // 判断选择物件2个以上
  525. if (docRef.selection.length > 1) {
  526. // 定义选择物件
  527. mySelection = docRef.selection;
  528. // 最上层物件为替换源
  529. var sourceObj = docRef.selection[0];
  530. // 定义数组用来保存选择物件的左上角坐标
  531. var alterObjectArray = new Array();
  532. for (var i = 0; i < mySelection.length; i++) {
  533. // PageItem.position 获得物件群组左上角坐标
  534. var sel_xy = mySelection[i].position
  535. alterObjectArray.push(sel_xy);
  536. }
  537. // 删除用来定位的下层物件
  538. for (var i = 1; i < mySelection.length; i++) {
  539. mySelection[i].remove();
  540. }
  541. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  542. var newGroup = sourceObj.parent.groupItems.add();
  543. for (var i = 1; i < alterObjectArray.length; i++) {
  544. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  545. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  546. }
  547. sourceObj.remove();
  548. }
  549. }
  550. // 读取加载jsxbin文件,传递给AI软件
  551. function load_jsxbin(file) {
  552. var file = new File(file);
  553. if (file.open('r')) {
  554. var fileContent = file.read();
  555. file.close();
  556. var swap1Message = fileContent;
  557. buildMsg(swap1Message);
  558. } else {
  559. alert('文件打开失败: ' + file);
  560. }
  561. }
  562. //========== 以下插件引用使用互联网各位大大的插件 =================//
  563. // 标注尺寸增强版 V2.1
  564. function make_size_plus() {
  565. load_jsxbin("c:/TSP/icon/makesize.dat");
  566. }
  567. // 自动群组
  568. function auto_group() {
  569. load_jsxbin("c:/TSP/icon/autogroup.dat");
  570. }
  571. // 调整尺寸
  572. function ResizeToSize() {
  573. load_jsxbin("c:/TSP/icon/resize.dat");
  574. }
  575. // 打包链接图片
  576. function img_pack_links() {
  577. load_jsxbin("c:/TSP/icon/packlinks.dat");
  578. }