4

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...

  • What command do you want to apply to the file ? – Nicolas Barbulesco Jun 21 '14 at 13:06
  • Do you want to handle a given file, or all files having a given suffix ? – Nicolas Barbulesco Jun 21 '14 at 13:06
  • When I double-click a file in Finder, I want the given command line to be executed with the double-clicked file as argument. E.g. if I click foo.txt in my home folder, I want emacsclient -c -a "" ~/foo.txt to be run. But I think I've found a solution I'm happy with using Automator workflows (see my second comment to Stephen S's answer) -- except for that annoying spinning gear wheel indicating a running workflow in the tray, that is. Thanks though! – dlukes Jun 21 '14 at 13:14
  • Btw if you want to associate shell script with a custom url scheme, have a look at Lincastor – Sergei Jun 22 '14 at 11:07
1

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.

  • 1
    Thanks for the extensive worked out example in AppleScript, it definitely helped me to broaden my horizons (didn't know about droplets before)! I already have a slightly different solution using Automator workflows, but this one may be cleaner/simpler, so I'll investigate it as well. – dlukes Jun 22 '14 at 15:59
1

To my knowledge, there is nothing to automatically run any script on double-script (I am most likely wrong). The best solution I've found is Automator. It's preinstalled. You want to create a service, which will place scripts into a context menu (right-clicking on file). For your actions, you want to find the action "Run Shell Script". From there, you can have it receive input from Finder, such as file paths and directory paths. If you pass input to stdin, the variable you will use in the shell script is $@.

if [[ ! -d "$@" ]]
then
    emacsclient -c -a "" $@;
    exit
fi

I do not know if that script is entirely correct, but it should put on you the right track.

  • 1
    Thanks, that's miles closer than I ever got! Still, I'll wait a bit before marking the answer as accepted, to see if anyone comes up with a solution as originally outlined. I'd love to upvote, but unfortunately, I don't have enough rep... :( – dlukes Jun 20 '14 at 20:28
  • 1
    Actually, you were really close! I managed to cobble together a solution using Automator and Run Shell Script, but saving the workflow as an Application with the "Pass input: As arguments" option, as described in the answer to this post. Want to add this to your solution so I can mark it as accepted? If you don't, I'll post an answer of my own along with the link. – dlukes Jun 21 '14 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .