Finderで選択したフォルダをVisual Studio Codeで開くAlfred Workflowの作り方

Finderで選択しているフォルダやファイルを、Visual Studio Codeで開くWorkflowの作り方を紹介しておきます。
作り方といっても、既に「Open with Sublime Text」というWorkflowを作ってくれてる人がいるので、このWorkflowを基にして、Sublime Textが開く部分をVisual Studio Codeに変更して作成します。
Open with Sublime Textのインポート
まずはGitHubのページから、「Open with Sublime Text」をダウンロードします。
「Open with Sublime Text.alfredworkflow」というファイルが入ってあるので、それをダウンロードしてダブルクリックすると、Alfredにインポートできます。

Visual Studio Codeで開くように修正
インポートしたら、念のためそのWorkflowを複製しておきます。
Workflowを右クリックして[Duplicate]をクリックすると複製できます。

インストールしたら、「Sublime Text」になっているところを「Visual Studio Code」に変えていきます。
- テキストが「Sublime Text」になっているのを「Visual Studio Code」に変更
- アイコンをVisual Studio Codeのものに変更
- キーワードが「subl」なのを「vs」などに変更する

「Run NSAppleScript」をダブルクリックして、開くアプリを「Sublime Text」から「Visual Studio Code」に変更します。
具体的にはset appPath to path to application "Sublime Text"の部分をVisual Studio Codeに変えます。

これでFinderで選択しているフォルダやファイルをVisual Studio Codeで開けるようになります。
Path Finderのパスも取得するようにしたい
このままでは、Finderの選択フォルダは開けるのですが、Path Finderの選択フォルダは開けません。
そこで、「Run NSAppleScript」の処理を書き換えて、Path Finderの選択フォルダも開けるようにします。
on alfred_script(q)
set finderSelection to ""
set theTarget to ""
set appName to "Visual Studio Code"
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 (q as string) is "" then
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 (folder of the front window as alias)
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 POSIX path of the target of the front finder window
end tell
tell application appName
open theTarget
end tell
end if
else
try
set targets to {}
set oldDelimiters to text item delimiters
set text item delimiters to tab
set qArray to every text item of q
set text item delimiters to oldDelimiters
repeat with atarget in qArray
if atarget starts with "~" then
set userHome to POSIX path of (path to home folder)
if userHome ends with "/" then
set userHome to characters 1 thru -2 of userHome as string
end if
try
set atarget to userHome & characters 2 thru -1 of atarget as string
on error
set atarget to userHome
end try
end if
set targetAlias to ((POSIX file atarget) as alias)
set targets to targets & targetAlias
end repeat
set theTarget to targets
tell application "Finder"
open theTarget using appPath
end tell
on error
return (atarget as string) & " is not a valid file or folder path."
end try
end if
end alfred_scriptこのset appName to "Visual Studio Code"のアプリケーション名を変えれば他のテキストエディタも開けるようになるので、複数使っているのであれば複製して用意しておくと、便利になりそうです。


Chrome拡張機能のVimiumを使ってキーボードだけでブラウザを操作する
iPhoneをもっと便利に使うための細かい小技集 #1日1Tips – 2020年2月
Evernoteで特定のタグを除外して検索する方法
Macの最前面ウインドウをハイライトし、背景を薄暗くして集中力を高める「HazeOver」
作業効率化に大きく貢献してくれているMacのアプリ達
Markdownで画像を表示する時はGyazoを使うのが便利
ATOKの「ATOKパレットの表示」「単語登録」ショートカットキーが、Emacsキーバインドと被るので変更したい
macOSで画面を拡大縮小するズーム機能の設定
Macのデフォルトブラウザの設定場所
Path Finderの見た目を最低限整えるための設定
iTerm2で「Use System Window Restoration Setting」を設定しているとアラートが表示されて機能しない
Google Chromeのサイト内検索(カスタム検索)機能を別のプロファイルに移行する方法
iPadで入力モードを切り替えずに数字や記号をすばやく入力する方法
iPhoneやiPadでYouTubeの再生速度を3倍速や4倍速にする方法
Keynoteで有効にしているはずのフォントが表示されない現象
MacのKeynoteにハイライトされた状態でコードを貼り付ける方法
iTerm2でマウスやトラックパッドの操作を設定できる環境設定の「Pointer」タブ
AirPodsで片耳を外しても再生が止まらないようにする方法
DeepLで「インターネット接続に問題があります」と表示されて翻訳できないときに確認すること
Ulyssesの「第2のエディタ」表示を使って2つのシートを横並びに表示する
1つのノートアプリにすべて集約するのをやめた理由|2025年時点のノートアプリの使い分け
Notionログイン時の「マジックリンク」「ログインコード」をやめて普通のパスワードを使う
AlfredでNotion内の検索ができるようになるワークフロー「Notion Search」
Gitで1行しか変更していないはずのに全行変更した判定になってしまう
Macでアプリごとに音量を調節できるアプリ「Background Music」
Macのターミナルでパスワード付きのZIPファイルを作成する方法
MacBook Proでディスプレイのサイズ調整をして作業スペースを広げる
SteerMouseの「自動移動」機能で保存ダイアログが表示されたら自動でデフォルトボタンへカーソルを移動させる
iPhoneでタッチが一切効かなくなった場合に強制再起動する方法