LanyaAITools.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #target illustrator
  2. // 创建面板 初始版,使用 dialog
  3. var panel = new Window("dialog", "蘭雅 Adobe Illustrator 工具箱© 2023.11.11");
  4. // var panel = new Window("palette", "蘭雅 Adobe Illustrator 工具箱© 2023.11.11");
  5. with (panel) {
  6. // 创建按钮组
  7. var BtGroup1 = panel.add("group");
  8. var BtGroup2 = panel.add("group");
  9. // 设置按钮组为水平布局
  10. BtGroup1.orientation = "row";
  11. BtGroup2.orientation = "row";
  12. // 添加按钮
  13. var button1 = BtGroup1.add("button", undefined, "标注尺寸");
  14. var button2 = BtGroup1.add("button", undefined, "文件名日期");
  15. var button3 = BtGroup1.add("button", undefined, "借咬口5mm");
  16. var button4 = BtGroup1.add("button", undefined, "尺寸取整");
  17. var button5 = BtGroup2.add("button", undefined, "拼版左上对齐");
  18. var button6 = BtGroup2.add("button", undefined, "物件尺寸大小");
  19. var button7 = BtGroup2.add("button", undefined, "物件轮廓边界");
  20. // 设置按钮组的边缘间距 设置按钮间距
  21. BtGroup1.spacing = 2; // 调整按钮之间的间距
  22. BtGroup2.spacing = 2;
  23. // 按钮点击事件处理程序
  24. button1.onClick = function () {
  25. make_size(); panel.close();
  26. };
  27. button2.onClick = function () {
  28. filename_date(); panel.close();
  29. };
  30. button3.onClick = function () {
  31. mark_5mm(); panel.close();
  32. };
  33. button4.onClick = function () {
  34. size_to_integer(); panel.close();
  35. };
  36. button5.onClick = function () {
  37. replace_align_position(); panel.close();
  38. };
  39. button6.onClick = function () {
  40. size_by_width_height(); panel.close();
  41. };
  42. button7.onClick = function () {
  43. size_by_controlBounds(); panel.close();
  44. };
  45. }
  46. // 显示面板
  47. panel.show();
  48. //==================================================================================//
  49. // 蘭雅 Adobe Illustrator 工具箱© 2023.11.11 各个按钮功能模块
  50. //==================================================================================//
  51. // 标注尺寸
  52. function make_size() {
  53. // 定义当前激活文档
  54. var docRef = activeDocument;
  55. var mm = 25.4 / 72; // pt 和 mm 转换系数
  56. var myFont = textFonts.getByName("MicrosoftYaHei");
  57. var myFontSize = 24;
  58. var x, y;
  59. // 格式化尺寸为 mm 取整数
  60. function formatSize(size) {
  61. return Math.round(size * mm).toFixed(0);
  62. }
  63. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  64. var cmykRed = new CMYKColor();
  65. cmykRed.cyan = 0;
  66. cmykRed.magenta = 100;
  67. cmykRed.yellow = 100;
  68. cmykRed.black = 0;
  69. function writeText(text) {
  70. var textRef = docRef.textFrames.add(); // 建立文本
  71. textRef.contents = text;
  72. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  73. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  74. textRef.textRange.characterAttributes.fillColor = cmykRed; // 设置颜色
  75. textRef.top = y + 15 / mm;
  76. textRef.left = x + 10 / mm;
  77. }
  78. // 遍历选择的物件标注尺寸
  79. if (docRef.selection.length > 0) {
  80. var mySelection = docRef.selection;
  81. for (var i = 0; i < mySelection.length; i++) {
  82. var s = mySelection[i]
  83. x = s.left; y = s.top
  84. var str = formatSize(s.width) + "x" + formatSize(s.height) + "mm";
  85. writeText(str)
  86. }
  87. }
  88. }
  89. // 文件名日期
  90. function filename_date() {
  91. // 获取当前时间
  92. function getdate() {
  93. var d = new Date(), month = '' + (d.getMonth() + 1),
  94. day = '' + d.getDate(), year = d.getFullYear();
  95. if (month.length < 2) month = '0' + month;
  96. if (day.length < 2) day = '0' + day;
  97. return [year, month, day].join('-');
  98. }
  99. // 获取 AI文档名称
  100. var docRef = activeDocument;
  101. var str = docRef.name;
  102. str = str + " " + getdate();
  103. // alert("本脚本建立一个文本:\n" + str);
  104. // 文档中建立一个新文本
  105. // var textRef = docRef.textFrames.add();
  106. // textRef.top = 100;
  107. // textRef.left = 200;
  108. // textRef.contents = str;
  109. var mm = 25.4 / 72; // pt 和 mm 转换系数
  110. var base = new Array();
  111. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  112. // alert("画板标尺原点mm x:" + base[0] * mm +" y:" + base[1] * mm + "\n画板大小mm 宽:" + docRef.width * mm +" 高:" + docRef.height * mm);
  113. var pw = 0;
  114. var ph = 0;
  115. var x = base[0]; // 画板左下角 x 坐标
  116. var y = - base[1]; // 画板左下角 y 坐标
  117. var myFont = textFonts.getByName("MicrosoftYaHei");
  118. var myFontSize = 8;
  119. pw = docRef.width; // 文档宽
  120. ph = docRef.height; // 文档高
  121. x = pw / 2 - x; // 转换x坐标: 画板中下x
  122. function filenameDate() {
  123. var textRef = docRef.textFrames.add(); // 建立文本
  124. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  125. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  126. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  127. textRef.textRange.characterAttributes.fillColor = docRef.swatches[1].color; // 设置拼版色
  128. textRef.top = y + 7.4; // 画板底向上偏移
  129. textRef.left = x - textRef.width - 10; // 画板x中,偏移文本宽和间隔宽
  130. }
  131. filenameDate();
  132. }
  133. // 借咬口5mm
  134. function mark_5mm() {
  135. // 获取 AI文档名称
  136. var docRef = activeDocument;
  137. var str = docRef.name;
  138. str = "借咬口5mm"
  139. var mm = 25.4 / 72; // pt 和 mm 转换系数
  140. var base = new Array();
  141. base = docRef.rulerOrigin; // 画板标尺原点,相对于画板的左上角
  142. var pw = 0;
  143. var ph = 0;
  144. var x = base[0]; // 画板左下角 x 坐标
  145. var y = - base[1]; // 画板左下角 y 坐标
  146. var myFont = textFonts.getByName("MicrosoftYaHei");
  147. var myFontSize = 64;
  148. pw = docRef.width; // 文档宽
  149. ph = docRef.height; // 文档高
  150. x = pw / 2 - x; // 转换x坐标: 画板中下x
  151. // 设置填充颜色为CMYK红色 (0, 100, 100, 0)
  152. var cmykRed = new CMYKColor();
  153. cmykRed.cyan = 0;
  154. cmykRed.magenta = 100;
  155. cmykRed.yellow = 100;
  156. cmykRed.black = 0;
  157. function writeText() {
  158. var textRef = docRef.textFrames.add(); // 建立文本
  159. textRef.contents = str; // 填充文本字符串: AI文档名称 + 时间
  160. textRef.textRange.characterAttributes.size = myFontSize; // 设置字体尺寸
  161. textRef.textRange.characterAttributes.textFont = myFont; // 设置字体名称
  162. textRef.textRange.characterAttributes.fillColor = cmykRed // docRef.swatches[4].color; // 从颜色版取色简单,但是结果不确定
  163. textRef.top = y - 15; // 画板底向上偏移
  164. textRef.left = x - textRef.width / 2; // 画板x中,偏移文本宽和间隔宽
  165. }
  166. writeText();
  167. }
  168. // 尺寸取整
  169. function size_to_integer() {
  170. // 定义当前激活文档
  171. var doc = activeDocument;
  172. var mm = 25.4 / 72; // pt 和 mm 转换系数
  173. // 格式化尺寸为 mm 取整数
  174. function formatSize(size) {
  175. return Math.round(size * mm).toFixed(0);
  176. }
  177. // 遍历选择的物件尺寸取整
  178. if (doc.selection.length > 0) {
  179. var src = doc.selection;
  180. for (var i = 0; i < src.length; i++) {
  181. var s = src[i]
  182. s.width = formatSize(s.width) / mm
  183. s.height = formatSize(s.height) / mm
  184. }
  185. }
  186. }
  187. // 物件轮廓边界
  188. function size_by_controlBounds() {
  189. var docRef = activeDocument;
  190. // 判断选择物件2个以上
  191. if (docRef.selection.length > 1) {
  192. // 定义选择物件
  193. mySelection = docRef.selection;
  194. // 最上层物件为替换源
  195. var sourceObj = docRef.selection[0];
  196. // 定义数组保存选择物件controlBounds
  197. var BoundsArray = new Array();
  198. for (var i = 0; i < mySelection.length; i++) {
  199. // PageItem.position 获得物件群组左上角坐标
  200. var sel_Bounds = mySelection[i].controlBounds;
  201. BoundsArray.push(sel_Bounds);
  202. }
  203. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  204. var newGroup = sourceObj.parent.groupItems.add();
  205. for (var i = 1; i < BoundsArray.length; i++) {
  206. var width = BoundsArray[i][2] - BoundsArray[i][0];
  207. var height = BoundsArray[i][1] - BoundsArray[i][3];
  208. sourceObj.width = width;
  209. sourceObj.height = height;
  210. // 移动源文件到目的物件左上角对齐
  211. var sel_xy = new Array(BoundsArray[i][0], BoundsArray[i][1]);
  212. sourceObj.position = sel_xy;
  213. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  214. }
  215. sourceObj.remove();
  216. }
  217. }
  218. // 物件尺寸大小
  219. function size_by_width_height() {
  220. var doc = activeDocument;
  221. // 判断选择物件2个以上
  222. if (doc.selection.length > 1) {
  223. // 定义选择物件
  224. src = doc.selection;
  225. var taget = doc.selection[0];
  226. // PageItem.position 获得物件群组左上角坐标
  227. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  228. // 修改taget大小, 移动到src物件左上角对齐, 复制副本
  229. var newGroup = taget.parent.groupItems.add();
  230. for (var i = 1; i < src.length; i++) {
  231. var sel_xy = src[i].position;
  232. taget.width = src[i].width;
  233. taget.height = src[i].height;
  234. taget.position = sel_xy;
  235. taget.duplicate(newGroup, ElementPlacement.PLACEATEND);
  236. }
  237. taget.remove();
  238. }
  239. }
  240. // 拼版左上对齐
  241. function replace_align_position() {
  242. var docRef = activeDocument;
  243. // 判断选择物件2个以上
  244. if (docRef.selection.length > 1) {
  245. // 定义选择物件
  246. mySelection = docRef.selection;
  247. // 最上层物件为替换源
  248. var sourceObj = docRef.selection[0];
  249. // 定义数组用来保存选择物件的左上角坐标
  250. var alterObjectArray = new Array();
  251. for (var i = 0; i < mySelection.length; i++) {
  252. // PageItem.position 获得物件群组左上角坐标
  253. var sel_xy = mySelection[i].position
  254. alterObjectArray.push(sel_xy);
  255. }
  256. // 删除用来定位的下层物件
  257. for (var i = 1; i < mySelection.length; i++) {
  258. mySelection[i].remove();
  259. }
  260. // PageItem.duplicate 复制对象, 需要一个相对对象定位
  261. var newGroup = sourceObj.parent.groupItems.add();
  262. for (var i = 1; i < alterObjectArray.length; i++) {
  263. sourceObj.position = alterObjectArray[i]; // 设置替换物的左上角位置,达到替换目的
  264. sourceObj.duplicate(newGroup, ElementPlacement.PLACEATEND);
  265. }
  266. sourceObj.remove();
  267. }
  268. }