# Finderで選択したフォルダを特定のアプリケーションで開くAlfred Workflowの作り方 URL: https://webrandum.net/alfred-open-with-application/ 公開日: 2019-09-29 更新日: 2019-09-29 カテゴリー: 作業効率化 タグ: Alfred, Workflow 出典: Webrandum(著者: サイトウ マサカズ) 以前「Finderで選択したフォルダをVisual Studio Codeで開くWorkflowの作り方」を[記事](https://webrandum.net/alfred-open-with-vscode/)にしたのですが、今回はVisual Studio Code以外のアプリケーションにも対応したくなってきました。 [Finderで選択したフォルダをVisual Studio Codeで開くAlfred Workflowの作り方](https://webrandum.net/alfred-open-with-vscode) そこで、アプリケーション名を入力したら、そのアプリケーションで選択フォルダを開くWorkflowを作ってみました。 ## Workflowの作成 まずはAlfredの環境設定の[Workflows]のサイドバー下の「+」ボタンを押し、「Blank Workflow」をクリックして必要情報を入力します。 今回の場合は「選択したフォルダを特定のアプリケーションで開くWorkflow」なので、名前は「Open with Application」としておきます。 ![Workflowの作成](https://webrandum.net/mskz/wp-content/uploads/2019/09/image_1-25-1024x571.png) ### キーワードの重複を修正 今回のWorkflowのキーワードは「o(openの頭文字)」が覚えやすくていいのですが、「o」は既にFile Search機能の「Opening Files」の起動で使っていました。 もし、設定している人は[Features]→[File Search]の「Opening Files」の「o」を「open」に戻しておくか、別のキーワードに設定しておきましょう。 ![[File Search]のOpening Filesを「o」以外に設定しておく](https://webrandum.net/mskz/wp-content/uploads/2019/09/image_2-21-1024x590.png) 正直、「Opening Files」はAlfred起動後にspaceを入力しても発火してくれるので、キーワードから発火することはほとんどありません。 ### キーワードリストの設定 黒い背景部分を右クリックして、[Inputs]→[List Filter]を選択します。 「Keyword:」には「o」を設定、あとはタイトルやサブタイトルを入力していきます。 ![List Filterの設定](https://webrandum.net/mskz/wp-content/uploads/2019/09/image_3-15-1024x590.png) 選択肢の「Title」と「Subtitle」は何でもいいのですが、「Arg」に関してはアプリケーション名を正確に入力するようにしてください。 正確に入力するためには、Finderの「アプリケーション」フォルダへ行って、直接アプリケーション名をコピーするのが1番です。 ひと通り登録し終わったら「Save」を押して保存します。 ### 選択アプリケーションで開く設定 Keywordの右横にあるポッチをクリックして、[Actions]→[Run NSAppleScript]を選択してAppleScriptを入力します。 ![AppleScriptのコード](https://webrandum.net/mskz/wp-content/uploads/2019/09/image_4-8-1024x590.png) 今回使用するAppleScriptは、[前回の記事](https://webrandum.net/alfred-open-with-vscode/)で使用したコードを更に改良しています。 ``` on alfred_script(q) set finderSelection to "" set theTarget to "" set appName to q set appPath to path to application appName set defaultTarget to (path to home folder as alias) tell application "System Events" set selectApp to name of the first process whose frontmost is true end tell -- comment line above and uncomment line below to open desktop instead of user home when there's no selection or open folder in the Finder: -- set defaultTarget to (path to desktop folder as alias) if selectApp is "Finder" then tell application "Finder" set finderSelection to (get selection) if length of finderSelection is greater than 0 then set theTarget to finderSelection else try set theTarget to POSIX path of the target of the front finder window on error set theTarget to defaultTarget end try end if tell application "Finder" open theTarget using appPath end tell end tell else tell application "Path Finder" set theTarget to {} repeat with pfItem in (get selection) set the end of theTarget to POSIX path of pfItem end repeat end tell tell application appName open theTarget end tell end if end alfred_script ``` `q`には直前のWorkflow ObjectであるList Filterの引数(Arg)が入力されるので、ここに選択したアプリケーション名が入って、開いてくれるというわけです。 ## まとめ 開きたいアプリケーションの追加・削除などの整理はList Filterで簡単にできます。 よく使うアプリケーションはKeyboard Maestroなどでショートカットキーから開けるようにした方がラクですが、たまにしか開かないアプリケーションはショートカットキーを覚えるのも面倒なので、Alfredから開けるようにした方が良さそうですね。