Search code examples
macosshellopen-with

Programmatically building applications for "Open with..." on Mac


I'd like to create a program which uses certain information given by my program's users and bakes this info into executables which can be used with Mac's "Open with...".

Since bash scripts cannot be used directly for Mac's "Open with..." functionality1, however, I'd like to find a way to wrap such a script (and as painlessly as possible).

I am aware Automator lets one build applications which include bash scripts and which work with "Open with...", but I want to be able to programmatically build both the script and the containing application (and I haven't found that Automator can accept command line arguments to compose applications).

What are some simple, lowest common denominator/open format approaches (without using Python, etc.) whereby I can do this (ideally in a way that could work on Linux as well)?


Solution

  • I found I was able to build an AppleScript application programmatically using osacompile (or apparently also possibly using Apple's JavaScript) using on open listener to listen for "Open with...", default file association, or dragged files (including when placed on the dock) and an on run listener to work with command line invocation. I could then use do shell script internally to invoke my desired file (in my case, calling the Node.js binary with a Node script and some arguments).

    I was also able to do try/on error check for an input and if none were present do a choose file with prompt (specifically choose file with prompt "Prompt message" of type {"js"} for JS files) so that the application could be double-clicked without a file argument but yet trigger a file dialog (e.g., for use on the dock).