up vote 1 down vote favorite
share [g+] share [fb]

I'm coming from Linux where it's easy to run a script against a set of files in nautilus or dolphin, but I was kind of surprised to see that this isn't easy in OSX.

Preferably, I would love to be able to ctrl-click on some selection in finder, choose my script, and have a terminal window pop up to show progress or any user interaction.

link|improve this question

60% accept rate
feedback

3 Answers

up vote 3 down vote accepted

The easiest, built-in way to do this is to just drag the items from Finder to a Terminal window. When you drop them, Terminal will “type” their (escaped) pathnames. You can then use your shell's command line editing to enter the rest of the command. Or if you are thinking ahead, you can type the command first then drag in the items.

The “drag first” version:

  1. Select and drag some Finder items to a Terminal window that is showing a shell prompt. Terminal will insert the (escaped) pathnames.
  2. Type Control+A (go to the beginning of the line; ⌃A is the default, but varies by shell/configuration).
  3. Type your command and a space (it will be inserted before the pathnames).

The “thinking ahead” version:

  1. In any order, do both:
    • In a Terminal window, type the initial part of your command and space (everything except the pathnames).
    • Select some items in Finder.
  2. Drag the selected items from Finder to the Terminal window where you typed the command.
  3. Activate the Terminal window and press Return.

You can (as other answers have indicated) turn to AppleScript or Automator if you need more a more automated solution. Since the extra “moving parts” can add some complexity to the solution, you will have to weigh the benefits of the above process (manual, but flexible) against the the complexity of implementing one or more of the automated solutions.

Some of the automation possibilities include:

  • an AppleScript droplet application
  • a plain AppleScript program launched by the built-in script menu or other launcher
    • get pathnames from Finder's selection
  • an Automator workflow, application, or Finder plug-in
  • an executable .command file (usually a shell script; open in Finder to run it in Terminal)
    • get pathnames from Finder's selection (via osascript)
  • some third-party tool that already provides the functionality you need
link|improve this answer
feedback

In Mac OS X, the preferred script language is Applescript. You can create a droplet (a script that will do something when you drop some files over its icon), place the script (which can be saved as application) icon over the desktop, and drag and drop the files you want to pass to the script.

As alternative, you can use folder actions. It is possible to associate a script Applescript to a folder that will be executed all the times some files (or directories) are dropped over the folder.

link|improve this answer
feedback

I don't think there is a way to do this directly. You can build an "Automator" application that will do some of what you are seeking. This shows what the Automator stack would look like:

enter image description here

Save the result as an Application, then in the finder select the files, right-click and select "Open with" then pick your application. (You may need to select "Other..." at the bottom of the application list.)

You don't get to interact with the script with this method, but you can loop over the files to perform some shell script.

link|improve this answer
It's even easier if you drag the files over the application icon, and drop them. – kiamlaluno Jun 21 '10 at 1:17
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.