AppleScriptのファイル参照にまつわるメモ

  • 特定のファイルに自在にアクセスして操作することは、コードを書く上で最も基本的な技術のはず。
  • にもかかわらず、AppleScriptでコードを書く時に的確なファイル参照がおろそかで、試行錯誤して無駄な時間を費やしていた。
  • もう、ファイル参照で迷わぬように、理解できないエラーで立ち止まらないように、できる限り調べてみた。

alias参照


--Mac形式のパステキストは、ディレクトリを":"で区切る
set Mac_path_text to "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--Mac形式のパステキストから、alias参照を求める(以下2通りの方法がある)
alias Mac_path_text
Mac_path_text as alias
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--":"は起動ディスクとみなされる
alias ":"
--結果:alias "Macintosh HD:"

--よって、"Macintosh HD:"が起動ディスクである場合、以下のパスはどちらも同じである
alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
alias ":Users:zari:Desktop:test_path_to.me.scpt"
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--存在しないファイルのalias参照はエラーになる
alias ":Users:zari:Desktop:test_path_to.me.no_exist"
--結果:error "ファイル alias :Users:zari:Desktop:test_path_to.me.no_exist of «script» が見つかりませんでした。"

  • alias参照のファイル追跡機能は強力だ!ファイル名を変更しようが、ゴミ箱に捨てようが、リンクし続ける。


--選択したファイルを追跡する
--ファイル名を変更したり、ゴミ箱に捨てたりしても、10秒後には元どおり
set alias_path to (choose file) as alias
tell application "Finder" to set alias_name to alias_path's name
tell application "Finder" to set alias_dir to alias_path's folder as alias

repeat
display dialog "10秒待機しています。" giving up after 10
tell application "Finder"
move alias_path to alias_dir
set alias_path's name to alias_name
end tell
end repeat

POSIX file参照


--UNIX形式のパステキストは、ディレクトリを"/"で区切る
set UNIX_path_text to "/Users/zari/Desktop/test_path_to.me.scpt"

--UNIX形式のパステキストから、file参照を求める(以下2通りの方法がある)
POSIX file UNIX_path_text
UNIX_path_text as POSIX file
--結果:file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--"/"は起動ディスクとみなされる
POSIX file "/"
--結果:file "Macintosh HD:"

--存在しないファイルのPOSIX file参照も取得できる
POSIX file "/Users/zari/Desktop/test_path_to.me.no_exist"
--結果:file "Macintosh HD:Users:zari:Desktop:test_path_to.me.no_exist"

--但し、それをalias参照に変換しようとするとエラーになる
POSIX file "/Users/zari/Desktop/test_path_to.me.no_exist" as alias
--結果:error "file \"Macintosh HD:Users:zari:Desktop:test_path_to.me.no_exist\" のタイプを alias に変換できません。"

参照の変換

  • text、alias、POSIX fileへ、相互に変換する例。


--Mac形式のパステキストから、alias参照に変換する
":Users:zari:Desktop:test_path_to.me.scpt" as alias
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--UNIX形式のパステキストから、POSIX file参照に変換する
"/Users/zari/Desktop/test_path_to.me.scpt" as POSIX file
--結果:file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"


--alias参照から、Mac形式のパステキストに変換する
alias ":Users:zari:Desktop:test_path_to.me.scpt" as text
--結果:"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--alias参照から、UNIX形式のパステキストに変換する(取り出す)
(alias ":Users:zari:Desktop:test_path_to.me.scpt")'s POSIX path
--結果:"/Users/zari/Desktop/test_path_to.me.scpt"


--POSIX file参照から、Mac形式のパステキストに変換する
POSIX file "/Users/zari/Desktop/test_path_to.me.scpt" as text
--結果:"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--POSIX file参照から、alias参照に変換する
POSIX file "/Users/zari/Desktop/test_path_to.me.scpt" as alias
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--POSIX file参照から、UNIX形式のパステキストに変換する(取り出す)
(POSIX file "/Users/zari/Desktop/test_path_to.me.scpt")'s POSIX path
--結果:"/Users/zari/Desktop/test_path_to.me.scpt"

Finder file参照

  • Finderによるファイル参照と、alias参照・POSIX file参照との違いとか、使用例とか。


--Finderに依存したfile参照もある。Finderの持つオブジェクトの連鎖で特定のファイルを指定している。
tell application "Finder"
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
--結果:document file "test_path_to.me.scpt" of folder "Desktop" of folder "zari" of folder "Users" of startup disk of application "Finder"
alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
POSIX file "/Users/zari/Desktop/test_path_to.me.scpt"
--結果:file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
end tell

--Finderに依存しているので、Finderのtellブロックの外側ではエラーになる
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
--結果:error "file \"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt\" を取り出すことはできません。"

--一方、alias参照やPOSIX file参照はAppleScript自身の基本機能なので問題ない。
alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
POSIX file "/Users/zari/Desktop/test_path_to.me.scpt"
--結果:file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"

--Finderのfileであることを示せば良いので、以下のような書き方でもOK。
application "Finder"'s file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
--結果:document file "test_path_to.me.scpt" of folder "Desktop" of folder "zari" of folder "Users" of startup disk of application "Finder"


--alias参照と違って、起動ディスクを省略することが出来ない。
tell application "Finder"
file ":Users:zari:Desktop:test_path_to.me.scpt"
--結果:error "Finder でエラーが起きました:file \":Users:zari:Desktop:test_path_to.me.scpt\" を取り出すことはできません。"
alias ":Users:zari:Desktop:test_path_to.me.scpt"
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
end tell

--text、alias、POSIX pathに変換できる
tell application "Finder"
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt" as text
--結果:"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt" as alias
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"'s POSIX path
--結果:"/Users/zari/Desktop/test_path_to.me.scpt"
end tell

--但し、変数に代入すると、POSIX pathに変換できなくなった...。
tell application "Finder"
set file_path to file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
file_path's POSIX path
--結果:error "«class docf» \"test_path_to.me.scpt\" of «class cfol» \"Desktop\" of «class cfol» \"zari\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"を期待されるタイプに変換できません。"
--text、aliasには問題なく変換できるのに...。不思議???
file_path as text
--結果:"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
file_path as alias
--結果:alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
end tell

ファイル情報など

  • info for、properties、path toの使い方。


--Finder file参照・alias参照・POSIX file参照は、基本的に全く同じファイルを指し示している。
--info forでOSの管理するファイルの基本情報が表示された。
info for application "Finder"'s file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
info for alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"
info for POSIX file "/Users/zari/Desktop/test_path_to.me.scpt"
--結果:{name:"test_path_to.me.scpt", creation date:date "2010年9月21日火曜日 9:42:31", modification date:date "2010年9月21日火曜日 10:37:26", size:7016, folder:false, alias:false, package folder:false, visible:true, extension hidden:false, name extension:"scpt", displayed name:"test_path_to.me.scpt", default application:alias "Macintosh HD:Applications:Utilities:AppleScript Editor.app:", kind:"スクリプト", file type:"osas", file creator:"ToyS", type identifier:"com.apple.applescript.script", locked:false, busy status:false, short version:"", long version:""}

tell application "Finder"
--Finderに対してpropertiesを調べると、Finderの持つファイル情報も表示された。
file "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"'s properties
alias "Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt"'s properties
--結果:{class:document file, name:"test_path_to.me.scpt", index:3, displayed name:"test_path_to.me.scpt", name extension:"scpt", extension hidden:false, container:folder "Desktop" of folder "zari" of folder "Users" of startup disk of application "Finder", disk:startup disk of application "Finder", position:{-1, -1}, desktop position:{1096, 215}, bounds:{-33, -33, 31, 31}, kind:"スクリプト", label index:0, locked:false, description:missing value, comment:"", size:7016, physical size:12288, creation date:date "2010年9月21日火曜日 9:42:31", modification date:date "2010年9月21日火曜日 10:37:26", icon:missing value, URL:"file://localhost/Users/zari/Desktop/test_path_to.me.scpt", owner:"zari", group:"staff", owner privileges:read write, group privileges:read only, everyones privileges:read only, file type:"osas", creator type:"ToyS", stationery:false, product version:"", version:""}
--但し、なぜかPOSIX file参照ではエラーになってしまった...。
POSIX file "/Users/zari/Desktop/test_path_to.me.scpt"'s properties
--結果:error "Finder でエラーが起きました:properties of file \"Macintosh HD:Users:zari:Desktop:test_path_to.me.scpt\" を取り出すことはできません。
--alias参照に変換すればOKだった。不思議???
(POSIX file "/Users/zari/Desktop/test_path_to.me.scpt" as alias)'s properties
end tell

--path to オブジェクトで、オブジェクトへのalias参照が取得できた。
path to me
--結果:alias "Macintosh HD:Users:zari:Library:Scripts:_Development:Finder file参照.scpt"
path to application "Finder"
--結果:alias "Macintosh HD:System:Library:CoreServices:Finder.app:"

特殊フォルダ一覧


application support --アプリケーションサポート
applications folder --アプリケーションフォルダ
desktop --デスクトップ
desktop pictures folder --デスクトップピクチャ・フォルダ
documents folder --書類フォルダ
favorites folder --よく使う項目フォルダ
Folder Action scripts --フォルダアクションスクリプト
fonts --フォント
frontmost application --最前面のアプリケーション
help folder --ヘルプフォルダ
home folder --ホームフォルダ
keychain folder --キーチェーンフォルダ
library folder --ライブラリフォルダ
modem scripts --モデムスクリプト
movies folder --ムービーフォルダ
pictures folder --ピクチャフォルダ
preferences --環境設定
printer descriptions --プリンタ記述
public folder --パブリックフォルダ
scripts folder --スクリプトフォルダ
scripting additions --スクリプティング機能追加
shared libraries --共有ライブラリ
shared documents folder --共有書類フォルダ
sites folder --サイトフォルダ
trash --ゴミ箱
startup disk --起動ディスク
system folder --システムフォルダ
temporary items --暫定項目
utilities folder --ユーティリティフォルダ
voices --音声
current user folder --現在ログイン中のユーザフォルダ
users folder --ユーザフォルダ
system preferences --システム環境設定

--利用例
desktop as alias
--結果:error "desktop のタイプを alias に変換できません。

path to desktop
--結果:alias "Leopard HD:Users:bebe:Desktop:"

desktop's POSIX path
--結果:error "POSIX path of desktop を取り出すことはできません。

(path to desktop)'s POSIX path
--結果:"/Users/bebe/Desktop/"

current user folder as text
--結果:"current user folder"

tell application "Finder" to (path to current user folder)'s name
--結果:"zari"

所感

  • 今まで、AppleScript基本機能のalias参照・POSIX file参照と、Finderのfile参照がごちゃ混ぜになっていた。今なら明確に区別できる。
  • POSIX file参照ではFinderのpropertiesへのアクセスがエラーになる。よく謎のエラーで悩まされていたのは、これが原因だったのかもしれない。
  • Finder file参照を変数に代入すると、POSIX pathに変換できなくなることも分かった。これも謎のエラーに悩まされる原因になりそう。
  • なぜエラーになるのか疑問は残ったが、alias参照に変換して利用することにしよう。
  • 起動ディスク名について、POSIX file参照では不要。alias参照では省略可能。Finder file参照では必須。
  • POSIX file参照は、ファイルが存在していなくてもOK。alias参照・Finder file参照は、ファイルが存在していないとエラーになる。
  • ファイルのpropertiesを取得したい時は、Finderに対して命令する。info for・path toは、Finderのtellブロック外側でもOK。