LanyaAITools.jsx 26 KB

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