LanyaAITools.jsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. #target illustrator
  2. #targetengine main
  3. var vs = "illustrator-" + app.version.substr(0, 2);
  4. // 实际代码建立 buildMsg(code) 函数传送代码
  5. function buildMsg(code) {
  6. try {
  7. var bt = new BridgeTalk;
  8. bt.target = vs;
  9. var msg = code;
  10. bt.body = msg;
  11. bt.send();
  12. } catch (e) { }
  13. }
  14. // 创建面板 使用 new Window("palette") ,需要 BridgeTalk, 它是 Adobe 应用程序之间进行通信的一种机制。
  15. // 它允许不同的 Adobe 应用程序在同一台计算机上进行交互和数据共享。
  16. // var bt = new BridgeTalk();
  17. // bt.target = "photoshop"; // 目标应用程序名称
  18. // bt.body = "alert('Hello from Illustrator!')"; // 要发送的消息或脚本
  19. // bt.send();
  20. icon_panel(); // main_panel();
  21. function main_panel() {
  22. var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.11.11");
  23. panel.alignChildren = ["left", "top"];
  24. panel.spacing = 2;
  25. panel.margins = 3;
  26. // 创建按钮组
  27. var BtGroup1 = panel.add("group");
  28. var BtGroup2 = panel.add("group");
  29. // 设置按钮组为水平布局
  30. BtGroup1.orientation = "row";
  31. BtGroup2.orientation = "row";
  32. // 设置按钮组的边缘间距
  33. BtGroup1.spacing = 2; // 调整按钮之间的间距
  34. BtGroup2.spacing = 2;
  35. // 添加按钮
  36. var button1 = BtGroup1.add("button", undefined, "标注尺寸");
  37. var button2 = BtGroup1.add("button", undefined, "批量旋转");
  38. var button3 = BtGroup1.add("button", undefined, "文件日期");
  39. var button4 = BtGroup1.add("button", undefined, "尺寸取整");
  40. var button5 = BtGroup2.add("button", undefined, "拼版左上对齐");
  41. var button6 = BtGroup2.add("button", undefined, "物件尺寸大小");
  42. var button7 = BtGroup2.add("button", undefined, "物件轮廓边界");
  43. var button8 = BtGroup2.add("button", undefined, "▲");
  44. button8.preferredSize = [26, 26];
  45. button1.helpTip = "标注尺寸, <Alt>增强标注";
  46. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  47. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  48. // 按钮点击事件处理程序
  49. button1.onClick = function () {
  50. if (ScriptUI.environment.keyboardState.altKey) {
  51. // Alt 键被按下
  52. make_size_plus();
  53. } else {
  54. buildMsg("make_size();");
  55. }
  56. };
  57. button2.onClick = function () {
  58. if (ScriptUI.environment.keyboardState.ctrlKey) {
  59. // Ctrl 加鼠标左键,自定义数字
  60. var input = prompt("请输入角度数字:", "45");
  61. if (!isNaN(parseFloat(input)))
  62. buildMsg("shapes_rotate(" + input + ");");
  63. } else if (ScriptUI.environment.keyboardState.altKey) {
  64. buildMsg("shapes_rotate(180);");
  65. } else {
  66. buildMsg("shapes_rotate(90);");
  67. }
  68. };
  69. button3.onClick = function () {
  70. if (ScriptUI.environment.keyboardState.altKey) {
  71. buildMsg("mark_5mm();");
  72. } else {
  73. buildMsg("filename_date();");
  74. }
  75. };
  76. button4.onClick = function () {
  77. buildMsg("size_to_integer();");
  78. };
  79. button5.onClick = function () {
  80. buildMsg("replace_align_position();");
  81. };
  82. button6.onClick = function () {
  83. if (ScriptUI.environment.keyboardState.altKey) {
  84. // Alt 键被按下
  85. alert("鼠标点击时按下了 Alt 键!");
  86. } else {
  87. // 没有按下 Alt 键
  88. alert("鼠标点击!");
  89. }
  90. };
  91. button7.onClick = function () {
  92. if (ScriptUI.environment.keyboardState.altKey) {
  93. buildMsg("size_by_controlBounds();");
  94. } else {
  95. buildMsg("size_by_width_height();");
  96. }
  97. };
  98. button8.onClick = function () {
  99. icon_panel();
  100. panel.close();
  101. };
  102. // 显示面板
  103. panel.show();
  104. }
  105. function icon_panel() {
  106. var panel = new Window("palette", "©蘭雅 Adobe Illustrator 工具箱");
  107. panel.alignChildren = ["left", "top"];
  108. panel.spacing = 2;
  109. panel.margins = 3;
  110. // 创建按钮组
  111. var BtGroup1 = panel.add("group");
  112. var BtGroup2 = panel.add("group");
  113. // 设置按钮组为水平布局
  114. BtGroup1.orientation = "row";
  115. BtGroup2.orientation = "row";
  116. // 设置按钮组的边缘间距
  117. BtGroup1.spacing = 2; // 调整按钮之间的间距
  118. BtGroup2.spacing = 2;
  119. // scriptFile = new File($.fileName); // 获取当前脚本文件的路径
  120. // var iconFile = new File(scriptFile.path + "/icon/icon.png"); // 拼接图标文件的完整路径
  121. var iconF1 = "c:/TSP/icon/size.png";
  122. var iconF2 = "c:/TSP/icon/icon.png";
  123. var iconF3 = "c:/TSP/icon/mark.png";
  124. var iconF4 = "c:/TSP/icon/debug.png";
  125. var iconF5 = "c:/TSP/icon/replace.png";
  126. var iconF6 = "c:/TSP/icon/gpucard.png";
  127. var iconF7 = "c:/TSP/icon/byBounds.png";
  128. var iconF8 = "c:/TSP/icon/repeat.png";
  129. // 添加图标按钮
  130. var button1 = BtGroup1.add("iconbutton", undefined, iconF1);
  131. var button2 = BtGroup1.add("iconbutton", undefined, iconF2);
  132. var button3 = BtGroup1.add("iconbutton", undefined, iconF3);
  133. var button4 = BtGroup1.add("iconbutton", undefined, iconF4);
  134. var button5 = BtGroup2.add("iconbutton", undefined, iconF5);
  135. var button6 = BtGroup2.add("iconbutton", undefined, iconF6);
  136. var button7 = BtGroup2.add("iconbutton", undefined, iconF7);
  137. var button8 = BtGroup2.add("iconbutton", undefined, iconF8);
  138. button1.helpTip = "标注尺寸, <Alt>增强标注";
  139. button2.helpTip = "批量左转90度,<Alt>转180度, <Ctrl>任意角度";
  140. button3.helpTip = "咬口处插入文件名日期,<Alt>红色备注文字";
  141. button4.helpTip = "尺寸取整, <Alt>统一大小";
  142. button5.helpTip = "快速替换, <Alt>";
  143. button6.helpTip = "暂时自动群组, <Alt>";
  144. // 设置按钮大小与图片大小相同
  145. button1.preferredSize = [48, 48];
  146. button2.preferredSize = [48, 48];
  147. button3.preferredSize = [48, 48];
  148. button4.preferredSize = [48, 48];
  149. button5.preferredSize = [48, 48];
  150. button6.preferredSize = [48, 48];
  151. button7.preferredSize = [48, 48];
  152. button8.preferredSize = [48, 48];
  153. // 按钮点击事件处理程序
  154. button1.onClick = function () {
  155. if (ScriptUI.environment.keyboardState.altKey) {
  156. // Alt 键被按下
  157. make_size_plus();
  158. } else {
  159. buildMsg("make_size();");
  160. }
  161. };
  162. button2.onClick = function () {
  163. if (ScriptUI.environment.keyboardState.ctrlKey) {
  164. // Ctrl 加鼠标左键,自定义数字
  165. var input = prompt("请输入角度数字:", "45");
  166. if (!isNaN(parseFloat(input)))
  167. buildMsg("shapes_rotate(" + input + ");");
  168. } else if (ScriptUI.environment.keyboardState.altKey) {
  169. buildMsg("shapes_rotate(180);");
  170. } else {
  171. buildMsg("shapes_rotate(90);");
  172. }
  173. };
  174. button3.onClick = function () {
  175. if (ScriptUI.environment.keyboardState.altKey) {
  176. buildMsg("mark_5mm();");
  177. } else {
  178. buildMsg("filename_date();");
  179. }
  180. };
  181. button4.onClick = function () {
  182. if (ScriptUI.environment.keyboardState.ctrlKey) {
  183. buildMsg("modify_size(-1, -1);");
  184. } else if (ScriptUI.environment.keyboardState.altKey) {
  185. buildMsg("modify_size(1, 1);");
  186. } else {
  187. buildMsg("size_to_integer();");
  188. }
  189. };
  190. button5.onClick = function () {
  191. buildMsg("replace_align_position();");
  192. };
  193. button6.onClick = function () {
  194. if (ScriptUI.environment.keyboardState.altKey) {
  195. ResizeToSize();
  196. } else {
  197. auto_group();
  198. }
  199. };
  200. button7.onClick = function () {
  201. if (ScriptUI.environment.keyboardState.altKey) {
  202. buildMsg("size_by_controlBounds();");
  203. } else {
  204. buildMsg("size_by_width_height();");
  205. }
  206. };
  207. button8.onClick = function () {
  208. if (ScriptUI.environment.keyboardState.altKey) {
  209. mini_panel();
  210. panel.close();
  211. } else {
  212. main_panel();
  213. panel.close();
  214. }
  215. };
  216. // 显示面板
  217. panel.show();
  218. }
  219. function mini_panel() {
  220. var panel = new Window("palette", "©蘭");
  221. panel.spacing = 0;
  222. panel.margins = 0;
  223. var icon = "c:/TSP/icon/repeat.png";
  224. var button_mini = panel.add("iconbutton", undefined, icon);
  225. button_mini.preferredSize = [40, 40];
  226. button_mini.onClick = function () {
  227. icon_panel();
  228. panel.close();
  229. };
  230. panel.show();
  231. }
  232. //==================================================================================//
  233. // 蘭雅 Adobe Illustrator 工具箱© 2023.11.11 各个按钮功能模块
  234. //==================================================================================//
  235. // 标注尺寸
  236. function make_size() {
  237. // 定义当前激活文档
  238. var docRef = activeDocument;
  239. var mm = 25.4 / 72; // pt 和 mm 转换系数
  240. var myFont = textFonts.getByName("MicrosoftYaHei");
  241. var myFontSize = 24;
  242. var x, y;
  243. // 格式化尺寸为 mm 取整数
  244. function formatSize(size) {
  245. return Math.round(size * mm).toFixed(0);
  246. }
  247. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  248. var cmykRed = new CMYKColor();
  249. cmykRed.cyan = 0;
  250. cmykRed.magenta = 100;
  251. cmykRed.yellow = 100;
  252. cmykRed.black = 0;
  253. function writeText(text) {
  254. var textRef = docRef.textFrames.add(); // 建立文本
  255. textRef.contents = text;
  256. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  257. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  258. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  259. textRef.top = y + 15 / mm;
  260. textRef.left = x + 10 / mm;
  261. }
  262. // 遍历选择的物件标注尺寸
  263. if (docRef.selection.length > 0) {
  264. var mySelection = docRef.selection;
  265. for (var i = 0; i < mySelection.length; i++) {
  266. var s = mySelection[i]
  267. x = s.left; y = s.top
  268. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  269. writeText(str)
  270. }
  271. }
  272. }
  273. // 文件名日期
  274. function filename_date() {
  275. // 获取当前时间
  276. function getdate() {
  277. var d = new Date(), month = '' + (d.getMonth() + 1),
  278. day = '' + d.getDate(), year = d.getFullYear();
  279. if (month.length < 2) month = '0' + month;
  280. if (day.length < 2) day = '0' + day;
  281. return [year, month, day].join('-');
  282. }
  283. // 获取 AI文档名称
  284. var docRef = activeDocument;
  285. var str = docRef.name;
  286. str = str + " " + getdate();
  287. // alert("本脚本建立一个文本:\n" + str);
  288. // 文档中建立一个新文本
  289. // var textRef = docRef.textFrames.add();
  290. // textRef.top = 100;
  291. // textRef.left = 200;
  292. // textRef.contents = str;
  293. var mm = 25.4 / 72; // pt 和 mm 转换系数
  294. var base = new Array();
  295. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  296. // alert("画板标尺原点mm x:" + base[0] * mm +" y:" + base[1] * mm + "\n画板大小mm 宽:" + docRef.width * mm +" 高:" + docRef.height * mm);
  297. var pw = 0;
  298. var ph = 0;
  299. var x = base[0]; // 画板左下角 x 坐标
  300. var y = - base[1]; // 画板左下角 y 坐标
  301. var myFont = textFonts.getByName("MicrosoftYaHei");
  302. var myFontSize = 8;
  303. pw = docRef.width; // 文档宽
  304. ph = docRef.height; // 文档高
  305. x = pw / 2 - x; // 转换x坐标: 画板中下x
  306. function filenameDate() {
  307. var textRef = docRef.textFrames.add(); // 建立文本
  308. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  309. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  310. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  311. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  312. textRef.top = y + 7.4; // 画板底向上偏移
  313. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  314. }
  315. filenameDate();
  316. }
  317. // 借咬口5mm
  318. function mark_5mm() {
  319. // 获取 AI文档名称
  320. var docRef = activeDocument;
  321. var str = docRef.name;
  322. str = "借咬口5mm"
  323. var mm = 25.4 / 72; // pt 和 mm 转换系数
  324. var base = new Array();
  325. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  326. var pw = 0;
  327. var ph = 0;
  328. var x = base[0]; // 画板左下角 x 坐标
  329. var y = - base[1]; // 画板左下角 y 坐标
  330. var myFont = textFonts.getByName("MicrosoftYaHei");
  331. var myFontSize = 64;
  332. pw = docRef.width; // 文档宽
  333. ph = docRef.height; // 文档高
  334. x = pw / 2 - x; // 转换x坐标: 画板中下x
  335. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  336. var cmykRed = new CMYKColor();
  337. cmykRed.cyan = 0;
  338. cmykRed.magenta = 100;
  339. cmykRed.yellow = 100;
  340. cmykRed.black = 0;
  341. function writeText() {
  342. var textRef = docRef.textFrames.add(); // 建立文本
  343. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  344. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  345. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  346. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  347. textRef.top = y - 15; // 画板底向上偏移
  348. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  349. }
  350. writeText();
  351. }
  352. var mm = 25.4 / 72; // pt 和 mm 转换系数
  353. // 格式化尺寸为 mm 取整数
  354. function formatSize(size) {
  355. return Math.round(size * mm).toFixed(0);
  356. }
  357. // 批量增加减少尺寸
  358. function modify_size(x, y) {
  359. var sr = app.activeDocument.selection;
  360. for (var i = 0; i < sr.length; i++) {
  361. var s = sr[i];
  362. s.width = formatSize(s.width) / mm + x / mm
  363. s.height = formatSize(s.height) / mm + y / mm
  364. }
  365. }
  366. // 批量修改尺寸
  367. function set_size(x, y) {
  368. var sr = app.activeDocument.selection;
  369. for (var i = 0; i < sr.length; i++) {
  370. var s = sr[i];
  371. s.width = x / mm
  372. s.height = y / mm
  373. }
  374. }
  375. // 遍历选择的物件尺寸取整
  376. function size_to_integer() {
  377. var sr = app.activeDocument.selection;
  378. for (var i = 0; i < sr.length; i++) {
  379. var s = sr[i];
  380. s.width = formatSize(s.width) / mm
  381. s.height = formatSize(s.height) / mm
  382. }
  383. }
  384. // 批量物件旋转角度
  385. function shapes_rotate(angle) {
  386. var sr = app.activeDocument.selection;
  387. for (var i = 0; i < sr.length; i++)
  388. sr[i].rotate(angle);
  389. }
  390. // 物件轮廓边界
  391. function size_by_controlBounds() {
  392. var docRef = activeDocument;
  393. // 判断选择物件2个以上
  394. if (docRef.selection.length > 1) {
  395. // 定义选择物件
  396. mySelection = docRef.selection;
  397. // 最上层物件为替换源
  398. var sourceObj = docRef.selection[0];
  399. // 定义数组保存选择物件controlBounds
  400. var BoundsArray = new Array();
  401. for (var i = 0; i < mySelection.length; i++) {
  402. // PageItem.position 获得物件群组左上角坐标
  403. var sel_Bounds = mySelection[i].controlBounds;
  404. BoundsArray.push(sel_Bounds);
  405. }
  406. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  407. var newGroup = sourceObj.parent.groupItems.add();
  408. for (var i = 1; i < BoundsArray.length; i++) {
  409. var width = BoundsArray[i][2] - BoundsArray[i][0];
  410. var height = BoundsArray[i][1] - BoundsArray[i][3];
  411. sourceObj.width = width;
  412. sourceObj.height = height;
  413. // 移动源文件到目的物件左上角对齐
  414. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  415. sourceObj.position = sel_xy;
  416. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  417. }
  418. sourceObj.remove();
  419. }
  420. }
  421. // 物件尺寸大小
  422. function size_by_width_height() {
  423. var doc = activeDocument;
  424. // 判断选择物件2个以上
  425. if (doc.selection.length > 1) {
  426. // 定义选择物件
  427. src = doc.selection;
  428. var taget = doc.selection[0];
  429. // PageItem.position 获得物件群组左上角坐标
  430. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  431. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  432. var newGroup = taget.parent.groupItems.add();
  433. for (var i = 1; i < src.length; i++) {
  434. var sel_xy = src[i].position;
  435. taget.width = src[i].width;
  436. taget.height = src[i].height;
  437. taget.position = sel_xy;
  438. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  439. }
  440. taget.remove();
  441. }
  442. }
  443. // 拼版左上对齐
  444. function replace_align_position() {
  445. var docRef = activeDocument;
  446. // 判断选择物件2个以上
  447. if (docRef.selection.length > 1) {
  448. // 定义选择物件
  449. mySelection = docRef.selection;
  450. // 最上层物件为替换源
  451. var sourceObj = docRef.selection[0];
  452. // 定义数组用来保存选择物件的左上角坐标
  453. var alterObjectArray = new Array();
  454. for (var i = 0; i < mySelection.length; i++) {
  455. // PageItem.position 获得物件群组左上角坐标
  456. var sel_xy = mySelection[i].position
  457. alterObjectArray.push(sel_xy);
  458. }
  459. // 删除用来定位的下层物件
  460. for (var i = 1; i < mySelection.length; i++) {
  461. mySelection[i].remove();
  462. }
  463. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  464. var newGroup = sourceObj.parent.groupItems.add();
  465. for (var i = 1; i < alterObjectArray.length; i++) {
  466. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  467. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  468. }
  469. sourceObj.remove();
  470. }
  471. }
  472. // 读取加载jsxbin文件,传递给AI软件
  473. function load_jsxbin(file) {
  474. var file = new File(file);
  475. if (file.open('r')) {
  476. var fileContent = file.read();
  477. file.close();
  478. var swap1Message = fileContent;
  479. buildMsg(swap1Message);
  480. } else {
  481. alert('文件打开失败: ' + file);
  482. }
  483. }
  484. //========== 以下插件引用使用互联网各位大大的插件 =================//
  485. // 标注尺寸增强版 V2.1
  486. function make_size_plus() {
  487. load_jsxbin("c:/TSP/icon/makesize.dat");
  488. }
  489. // 自动群组
  490. function auto_group() {
  491. load_jsxbin("c:/TSP/icon/autogroup.dat");
  492. }
  493. // 调整尺寸
  494. function ResizeToSize() {
  495. load_jsxbin("c:/TSP/icon/resize.dat");
  496. }