- Run Automator,
File -> New
and choose Application. - Click
Library -> Utilities
and double click Run AppleScript. - Paste the script from AppleScript section below.
- Save; if Automator tries to save to iCloud drive, better to save locally - save as
VSCodeOpenerHelper.app
to the Desktop and then move it to~/Library/Services
. - Drag and drop
VSCodeOpenerHelper.app
onto the Finder toolbar while holding down the command key to place a toolbar launch icon. Note that the first time you launch there may be a security popup sayingVSCodeOpenerHelper.app
wants access... Click OK to all. - To replace the generic Automator app icon with the Visual Studio Code icon, command+i (Get info) on the original Visual Studio Code application, select the application icon in the top left corner of the window and copy it (command+c). Then command+i (Get info) to
VSCodeOpenerHelper.app
file, select the application icon in the top left corner of the window and paste (command+v).
on run {input, parameters}
set frontApp to (path to frontmost application as Unicode text)
if (frontApp does not contain "Finder.app") then
-- Finder does not have focus.
return
end if
tell application "Finder"
set sel to selection as alias list
if not sel = {} then
-- stuff is selected
set dir_path to my finderSelectionToString(sel)
else
-- no items selected
set listSize to count of (every window)
if listSize is equal to 0 then
-- The Finder desktop has focus and no windows anywhere else. default to home dir.
set dir_path to "~"
else
try
set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
on error errMsg
-- This is a special dir (e.g. Network or "machine name"). default to home dir.
set dir_path to "~"
end try
end if
end if
end tell
do shell script "open -n -b \"com.microsoft.VSCode\" --args " & dir_path
end run
on finderSelectionToString(selection_list)
-- finder's selection list to list of paths (strings)
set path_list to {}
repeat with selection_item in selection_list
set the end of path_list to quoted form of POSIX path of (contents of selection_item)
end repeat
-- list of paths to space-joined string
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set concat_path to path_list as text
set AppleScript's text item delimiters to TID
return concat_path
end finderSelectionToString
Inspired on pdanford's iTerm version