Jun 29 2016

Launch Mac apps with command-line parameters from Windows running under Parallels

I love running Windows using Parallels Desktop on my MacBook Pro (yes, those of you who know me well are aware that it wasn’t that long ago that I was amongst the Mac clueless).  Parallels makes it easy to associate your files with Mac applications running on your host.  If you want to launch a Mac app from the command line, either PowerShell or cmd.exe, you just need to look in the following directory for the app to launch:

"C:\Users\rob\AppData\Roaming\Parallels\Shared Applications"

Note that you will need to replace my username (i.e. rob) in the path above with whatever account you are logged into in your Windows VM.

So, if I want to run the Sublime Text editor installed on my Mac host, I can just run the following from PowerShell:

& "C:\Users\rob\AppData\Roaming\Parallels\Shared Applications\Sublime Text (Mac).exe"

However, when I tried to pass a command-line parameter to this app, I couldn’t get it to work.  I was trying to pass the file that I wanted to open in Sublime Text from the command line, but Sublime always opened to a blank document.

Thanks to the fine folks at Parallels Support on Twitter, I got the solution: You must pass the full path to the file, not the relative path. It worked!

Now I had one more problem.  From PowerShell, I typically pass the relative path of a file (if I pass any path at all) to apps at the command line.  So, I needed to find a way to pass either the relative path to the file or the full path, if I already have it available.  So, I wrote a PowerShell function:

function s { & "C:\Users\rob\AppData\Roaming\Parallels\Shared Applications\Sublime Text (Mac).exe" (Get-Item $args[0]).FullName }

So, now I can run either of the following PowerShell commands to open a file in Sublime Text:

cd C:\Users\rob\Documents
s file.txt


s C:\Users\rob\Documents\file.txt

This means that I don’t have to install apps that I already own on my Mac in my Windows VM, as well.  This will save me from having to configure each of these apps twice.  This makes setting up new VMs really easy as well since they will just leverage my Mac’s configured apps.  Woohoo!