13

I want to add an option to the finder context menu that calls hg add %1 with %1 being the full path of the selected file in finder.
Of course there are more useful cases I can think of, to add to the context menu.
Is there a simple way to do that which doesn't involve installing any 3rd party software or coding in a compiled language and building binary plugins?
Like creating a script with the script editor and dropping it in /Library/Contextual Menu Items/ ?

studgeek
  • 14,272
  • 6
  • 84
  • 96
Petruza
  • 11,744
  • 25
  • 84
  • 136
  • 1
    Well... Open Automator, create a custom workflow. From `Library` choose `Utilites`, then drag and drop `Run shell script` to the workflow. Set `Pass input` to `As arguments`. Write in your script: `hg add $1`. Then File menu->Save a Plugin, specify the name, choose plugin for Finder, Save. Right click on the file, choose More->Autamator-> and enjoy. But there is nothing concerning programming here... – khachik Nov 18 '10 at 13:50
  • @khachik: It is still development-related. You should make your comment an answer. – Philip Regan Nov 18 '10 at 14:15
  • @Philip Ok, it is an answer actually... – khachik Nov 18 '10 at 14:22

3 Answers3

21

The steps have changed for Snow Leopard/10.6+ since @khachik's correct answer. To make sure its clear, here are the steps:

  • Open Automator
  • Create a new Service
  • Set the top two drop downs across the top to "Service receives selected files or folders in Finder.app"
  • Set Pass input to as arguments
  • Write your script (see below for that).
  • Save and choose the service name

Your Automator window should look like the this: :screenshot

You can now select multiple files in Finder and then execute your service from the Services sub-menu.

For your script, I think you want the following. This changes to each argument's directory then adds it. I'm using a for loop because Finder allows you to select multiple files in different folders (which could be in different repositories).

for f in "$@"
do
    cd $(dirname $f); hg add $f 
done

If you assumed they are all in the same repository you could do this:

cd $(dirname $1); hg add $@
studgeek
  • 14,272
  • 6
  • 84
  • 96
  • 2
    Automator runs the script in an environment that sets the path to what is defined in /etc/paths (and maybe something else) BUT it doesn't call your ~/.bash_profile ---> So in case you have some path defines needed to locate the script/program you want to execute from your Automator script your either have to use the full path or add "source ~/.bash_profile" (without quotes) as the first line to your script – georg Dec 19 '12 at 21:21
  • The `create service` option has been removed, and "Quick Actions" or other WorkFlows don't appear to show up in the Context Menu any more (even when placed in the `~/Lib/Services` folder). They only show up in the Services menu. Any idea how to make this work again? – Demis Apr 27 '22 at 03:57
15

Open Automator, create a custom workflow. From Library choose Utilites, then drag and drop Run shell script to the workflow. Set Pass input to As arguments. Write in your script: hg add $1. Then File menu->Save as a Plugin, specify the name, choose plugin for Finder, Save. Right click on the file, choose More->Autamator-><PLUGIN_NAME>.

khachik
  • 28,112
  • 9
  • 59
  • 94
  • Wow, great explanation. Although I couldn't find how to save as plugin, the only options I get are Workflow or Application. I'm running OS 10.6.4 – Petruza Nov 18 '10 at 14:34
  • There is no `Save as a Plugin...` item in `File` menu in Automator? I'm running leopard, and it is there... I believe there should be something equivalent even if it doesn't have `save as a plugin`. – khachik Nov 18 '10 at 14:38
  • 1
    Well, I've found that now in 10.6 it's called services, but it's the same. I managed to create it, works fine, thanks! now the problem is I need to `cd` to the %1 file's path, would you mind telling me how to get this? thanks! – Petruza Nov 18 '10 at 15:10
  • Mmm... (cd ``dirname "$1"`` && _do something_) ? – khachik Nov 18 '10 at 15:14
  • 3
    To get this to work in Snow Leopard you need to do New -> Service when starting automator, _not_ New -> Workflow. You can then use the "Service receives selected ____ in ____" control to bind the service to a specific context menu in a specific app. The rest works the same as in this answer. – aroth Mar 12 '12 at 04:24
  • @Petruza - I'm using the following to determine the path of a file/folder: `if [ -d "$1" ]; then DIR=$1 else DIR=\`dirname "$1"\` fi`. Then you can do `cd "$DIR"` to change into it. – aroth Mar 12 '12 at 04:27
  • where do I see the trecently added custom service using atomate – xkeshav Jan 06 '23 at 18:18
2

Yeah, I know this is third party software - but for the sake of a fuller overview - http://www.abracode.com/free/cmworkshop/on_my_command.html. Another tool that would make your script writing easier is http://wafflesoftware.net/thisservice/ adding items to the service menu.

Zygmunt
  • 281
  • 1
  • 6