LanyaAITools.jsx 29 KB

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