Create bash script to open URL in Mac OS X

automatorosx lion

I want to OS X to intelligently open git URLs by first trying to open their repo page on GitHub, and then falling back to something like GitBox.app.

I found this question extremely helpful, and I created an Automator app to wrap a bash script (which does all of the intelligent stuff), and used RCDefaultApp to set OS X to use my automator app to open git:// URLs.

This didn't work, so I tried some debugging. I set my bash script to output its arguments to /tmp/output.txt, and it turns out that the script isn't getting any command line arguments. If I set OS X to use this same automator app as the default app for *.txt files, the bash script correctly gets the path of the file as the first argument, but it doesn't work with URLs. Any idea how to get this to work?

Also, I'm running 10.7.

Edit: Here's a snapshot of the Automator app:
Automator app

And here's the text of that simple bash script (not what I would actually use to open the git:// URLs, but it demonstrates the lack of arguments:

rm -f /tmp/output.txt
echo $0 >> /tmp/output.txt
echo $* >> /tmp/output.txt

And the only output I get in /tmp/output.txt is:

-

Best Answer

It doesn't work like that, because OS X doesn't treat files, folders, and URLs, which are just command line arguments to the associated programs on other systems, like other platforms. Automator programs really only can handle files and folders.


You need to create an AppleScript based application, which responds to open location.

Open AppleScript Editor, and paste the following code (changing the script of course):

on open location myURL
    do shell script "echo " & myURL & " > /Users/danielbeck/test"
end open location

Save as application. Then select the application bundle you just created, right-click, Show Package Contents, and edit Contents/Info.plist using a text editor (after converting to XML using plutil on the command line, if it's binary), or the default editor which is part of Apple's Developer Tools.

Add the following to its top-level structure (the screenshot is how it looks like in current Xcode, the XML is what you'd add in a text editor):

enter image description here

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>openme</string>
        </array>
        <key>CFBundleURLName</key>
        <string>AppleScript Testing URL</string>
    </dict>
</array>

That will associate openme:// URLs with that application. Save, move the program to a different folder and back to update Launch Services, and test it by typing an openme:// URL into a web browser's address bar:

enter image description here

You'll want to replace openme by git and the echo call by your shell script, of course.

Related Question