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変数にパスが入ってあるので、それを使用してください。


Google Meetをさらに便利にする便利なChrome拡張機能と、公式でサポートされるようになった機能
Apple Watchの余計な通知を来ないようにして快適にする方法
Alfredの環境設定の同期・バックアップの方法
Chrome拡張機能の「Create Link」を使って簡単にサイトリンクを生成する
Path Finderでタブセットを使うと、よく使うフォルダ一式を一気に開けるようになる!
Illustratorをスクリプトで操作する時の基本
Path Finder 8で動作がおかしいと思ったときにチェックすること
「Karabiner-Elements Alert」のダイアログが出てきて、Karabiner-Elementsが使えない場合の対処法
ATOKで簡単に今日の日付を入力する方法
AirPodsで片耳を外しても再生が止まらないようにする方法
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
Bartender ProのTop Shelf機能でできること
高機能なスクリーンショットアプリ「CleanShot X」
macOSの効率化に関するDiscordコミュニティ「CMD-F」を立ち上げました
1Passwordの開発者向け機能(SSH Agent / Environments / 1Password CLI / Developer Watchtower)
Default Folder Xで保存ダイアログ内のファイルリストにフォーカスする方法
Obsidianで整理した知識をAnkiで定着させる方法
Figma × Keyboard Maestroでどこまで効率化できる?
マウス操作の完成度が高いウインドウリサイズアプリ「Magnet」
Macで複数ディスプレイを使っている場合に便利な設定集
AppCleanerでMacアプリを関連ファイルごと完全に削除する