フォルダを開きたい!
AppleScript 勉強中。φ(`д´)メモメモ
ドックに登録しても良いんですが、フォルダだらけで煩雑になってきたので、何か手はないかと。
tell application "Finder"
activate
open folder "Macintosh HD:Users:ユーザー名:Documents:×××"
end tell
"Macintosh HD:Users:ユーザー名:Documents:×××"は開きたいフォルダのパス。で、書類フォルダの中の「×××フォルダ」を開くことが出来ます。でも頻繁に使うフォルダは、ひとつだけじゃないのでリストで選択しちゃおう。
set theList to {"書類", "ミュージック", "×××"}
書類フォルダやミュージックフォルダや×××フォルダって、開きたいフォルダ名のリストを作って選択します。選択するフォルダ名を set theList to の{}中と、else if the_choice is 〜中に、パスを else if 〜の中に書けば沢山登録できます。ランチャーソフトみたい。開く作業は同じなので,
いちいち記述するのは面倒なので、ハンドラで。
on 命令文の名前(変数)
命令文
end 命令文の名前
こんな on~end をハンドラ というらしい。命令文の名前を open_folder に、開くフォルダのパスを変数として folder_path と、それぞれ名付けよう。
on open_folder(folder_path)
tell application "Finder"
activate
open folder folder_path
end tell
end open_folder
出来た!
set theList to {"書類", "ミュージック", "×××"}
choose from list theList with prompt "フォルダを選択"
set the_choice to result as text
if the_choice is "false" then
stop
else if the_choice is "書類" then
open_folder("Macintosh HD:Users:ユーザー名:Documents:")
else if the_choice is "ミュージック" then
open_folder("Macintosh HD:Users:ユーザー名:Music:")
else if the_choice is "×××" then
open_folder("Macintosh HD:Users:ユーザー名:Documents:×××")
end if
on open_folder(folder_path)
tell application "Finder"
activate
open folder folder_path
end tell
end open_folder
実際は、「書類フォルダ」「ミュージックフォルダ」などより、もっと深い階層のフォルダを登録する方が、意味が出てくるスクリプトでしょう。最初のフォルダ登録も AppleScript で出来そうかも?
フォルダのパスを知るのには、こんな AppleScript で出来ました。知りたいフォルダのパスがクリップボードにコピーされます。
set the_folder to choose folder with prompt "フォルダを選択"
set thePath to the_folder as string
set the clipboard to thePath
僕みたいな素人でも、色々出来ちゃうのが、楽しいです。