Skip to content

Instantly share code, notes, and snippets.

@Viranchee
Created August 22, 2023 16:56
Show Gist options
  • Save Viranchee/1becc79dbbc302b980607417702e33d9 to your computer and use it in GitHub Desktop.
Save Viranchee/1becc79dbbc302b980607417702e33d9 to your computer and use it in GitHub Desktop.
Finder to VSCode
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment