11月 16, 2011
inomoto

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

InDesignCS4:全てのリンクをLinksフォルダの同名のファイルに置換

 InDesignのリンク画像は、絶対パスで管理されているらしく、親フォルダの名称を変更したり、別フォルダに移動したりすると、次にドキュメントを開いたときにリンクが切れてしまっている場合が多いです。
 リンクパレットから再リンクボタンをoptionクリックしてフォルダを選択し……とすると再リンクはできるのですが、ドキュメントの数が多いとそれも面倒なのでこのスクリプトを作成しました。

 ドキュメントがパッケージされており、最新の画像データがドキュメントと同一階層の「Links」フォルダにあることを前提にしています。
 ドキュメントを開き、このスクリプトを実行すると、全てのリンクをLinksフォルダの同名のファイルに再リンクします。

スクリプト名:全てのリンクをLinksフォルダの同名のファイルに置換.scpt

tell
application “Adobe InDesign CS4″
  tell document 1
    set myPath to file path as Unicode text
    set
linkPath to myPath & “Links:” as Unicode text
    
    set
allLinks to object reference of every link
    repeat with
aLink in allLinks
      tell aLink
        set filePath to file path as Unicode text
        set
itemName to item -1 of textToList(filePath, “:”) of me
        set
newLink to linkPath & itemName as Unicode text
        set
newLink to newLink as alias
        
relink aLink to newLink
      end tell
    end repeat
  end tell
end tell

—————————————————–
テキストをデリミタでバラしてリストにして返すルーチン
—————————————————–
on
textToList(aText, aDelim)
  set retList to {}
  set
oldDelim to AppleScript‘s text item delimiters
  set
AppleScript‘s text item delimiters to aDelim
  set
retList to every text item of aText
  set
AppleScript‘s text item delimiters to oldDelim
  return
retList
end textToList

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

Excel2008:タブ区切りテキストを全列文字列フォーマットで開いてxlsで保存

 UTF16BEのテキストを前提にしています。スクリプトを実行するとダイアログでファイルの選択を求められます。複数選択可能です。
 選択したファイルを全て、全列文字列フォーマットで開いて、テキストファイルと同じ場所にxls形式で保存します。saveAsXLSX内の、「–xlsx形式」のコメントがついている行をアンコメントするとxlsx形式で保存します。その際は「–xls形式」のコメントがついている行はコメントアウトしてください。

スクリプト名:タブ区切りテキストを全列文字列フォーマットで開いてxlsで保存.scpt
set tgtFiles to choose file with prompt “Excel形式で保存するテキストファイルを複数選択” with multiple selections allowed

repeat with tgtFile in tgtFiles
  set tgtFile to tgtFile as Unicode text
  
openTabTxt(tgtFile) of openTabTxtKitタブ区切りテキストを全列文字列フォーマットで開く
  
saveAsXLSX(tgtFile) of saveAsXLSXKitactive workbookを指定のフォルダに保存
end repeat

—————————————————–
タブ区切りテキストを全列文字列フォーマットで開く
—————————————————–
script
openTabTxtKit
  
  –
タブ区切りテキストを全列文字列フォーマットで開く
  on
openTabTxt(tgtFile)
    tell application “Microsoft Excel”
      set aDelim to tab
      set
aFormat to text format
      
      set
cntLabel to getCountLabel(tgtFile, aDelim) of me –テキストの一行目から,列見出しの数を取得
      set
fieldInfo to getFieldInfo(cntLabel, aFormat) of me –field infoを取得(全列text format)
      
      (*

      open text fileのパラメータ:
       data type delimited:区切り記号付き
       text qualifier text qualifier double quote:文字列の引用府=ダブルクオーテーション
       tab true:区切り文字=タブ
      
*)
      
open text file filename tgtFile data type delimited text qualifier text qualifier double quote field info fieldInfo with tab
    end tell
  end openTabTxt
  
  –
field infoを取得(全列統一限定)
  on
getFieldInfo(cntLabel, aFormat)
    tell application “Microsoft Excel”
      set fieldInfo to {}
      repeat with
i from 1 to cntLabel
        set the end of fieldInfo to {i, aFormat}
      end repeat
      return
fieldInfo
    end tell
  end getFieldInfo
  
  –
テキストの一行目から,列見出しの数を取得
  on
getCountLabel(tgtFile, aDelim)
    set tgtFile to tgtFile as alias
    set
inTxt to readFile(tgtFile) of me
    set
aLabel to paragraph 1 of inTxt
    set
aLabelList to textToList(aLabel, aDelim) of me
    set
cntLabel to count of aLabelList
    return
cntLabel
  end getCountLabel
  
  –
テキストファイル読み込み(UTF16限定)
  on
readFile(tgtFile)
    set aTxt to read tgtFile as Unicode text
    set
aTxt to aTxt as Unicode text
    
log aTxt
    return
aTxt
  end readFile
  
  –
—————————————————–
  –
テキストをデリミタでバラしてリストにして返すルーチン
  –
—————————————————–
  on
textToList(aText, aDelim)
    set retList to {}
    set
oldDelim to AppleScript‘s text item delimiters
    set
AppleScript‘s text item delimiters to aDelim
    set
retList to every text item of aText
    set
AppleScript‘s text item delimiters to oldDelim
    return
retList
  end textToList
  
end script

—————————————————–
active workbookを指定のフォルダに保存
—————————————————–
script
saveAsXLSXKit
  
  on
saveAsXLSX(tgtFile)
    set tgtFol to getParentFol(tgtFile) of me –親フォルダを取得
    
    tell
application “Microsoft Excel”
      tell active workbook
        
        –
ファイル名から保存名を取得
        tell
active sheet
          set aName to name
          –
set aName to replaceText(aName, “.txt”, “.xlsx”) of me –xlsx形式
          set
aName to replaceText(aName, “.txt”, “.xls”) of me –xls形式
          set
saveTo to tgtFol & aName as Unicode text
        end tell
        
        –
xlsx形式で別名保存
        –
save as active sheet filename saveTo file format workbook normal file format –xlsx形式
        
save as active sheet filename saveTo file format Excel98to2004 file formatxls形式
      end tell
      
close active workbook
    end tell
  end saveAsXLSX
  
  on
getParentFol(tgtFile)
    set tgtFile to tgtFile as alias
    tell
application “Finder”
      set tgtFol to parent of tgtFile
      set
tgtFol to tgtFol as Unicode text
    end tell
    return
tgtFol
  end getParentFol
  
  –
—————————————————–
  –
文字置換ルーチン
  –
—————————————————–
  on
replaceText(origText, targetText, changeValue)
    set delim to AppleScript‘s text item delimiters
    set
AppleScript‘s text item delimiters to targetText
    set
str1 to text items of origText
    set
AppleScript‘s text item delimiters to changeValue
    set
str2 to str1 as string
    set
AppleScript‘s text item delimiters to delim
    return
str2
  end replaceText
  
end script

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

Leave a comment