Alfred Workflowで、AppleScriptを使ってFinderとPath Finderの現在開いているパスを取得する方法

AlfredのWorkflowを使っていると、FinderやPath Finderで開いているパスを取得して、そのパスに対してなにか操作したいときがあります。
今回はFinderとPath Finderそれぞれのパスの取得方法と、どちらかアクティブになっている方を取得する方法をまとめておきます。
AlfredのWorkflowでAppleScriptを使う方法
AlfredのWorkflowでAppleScriptを使いたいときは、Workflowの編集画面で右クリックして[Actions]→[Run NSAppleScript]を選択します。
下の画像のようなパネルが表示されます。

この中にAppleScriptを書いていくのですが、最初と最後の行は消すと動かなくなってしまうので、そのままにしておきます。
on alfred_script(q)
-- your script here
end alfred_scriptパスの取得方法
ここからパスの取得方法になります。
Finderのパス取得
Finderで現在開いているフォルダのパスを取得したい場合は下記のコードをコピペします。
on alfred_script(q)
tell application "Finder"
set pathList to (quoted form of POSIX path of (folder of the front window as alias))
return pathList
end tell
end alfred_script何故かreturnで返すと、パスの最初と最後に「'(シングルクォート)」が付いてしまい、後の操作のときに困ります。
2019年8月21日:追記
Twitterにて@MD5500さんよりquoted form ofの部分を消せばFinderのシングルクォーテーションは消えるとのご指摘いただきました。
なぜかシングルクォートがついてしまいますということですが、『quoted form of』を消せばいいのでは??
— MD5500 (@MD5500) August 21, 2019
on alfred_script(q)
tell application "Finder"
set pathList to POSIX path of (folder of the front window as alias)
return pathList
end tell
end alfred_script全然気が付かず、「あれ?こんな仕様だったっけ?Alfred側の問題?」とか色々考えていましたが、ものすごく初歩的なミスでした。ご指摘ありがとうございます。
以降のコードに関しては、コピペがしやすいように該当部分を修正させていただきます。
【追記終了】
そこで、Run NSAppleScriptのあとに[Utilities]→[Replace]を追加して、シングルクォートを空白(何もなし)に置き換えています。

置き換えをしてしまうと、ファイル名にシングルクォートが付いている場合にうまく動作してくれなくなってしまいますが、ひとまず応急処置としてこのようにしています(Path Finderではシングルクォートが付かないので問題ない)。
もっとスマートなやり方ありましたら、ぜひアドバイスをお願いします。
Path Finderのパス取得
Path Finderで現在開いているフォルダのパスを取得したい場合は下記のコードをコピペします。
on alfred_script(q)
tell application "Path Finder"
set pathList to POSIX path of the target of the front finder window
return pathList
end tell
end alfred_scriptFinderのパスを優先的に取得
基本的にはFinderで現在開いているフォルダのパスを取得しますが、Path Finderがアクティブなときのみ、Path Finderで現在開いているフォルダのパスを取得したい場合は、下記のコードをコピペします。
on alfred_script(q)
tell application "System Events"
set appName to name of the first process whose frontmost is true
end tell
if appName is "Path Finder" then
tell application "Path Finder"
set pathList to POSIX path of the target of the front finder window
return pathList
end tell
else
tell application "Finder"
set pathList to POSIX path of (folder of the front window as alias)
return pathList
end tell
end if
end alfred_scriptPath Finderのパスを優先的に取得
基本的にはPath Finderで現在開いているフォルダのパスを取得しますが、Finderがアクティブなときのみ、Finderで現在開いているフォルダのパスを取得したい場合は、下記のコードをコピペします。
on alfred_script(q)
tell application "System Events"
set appName to name of the first process whose frontmost is true
end tell
if appName is "Finder" then
tell application "Finder"
set pathList to POSIX path of (folder of the front window as alias)
return pathList
end tell
else
tell application "Path Finder"
set pathList to POSIX path of the target of the front finder window
return pathList
end tell
end if
end alfred_script取得したあとの処理
取得したあとは、アクションの中に{query}と入力すれば、そこにパスが入ります。
もし、そのままAppleScriptの処理をしたい場合はpathList変数にパスが入ってあるので、それを使用してください。

Path Finder 9がリリース!シェルフ機能が復活してモジュールの利用が更に便利に!
ATOKで言葉の意味を確かめる「電子辞典検索」タブの設定項目
単語登録機能に日常でよく使うフレーズを登録して、文章を素早く入力する方法
Macの「システム終了」や「スリープ」操作を素早く行う方法
AlfredでFigmaのチーム・プロジェクト・ファイルを素早くの検索ができる「Alfred Figma Workflow」
MacのターミナルアプリはiTerm2で決まり!!オススメの設定と基本的な機能まとめ
Hazelを使ってMacのファイル整理を自動化する
Orbital 2で共通の設定を使い回すためのテンプレートファイルを作成する方法
Illustratorでスクリプトを実行するいくつかの方法
「Karabiner-Elements Alert」のダイアログが出てきて、Karabiner-Elementsが使えない場合の対処法
AirPodsで片耳を外しても再生が止まらないようにする方法
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
ウェブサイト上で簡単にWYSIWYGエディタを実装する「TinyMCE」
CSSのz-indexの確認や管理を便利にする方法
Macにデフォルトで入っている壁紙やスクリーンセーバ、アイコンの保存場所
JavaScriptで配列内から特定条件の要素を探す方法
gulp 4.0から新しく加わったseriesとparallelについての備忘録
操作に没頭できる!Firefox版 Arcのようなブラウザ「Zen Browser」
1Passwordで素早くパスワードを入力するためのショートカットキー
MacのSIPのオン・オフを切り替える方法
ウェブサイトをダークモードに対応するときに気をつけること