LanyaAITools.jsx 29 KB

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