Skip to content

Instantly share code, notes, and snippets.

@dsiguero
Last active June 20, 2022 03:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsiguero/7299df757a8e7ae92ac39e5452833250 to your computer and use it in GitHub Desktop.
Save dsiguero/7299df757a8e7ae92ac39e5452833250 to your computer and use it in GitHub Desktop.

Applescript script to open current folder or selected items in Visual Studio Code

  1. Run Automator, File -> New and choose Application.
  2. Click Library -> Utilities and double click Run AppleScript.
  3. Paste the script from AppleScript section below.
  4. 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.
  5. 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 saying VSCodeOpenerHelper.app wants access... Click OK to all.
  6. 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).

AppleScript

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment