MacOS – OS X: Specifying custom command line invocation of program as application for opening files on double click

applescriptcustomizationfilemacosunix

I'm primarily a Linux user who recently acquired a MacBook Air and I'm currently desperately trying to find out whether there's an equivalent for Linux's desktop definition files, where you can define an application by its command line invocation, with an optional placeholder for the file that X11 will pass to it when a file of file type associated with that application is double-clicked.

E.g. on Linux, I can set .txt files to be opened with a specific command line invocation of emacsclient like so:

emacsclient -c -a "" %f

where %f is the placeholder that will get replaced by the path to the actual file you double-click on. (In general, Linux file managers allow you to specify this command line in the File info window.)

Is there any simple way to achieve this on OS X? I've tried tinkering with AppleScript, as well as Google around for a solution, but to no avail. I probably still need to build up my Apple-technology-related knowledge and vocabulary, so as to be able to ask the right questions with the right words — so please bear with me…

Best Answer

AppleScript is your friend.

In the AppleScript Editor, create a new script, with the following source code :

(* This script is destined to be saved as an application droplet. It will process the items dropped onto it. *)

-- This routine processes the dropped item(s)
on open theDroppedItems
  repeat with i from 1 to the count of theDroppedItems
    set aDroppedItem to item i of theDroppedItems
    processItem(aDroppedItem)
  end repeat
end open

-- This routine processes an item
on processItem(anItemToProcess)
  set pathUnix to POSIX path of anItemToProcess
  set pathUnixQuoted to quoted form of pathUnix
  -- Here you make your shell command !
  set commandToRun to "cat " & pathUnixQuoted
  do shell script commandToRun
end processItem

For the shell command, adapt the commandToRun to your desires.

Save the script. In the Save dialog :

  • Name the script OpenItems.
  • Place the script somewhere you like ; for example, you can place the script in /Applications.
  • Choose the format : Application.
  • Leave the two check boxes unticked.

In the source code, look at the construction on open theDroppedItems. This sort of AppleScript is called a droplet. Look at the icon of the application OpenItems you have saved. The big arrow on it shows that it is a droplet. It means that the application accepts item(s) dropped onto it.

Now, drag-and-drop the file you want onto the application OpenItems. This will launch the command with your file.

The next step is associating the file(s) with the droplet. Select the file you want, and press Apple I (  I ) to get info. In Open with, choose the application OpenItems. This will associate the file with the droplet. So, when you double-click the file, the file will be opened with the droplet OpenItems, and the shell command for the file will run. If you want this for all files of the same type, click the button Change all…

I have tested this solution on Mavericks. It works.