Guest User

Untitled

a guest
Dec 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #target illustrator
  2.  
  3. // 選択されているテキストオブジェクトの内容を正規表現で検索して、
  4. // 一致した箇所に指定した文字スタイルを割り当てます。
  5.  
  6. (function(){
  7. const SCRIPTNAME = "正規表現で文字スタイル割り当て";
  8.  
  9. var _opt = {
  10. ignore_case : true,
  11. clearing_overrides : true,
  12. styles : [],
  13. input_field_width : 16,
  14. rex : "",
  15. character_style : "",
  16. text_frames : []
  17. }
  18.  
  19. function showDialog(){
  20. getCharacterStyleNames();
  21.  
  22. _opt.text_frames = [];
  23. getTextFramesFromSelection(_opt.text_frames);
  24. if(_opt.text_frames.length < 1){
  25. showError("テキストオブジェクトが選択されていません。");
  26. return;
  27. }
  28.  
  29. if(app.documents.length < 1) return;
  30. var adoc = app.activeDocument;
  31.  
  32. var win = new Window("dialog", SCRIPTNAME);
  33. win.alignChildren = "left";
  34.  
  35. win.add("statictext", undefined, "検索文字列(正規表現)");
  36. win.etext1 = win.add("edittext", undefined, "");
  37. win.etext1.characters = _opt.input_field_width;
  38.  
  39. win.add("statictext", undefined, "適用する文字スタイル");
  40. win.list1 = win.add("dropdownlist", undefined, _opt.styles);
  41. win.list1.selection = 0;
  42.  
  43. win.check0 = win.add("checkbox", undefined, "大文字小文字を同一視");
  44. win.check0.value = _opt.ignore_case;
  45.  
  46. win.check1 = win.add("checkbox", undefined, "オーバーライド解除");
  47. win.check1.value = _opt.clearing_overrides;
  48.  
  49. win.buttons = win.add("group");
  50. win.buttons.alignment = "center";
  51. win.buttons.ok = win.buttons.add("button", undefined, "実行");
  52. win.buttons.cancel = win.buttons.add("button", undefined, "キャンセル");
  53.  
  54. var isValuesOK = function(){
  55. _opt.rex = win.etext1.text;
  56. if(_opt.rex == ""){
  57. showError("検索文字列(正規表現)を指定してください。");
  58. return false;
  59. }
  60.  
  61. var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
  62. if(style_name == ""){
  63. showError("適用する文字スタイルを選択してください。");
  64. return false;
  65. }
  66. _opt.character_style = app.activeDocument.characterStyles.getByName(style_name);
  67.  
  68. _opt.ignore_case = win.check0.value;
  69. _opt.clearing_overrides = win.check1.value;
  70. return true;
  71. }
  72.  
  73. win.buttons.ok.onClick = function(){
  74. try{
  75. win.enabled = false;
  76. if(isValuesOK() == false) return;
  77. if(main() == false) return;
  78. } catch(e) {
  79. alert(e);
  80. } finally {
  81. win.enabled = true;
  82. }
  83. win.close();
  84. }
  85.  
  86. win.buttons.cancel.onClick = function(){
  87. win.close();
  88. }
  89.  
  90. win.show();
  91. }
  92.  
  93. function getCharacterStyleNames(){
  94. var styles = app.activeDocument.characterStyles;
  95. _opt.styles = [];
  96. for(var i = 0; i < styles.length; i++){
  97. _opt.styles.push(styles[i].name);
  98. }
  99. }
  100.  
  101. function main(){
  102.  
  103. // regex_changeContentsOfWordOrString_RemainFormatting.jsx
  104. // regards pixxxel schubser
  105.  
  106. var result, aCon;
  107. var regex_option = _opt.ignore_case ? "gmi" : "gm";
  108.  
  109. for(var i = _opt.text_frames.length - 1; i >= 0; i--){
  110. var atf = _opt.text_frames[i];
  111.  
  112. var s = new RegExp(_opt.rex, regex_option);
  113.  
  114. while ( result = s.exec(atf.contents)) {
  115.  
  116. try {
  117.  
  118. aCon = atf.characters[result.index];
  119.  
  120. aCon.length = result[0].length;
  121.  
  122. _opt.character_style.applyTo(aCon, _opt.clearing_overrides);
  123.  
  124. } catch (e) {};
  125. }
  126.  
  127. }
  128. }
  129.  
  130. function getTextFramesFromSelection(textFrames, items){
  131. if(!items) items = app.activeDocument.selection;
  132. for(var i = 0; i < items.length; i++){
  133. if(items[i].locked || items[i].hidden){
  134. continue;
  135. } else if(items[i].typename == "TextFrame"){
  136. textFrames.push(items[i]);
  137. } else if(items[i].typename == "GroupItem"){
  138. getTextFramesFromSelection(textFrames, items[i].pageItems);
  139. }
  140. }
  141. }
  142.  
  143. function showError(msg){
  144. alert(SCRIPTNAME + "\rERROR :\r"
  145. + msg);
  146. }
  147.  
  148. showDialog();
  149. })();
Add Comment
Please, Sign In to add comment