Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shspage/82e70da926dc5c9bfdc591d1c4afb7d5 to your computer and use it in GitHub Desktop.
Save shspage/82e70da926dc5c9bfdc591d1c4afb7d5 to your computer and use it in GitHub Desktop.
@jamesw05さんの illustrator-shortcuts/apply_Style_With_RegExp.jsx のUIやメッセージを日本語にしたもの。
#target illustrator
// 選択されているテキストオブジェクトの内容を正規表現で検索して、
// 一致した箇所に指定した文字スタイルを割り当てます。
(function(){
const SCRIPTNAME = "正規表現で文字スタイル割り当て";
var _opt = {
ignore_case : true,
clearing_overrides : true,
styles : [],
input_field_width : 16,
rex : "",
character_style : "",
text_frames : []
}
function showDialog(){
getCharacterStyleNames();
_opt.text_frames = [];
getTextFramesFromSelection(_opt.text_frames);
if(_opt.text_frames.length < 1){
showError("テキストオブジェクトが選択されていません。");
return;
}
if(app.documents.length < 1) return;
var adoc = app.activeDocument;
var win = new Window("dialog", SCRIPTNAME);
win.alignChildren = "left";
win.add("statictext", undefined, "検索文字列(正規表現)");
win.etext1 = win.add("edittext", undefined, "");
win.etext1.characters = _opt.input_field_width;
win.add("statictext", undefined, "適用する文字スタイル");
win.list1 = win.add("dropdownlist", undefined, _opt.styles);
win.list1.selection = 0;
win.check0 = win.add("checkbox", undefined, "大文字小文字を同一視");
win.check0.value = _opt.ignore_case;
win.check1 = win.add("checkbox", undefined, "オーバーライド解除");
win.check1.value = _opt.clearing_overrides;
win.buttons = win.add("group");
win.buttons.alignment = "center";
win.buttons.ok = win.buttons.add("button", undefined, "実行");
win.buttons.cancel = win.buttons.add("button", undefined, "キャンセル");
var isValuesOK = function(){
_opt.rex = win.etext1.text;
if(_opt.rex == ""){
showError("検索文字列(正規表現)を指定してください。");
return false;
}
var style_name = win.list1.selection == null ? "" : win.list1.selection.text;
if(style_name == ""){
showError("適用する文字スタイルを選択してください。");
return false;
}
_opt.character_style = app.activeDocument.characterStyles.getByName(style_name);
_opt.ignore_case = win.check0.value;
_opt.clearing_overrides = win.check1.value;
return true;
}
win.buttons.ok.onClick = function(){
try{
win.enabled = false;
if(isValuesOK() == false) return;
if(main() == false) return;
} catch(e) {
alert(e);
} finally {
win.enabled = true;
}
win.close();
}
win.buttons.cancel.onClick = function(){
win.close();
}
win.show();
}
function getCharacterStyleNames(){
var styles = app.activeDocument.characterStyles;
_opt.styles = [];
for(var i = 0; i < styles.length; i++){
_opt.styles.push(styles[i].name);
}
}
function main(){
// regex_changeContentsOfWordOrString_RemainFormatting.jsx
// regards pixxxel schubser
var result, aCon;
var regex_option = _opt.ignore_case ? "gmi" : "gm";
for(var i = _opt.text_frames.length - 1; i >= 0; i--){
var atf = _opt.text_frames[i];
var s = new RegExp(_opt.rex, regex_option);
while ( result = s.exec(atf.contents)) {
try {
aCon = atf.characters[result.index];
aCon.length = result[0].length;
_opt.character_style.applyTo(aCon, _opt.clearing_overrides);
} catch (e) {};
}
}
}
function getTextFramesFromSelection(textFrames, items){
if(!items) items = app.activeDocument.selection;
for(var i = 0; i < items.length; i++){
if(items[i].locked || items[i].hidden){
continue;
} else if(items[i].typename == "TextFrame"){
textFrames.push(items[i]);
} else if(items[i].typename == "GroupItem"){
getTextFramesFromSelection(textFrames, items[i].pageItems);
}
}
}
function showError(msg){
alert(SCRIPTNAME + "\rERROR :\r"
+ msg);
}
showDialog();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment