Skip to content

Instantly share code, notes, and snippets.

@nbqx
Created October 20, 2010 04:40
Show Gist options
  • Save nbqx/635778 to your computer and use it in GitHub Desktop.
Save nbqx/635778 to your computer and use it in GitHub Desktop.
#target InDesign
//DTPっぽいことをスクリプティングするためのユーティリティ
DTP = this.DTP = {};
//TextFrameのなかを文字列検索してHitした結果を返す
//arguments: o => TextFrameオブジェクトなど, str => 検索する文字列(配列も可)
//戻るのはText, Word, Characterなど
/* ex.
var ret = DTP.filter(app.activeDocument.selection[0], "(");
var ret = DTP.filter(app.activeDocument.selection[0], ["(", ")"]);
*/
DTP.filter = function(o,str){
var ret = [];
var tgt = o;
var text = (str instanceof Array)? str : [str];
for(var i=0; i<text.length; i++){
app.findTextPreferences.findWhat = text[i];
var hits = tgt.findText();
for(var j=0; j<hits.length; j++){
ret.push(hits[j]);
}
app.findTextPreferences = NothingEnum.nothing;
}
return ret
}
//TextFrameのなかを文字列検索してHitしたものにfunctionを適用
//arguments: o => TextFrameオブジェクトなど(Paragraphとかでもいいよ), str => 検索する文字列, fn => ヒットしたものにあてるfunc
DTP.map = function(obj,str,fn){
var tgt = obj;
var text = str;
var func = function(o){return o};
if(fn){ func = fn; };
var hits = DTP.filter(tgt,text);
try{
for(var i=0; i<hits.length; i++){
var hit = hits[i];
func(hit);
}
}catch(e){
alert(e);
exit();
}
}
//コピペする関数
//arguments: src => コピーするオブジェクト(文字列でもオブジェクトでもなんでもいいよ), dest => はりつけるオブジェクト(Characterとか)
DTP.copyAndPaste = function(src,dest){
src.select();
app.copy();
dest.select();
app.paste();
}
//カットアンドペーストする関数
//arguments: copyAndPasteとおなじ
DTP.cutAndPaste = function(src,dest){
src.select();
app.cut();
dest.select();
app.paste();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment