6月 7, 2011
inomoto

【InDesignCS4】スクリプト2件ご紹介

Inomotoです.今日は,InDesign CS4向けのAppleScriptを2つ紹介します.

段落のtext style rangeを正しく取得

 InDesignCS4での不具合を発見しました.AppleScriptで,段落内のtext style rangeを取得しようとすると,次の段落の頭の方まで段落内と認識されてしまうという現象です.

 text style rangeのparentを再起的に取得していくと,paragraphではくstoryに行き着くので,おそらくそのあたりが関係しているのではないかと思われます.

 正しく段落内のみのtext style rangeを取得するようスクリプトを作成しました.paragraphと,text frame内のtext 1のobject  referenceを渡すとtext style rangeのリストを返します.

 あまり多くの条件でテストしていないので,エラーが出たりするケースがあると思います.そのときはコメントなどいただけると幸いです.

スクリプト名:段落のtext style rangeを正しく取得.scpt

tell
application “Adobe InDesign CS4″
  activate
  tell
document 1
    tell text frame 1
      set tgtTxt to object reference of text 1 –text frame内のtext全て
      set
allParas to object reference of every paragraph
      repeat with
aPara in allParas
        set tStyleRanges to getStyleRanges(aPara, tgtTxt) of me
        repeat with
aRange in tStyleRanges
          select aRange
          
delay 0.5
        end repeat
        
display dialog “paragraph end” giving up after 1
      end repeat
    end tell
  end tell
end tell

段落のtext style rangeを正しく取得
on
getStyleRanges(aPara, tgtTxt)
  set retRanges to {} –戻り値用の配列
  tell
application “Adobe InDesign CS4″
    tell document 1
      tell aPara
        set idxLast_Para to index of last character段落の最後の文字のインデックスを取得
        
        set
allRanges to object reference of every text style range
        repeat with
aRange in allRanges
          tell aRange
            set addRange to nothing
            set
idxFirst_Range to index of first characterrangeの最初の文字のインデックスを取得
            set
idxLast_Range to index of last characterrangeの最後の文字のインデックスを取得
            
            if
idxFirst_Range > idxLast_Para then –段落の最後よりrangeの最初が後ろの場合(全て段落の範囲外)
              –skip
            else if idxLast_Para < idxLast_Range then –段落の最後よりrangeの最後が後ろの場合(一部段落の範囲外)
              set aRange to object reference of text from character idxFirst_Range to character idxLast_Para of tgtTxtrangeを再設定(rangeの最初〜段落の最後)
              set
addRange to aRange
            else –それ以外の場合(全て段落の範囲内)
              set addRange to aRange
            end if
            
            if
addRange is not equal to nothing then
              set the end of retRanges to addRange戻り値用の配列に入れる
            end if
          end tell
        end repeat
        
      end tell
    end tell
  end tell
  return
retRanges
end getStyleRanges

▼新規書類にペースト  ▼カーソル位置にペースト  ▼ドキュメント末尾にペースト

ライブラリからアンカーオブジェクトを配置

あらかじめライブラリにassetを登録しておき,このスクリプトを実行すると,指定の場所にアンカーオブジェクトとしてassetを配置します.
このサンプルでは1ページ目の最前面のtext frameの一番始めのinsertion pointに配置します.

スクリプト名:ライブラリからアンカーオブジェクトを配置.scpt
set assetName to “xxxx” –ここにassetの名前を入れる

tell application “Adobe InDesign CS4″
  tell document 1
    set aAsset to getAsset(assetName) of me –指定名称のassetをライブラリから取得
    
    set
tgtPoint to insertion point 1 of text frame 1 of page 1
    set
placedAsset to placeAsset(tgtPoint, aAsset) of me
    
select placedAsset
  end tell
end tell

————————————–
指定の場所にassetを配置し,配置したassetを返す
————————————–
on
placeAsset(tgtObj, aAsset)
  tell application “Adobe InDesign CS4″
    tell document 1
      set tgtAsset to place asset aAsset on tgtObj
      return
item 1 of tgtAssetリストで取得されるので一つだけ返す
    end tell
  end tell
end placeAsset

————————————–
指定名称のassetをライブラリから取得
————————————–
on
getAsset(assetName)
  tell application “Adobe InDesign CS4″
    tell library 1
      set aAsset to object reference of asset assetName
      return
aAsset
    end tell
  end tell
end getAsset

▼新規書類にペースト  ▼カーソル位置にペースト  ▼ドキュメント末尾にペースト

Leave a comment