Skip to content

Instantly share code, notes, and snippets.

@sttk3
Created October 27, 2023 10:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sttk3/6f90afba6175014268c5415a1cf9bf7c to your computer and use it in GitHub Desktop.
Save sttk3/6f90afba6175014268c5415a1cf9bf7c to your computer and use it in GitHub Desktop.
選択しているテキストのフォントをIllustratorのtargetengineに記憶するスクリプト。pasteFont.jsxとセットで利用する
/**
* @file 選択しているテキストのフォントをtargetengineに記憶する。pasteFont.jsxとセットで利用する
* @version 1.0.0
* @author sttk3.com
* @copyright © 2023 sttk3.com
*/
//@target 'illustrator'
//@targetengine 'com.sttk3.attributes'
var globalTargetFont ;
(function() {
if(app.documents.length <= 0) {return ;}
var sel = getTextSelection() ;
if(sel.length <= 0) {return ;}
try {
// 1文字目のフォントへの参照を取得する
var appliedFont = sel[0].characters[0].textFont ;
// 取得に成功したらtargetengineに記憶する
if(appliedFont) {globalTargetFont = appliedFont ;}
} catch(e) {
return ;
}
})() ;
/**
* selectionを常にtextRangeのArrayとして返す
* @return {Array<TextRange>} 選択なしの場合は[]
*/
function getTextSelection() {
var sel = app.selection ;
var selLength = sel.length ;
var res = [] ;
switch(sel.constructor.name) {
case 'Array' :
if(!sel[0]) {return res ;}
for(var i = 0 ; i < selLength ; i++) {
var itemType = sel[i].constructor.name ;
if(itemType == 'TextFrame') {
res.push(sel[i].textRange) ;
} else if(itemType == 'TextRange') {
res.push(sel[i]) ;
}
}
break ;
case 'TextRange' :
res.push(sel) ;
break ;
}
return res ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment