人気ブログランキング | 話題のタグを見る
「a.indd」を閉じて「xxxa.indd」にリネームせよ
仕事してると、どうしても同じ名前の InDesign ファイルが複数できる。直しが出た場合などに、間違ったファイルで作業して矛盾が出たりする。割と頻繁にある。この問題に対する定石対処があるのかも知れないが、いろんな作業手順を検討した結果、「一度パッケージしたら二度と元ファイルは開かない」「そのために一度パッケージした元ファイルは強制的にファイル名を変える」ことにした。すでに「デスクトップにPDFとパッケージフォルダを書き出してパッケージフォルダをZIP圧縮する」作業は AppleScript で自動化してるので、「(InDesignで「a.indd」というファイルを開いているときに)『a.indd』を閉じて『xxxa.indd』にリネームせよ」というスクリプトができれば、それに合体させられるはずだ。

とりあえず準備体操として、「『a.indd』を閉じて Finder でラベルの色を着けよ」という AppleScript に挑戦してみよう。
--test 1
tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell
tell application "Finder"
set label index of file doc_name of window 1 to 3
end tell
おお、機能するぞ。「a.indd」が「Finder window 1」にあるとは限らないという、けっこう重大な問題はあるけれど。

この書き方だとここまではあっさりできたみたいだが、実際はこの解に辿り着くのに1時間以上はかかった。また、こんな風に書くとまるで僕がAppleScriptの基本を理解してるように思われるかも知れないが、それも誤解だ。例えば「name of document 1」を何故カッコで挟む必要があるのか、その後ろに付いてる「as Unicode text」とはどういうことなのか、さっぱりわからないまま、継ぎはぎのコピペを繰り返しては試行錯誤の果てに、結果として機能したらオッケー、という、そんな感じ。まあ、逆に言うと、「save document 1」ぐらいはわかって書いてる。「document 1」は「最前面の書類」という意味。ちなみに Finder の最前面のウィンドウは「window 1」。

ちなみに数字とラベルの関係は、うどん県で働く Web ディレクターさんによると以下の通り。

0 ラベルなし
1 オレンジ
2 レッド
3 イエロー
4 ブルー
5 パープル
6 グリーン
7 グレイ

次はリネームだ。
--test 2
tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell
tell application "Finder"
set xxxdoc_name to "xxx" and doc_name
set name of file doc_name of window 1 to xxxdoc_name
end tell
ダメだ。「error "xxx" のタイプを boolean に変換できません」とか言われる。

以下のスクリプトをアプリケーション形式で保存すると「ドロップレット」ができ、リネームは成功する。何故だ。Finder に命じるなら「as string」、InDesignに命じるなら「as Unicode text」ということだろうか。
--test 3
on open ObjList
tell application "Finder"
repeat with obj in ObjList
set aName to name of obj
set name of obj to ("xxx" & (name of obj) as string)
set _Name to name of obj
end repeat
end tell
end open

以下のスクリプトはエラーになる。
--test 4
tell application "Finder"
set aName to name of selection
set name of selection to ("xxx" & (name of obj) as string)
end tell

「osx finder リネーム AppleScript」でググった結果、MacWiki - AppleScriptTipsというありがたやなページを発見。
--test 5
tell application "Finder"
set addStr to "xxx"
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
end tell
おお、意味はわからんけど機能した。いけるかも。

--test 6
tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell
tell application "Finder"
set addStr to "xxx"
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
end tell
できた。すごいじゃんオレ。今週も街の巨匠に感謝。動作は遅いけど、かまわない。

「set FinderSelection to selection as alias list」というのはなんのことだろう。
「alias list」ってナニ?

という疑問はともかくだ、「そのファイルのあるウィンドウが Finder において最前面でない場合」に対応しなくてはならない。「InDesign に document 1 のパスを取得させてフォルダ名を取り出し・・・」てなことができればいいんだろうけど、残念ながら僕の脳味噌にそこまでの能力はないので、片っ端から総当たりでやるしかない。

--test 2-B
tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell
tell application "Finder"
try
select file doc_name of window 1
on error
select window 2
select file doc_name of window 1
end try
set label index of file doc_name of window 1 to 5
end tell
機能する。

--test 2-C
tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell
tell application "Finder"
try
select file doc_name of window 1
on error
select window 2
select file doc_name of window 1
on error
select window 3
select file doc_name of window 1
end try
set label index of file doc_name of window 1 to 2
end tell
機能しない。というか構文エラーで保存できない。

tell application "Finder"
 try
  window 1 のファイル「A.indd」を選択せよ
 on error
  window 2 を選択せよ(これで window 2 が window 1 になる)
  window 1 のファイル「A.indd」を選択せよ
 on error
  window 3 を選択せよ(これで window 3 が window 1 になる)
  window 1 のファイル「A.indd」を選択せよ
 end try
end tell
というのは、間違った構文らしい。そうなのか。どうすりゃいいのさ。

try
 Aを実行せよ
on error
  try
    Bを実行せよ
  on error
    try
      Cを実行せよ
    end try
  end try
end try
というマトリョーシカ構造にしなきゃダメってことか。そっか。
いやそれよりも「if exists file ホニャララ of window 1 then」みたいなので行けるのか。行けそうな気もするな。

tell application "Finder"
if exists file "001.jpg" of window 1 then
open file "001.jpg" of window 1
end if
end tell
機能する。window 1 に「001.jpg」が存在しないときは、エラーを吐かず、ただ何もしない。

tell application "Finder"
if exists file "79bb4618.jpg" of window 1 then
open file "79bb4618.jpg" of window 1
select window 1
else if exists file "79bb4618.jpg" of window 2 then
open file "79bb4618.jpg" of window 2
select window 2
else if exists file "79bb4618.jpg" of window 3 then
open file "79bb4618.jpg" of window 3
select window 3
else if exists file "79bb4618.jpg" of window 4 then
open file "79bb4618.jpg" of window 4
select window 4
else if exists file "79bb4618.jpg" of window 5 then
open file "79bb4618.jpg" of window 5
select window 5
else if exists file "79bb4618.jpg" of window 6 then
open file "79bb4618.jpg" of window 6
select window 6
else if exists file "79bb4618.jpg" of window 7 then
open file "79bb4618.jpg" of window 7
select window 7
else if exists file "79bb4618.jpg" of window 8 then
open file "79bb4618.jpg" of window 8
select window 8
else if exists file "79bb4618.jpg" of window 9 then
open file "79bb4618.jpg" of window 9
select window 9
else if exists file "79bb4618.jpg" of window 10 then
open file "79bb4618.jpg" of window 10
select window 9
else if not (exists file "79bb4618.jpg" of window 10) then
display dialog "ファイルが見つかりません" with icon caution
end if
end tell
やった。機能する。これを「test6」と合体させればいいんだな。

tell application "Finder"
set addStr to "xxx"
if exists file doc_name of window 1 then
select window 1
select file doc_name of window 1

else if exists file doc_name of window 2 then
select window 2
select file doc_name of window 1

else if exists file doc_name of window 3 then
select window 3
select file doc_name of window 1

else if exists file doc_name of window 4 then
select window 4
select file doc_name of window 1

else if exists file doc_name of window 5 then
select window 5
select file doc_name of window 1

else if exists file doc_name of window 6 then
select window 6
select file doc_name of window 1

else if exists file doc_name of window 7 then
select window 7
select file doc_name of window 1

else if exists file doc_name of window 8 then
select window 8
select file doc_name of window 1

else if exists file doc_name of window 9 then
select window 9
select file doc_name of window 1

else if exists file doc_name of window 10 then
select window 10
select file doc_name of window 1

else if not (exists file doc_name of window 10) then
display dialog "ファイルが見つかりません" with icon caution
end if
end tell
こう書き変えて、この「◉」部分に
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
を入れればいいわけだ。

tell application "Adobe InDesign CS4"
save document 1
set doc_name to (name of document 1) as Unicode text
close document 1
end tell

tell application "Finder"
set addStr to "xxx"
if exists file doc_name of window 1 then
select window 1
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 2 then
select window 2
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 3 then
select window 3
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 4 then
select window 4
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 5 then
select window 5
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 6 then
select window 6
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 7 then
select window 7
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 8 then
select window 8
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 9 then
select window 9
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if exists file doc_name of window 10 then
select window 10
select file doc_name of window 1
try
set FinderSelection to selection as alias list
on error
set FinderSelection to selection
end try
repeat with aFile in FinderSelection
set oldName to name of aFile
set newName to addStr & oldName
set name of aFile to newName
end repeat
else if not (exists file doc_name of window 10) then
display dialog "ファイルが見つかりません" with icon caution
end if
end tell
できた。
by nobiox | 2014-01-28 19:49 | ├バカスクリプト |
<< InDesign で PDFと... | 明治維新と産業革命と印象派 >>