General
Adding an “Open in VS Code” button to Finder with AppleScript
I wanted a button in Finder that would open the selected file or folder in Visual Studio Code. If nothing was selected, I wanted it to open the folder I was browsing.
A small AppleScript app handles this. Finder supplies the selection or current location, and macOS opens it in VS Code. Save the script as an application, add it to Finder’s toolbar, and it becomes a button you can use from any Finder window.
I’ve published the source in codestuffio/finder-vscode-opener. You can build it from the repository or create it yourself in Script Editor.
Build it from the public repository
The repository includes the editable AppleScript, a build script for regular VS Code and VS Code Insiders, an original icon, and build regression tests. The code and original artwork are available under the MIT license.
With Git and your preferred edition of VS Code installed, run:
git clone https://github.com/codestuffio/finder-vscode-opener.git
cd finder-vscode-opener
For regular VS Code:
./scripts/build.sh
For VS Code Insiders:
./scripts/build.sh insiders
The build creates the launcher in dist/. Move that app to a permanent location, such as your user Applications folder, before adding it to Finder’s toolbar.
Rebuilding replaces the generated app for that edition, so keep your installed copy outside dist/.
The build looks for VS Code in /Applications and ~/Applications. If yours is somewhere else, supply its location:
VSCODE_APP_PATH='/path/to/Visual Studio Code - Insiders.app' \
./scripts/build.sh insiders
The generated app has a local ad hoc signature. It is intended to be built on your own Mac and is not Developer ID signed or notarized.
What AppleScript does here
AppleScript lets you ask Mac applications to perform actions through their scripting interfaces. In this case, the script asks Finder which items are selected. If that list is empty, it asks for the folder displayed in the front Finder window.
The launcher supports:
- Opening a selected file.
- Opening one or more selected folders or files.
- Opening the current folder when nothing is selected.
- Opening files or folders dropped onto the launcher.
- Using the Desktop when no Finder window is open.
My Mac has VS Code Insiders installed, so my launcher targets that version. The example below targets regular VS Code, with a one-line change for Insiders.
Create the script yourself
Open Script Editor, which is included with macOS, and create a new document. Make sure the language is set to AppleScript, then paste this code:
property editorBundleID : "com.microsoft.VSCode"
on run
try
tell application "Finder"
set selectedItems to selection as alias list
if (count of selectedItems) is 0 then
if (count of Finder windows) > 0 then
set selectedItems to ¬
{target of front Finder window as alias}
else
set selectedItems to {desktop as alias}
end if
end if
end tell
my openItems(selectedItems)
on error errorMessage number errorNumber
my showError(errorMessage, errorNumber)
end try
end run
on open droppedItems
try
my openItems(droppedItems)
on error errorMessage number errorNumber
my showError(errorMessage, errorNumber)
end try
end open
on openItems(itemsToOpen)
set openCommand to "/usr/bin/open -b " & ¬
quoted form of editorBundleID
repeat with anItem in itemsToOpen
set openCommand to openCommand & " " & ¬
quoted form of POSIX path of (anItem as alias)
end repeat
do shell script openCommand
end openItems
on showError(errorMessage, errorNumber)
if errorNumber is -1743 then
display alert "Allow access to Finder" message ¬
"This launcher needs Finder access to read your selection or current folder. Enable Finder for the launcher in System Settings > Privacy & Security > Automation, then try again."
else
display alert "Could not open in VS Code" message errorMessage
end if
end showError
For VS Code Insiders, change the first line to:
property editorBundleID : "com.microsoft.VSCodeInsiders"
The launcher uses macOS’s open command to find the application by its bundle identifier. You do not need to install VS Code’s code terminal command.
How the script works
The run handler executes when you launch the app, including by clicking its Finder toolbar button.
This line reads Finder’s selected files and folders:
set selectedItems to selection as alias list
An alias is AppleScript’s reference to an item on disk. If there are no selected items, the script uses the front Finder window’s target, which is the location displayed in that window.
The openItems handler converts each item into a filesystem path and adds it to the command that opens VS Code.
Each path is quoted for the shell:
quoted form of POSIX path of (anItem as alias)
That quoting handles spaces, apostrophes, and shell punctuation in filenames.
The separate open handler receives items dropped onto the app. Both handlers use the same opening logic. Apple documents these application handlers in its AppleScript Language Guide.
Save it as an application
If you used Script Editor instead of the repository’s build script, click Compile to check the syntax, then:
- Choose File > Export.
- Name it
Open in VS Code. - Set File Format to Application.
- Leave “Stay open after run handler” unchecked.
- Save it in a permanent location, such as your Applications folder.
Keep an editable copy of the script too. Apple’s guide to saving scripts explains the application format.
Saving it as an application lets Finder launch it directly, without opening Script Editor.
Add the button to Finder
Locate the launcher app in Finder. Hold Command and drag it into the top toolbar. Release it when you see the green plus sign.
This is Finder’s built-in method for adding an application to its toolbar, described in Apple’s Finder customization guide.
Add it from the location where you intend to keep it. Moving or deleting the app afterward can break the toolbar shortcut.
To remove the button, hold Command and drag it out of the toolbar.
Give it an icon
The repository’s build includes an original folder-and-code icon. Exporting directly from Script Editor gives you the default AppleScript icon.
To use your own image:
- Open the image in Preview.
- Press Command-A, then Command-C.
- Select your launcher in Finder and press Command-I.
- Click the small icon in the top-left corner of the Get Info window.
- Press Command-V.
Select the small icon beside the app’s name, rather than the larger image under Preview. Apple describes this process in its custom icon instructions.
My original personal launcher used the VS Code Insiders icon. The public example uses its own artwork because Microsoft’s brand guidelines prohibit using its icon to identify another application.
There was also a build detail worth fixing. AppleScript’s compiler added a CFBundleIconName setting pointing to its default icon. Setting CFBundleIconFile to a replacement image left that override in place. The repository’s build removes the default asset reference before signing the app.
If Finder still shows an old icon after replacing the launcher, remove the toolbar button and add it again from the installed copy.
Allow Finder access
The first time you click the button, macOS may ask whether the launcher can control Finder. Allow that access so it can read the selection and current folder.
If you previously denied access, go to System Settings > Privacy & Security > Automation, find the launcher, and enable Finder.
You can also see a permission request for Script Editor if you run the script there before exporting it. The exported app may need its own permission.
Check the behavior
Try the launcher with a selected file, then with a selected project folder. Clear the selection and click it again to check that it opens the current folder. Try multiple selected items and filenames containing spaces or apostrophes too.
For my original launcher, I verified that a file selected in Finder opened in VS Code Insiders.
The repository also has six build regression tests. On macOS with Python 3 installed, run:
python3 -m unittest discover -s tests -v
They check both build variants, the compiled editor identifiers, icon settings, signatures, and rebuild behavior. They also check invalid arguments and missing or mismatched editor installations. They do not launch VS Code or exercise Finder’s permission prompts.
One rebuild test caught another packaging bug: copying a newly signed app over an existing bundle could leave obsolete files behind and invalidate its signature. The build now replaces the generated bundle and verifies the final copy.
If it does not open the expected item
A selected item takes priority over the current folder. If you want to open the folder you are browsing, clear the selection first.
If VS Code cannot be found, check that it is installed and that the script uses the identifier for your version.
Finder views such as Recents and search results do not always represent a single folder on disk. In those views, select the actual file or folder you want to open.