Need Some Help? Have a Tip to Share?

Email me at MacTipper@gmail.com, or, leave a comment!

Subscribe To Our FREE RSS Feed!

Don't miss out on any posts from the Concise MacTipper Blog.

Wednesday, May 27, 2009

Assign a URL to an AppleScript

Here's how you can make an AppleScript run when you browse to a URL beginning with your choice of string.

Step 1: In the AppleScript, add an "on open location the_url" section. This will allow the AppleScript to open the URL when you load it in Safari. The "the_url" identifier is the actual URL that is passed to the AppleScript. If you need information from the URL, you will need to parse it.

Step 2: Once you've added the open location handler, you need to save your script as an Application Bundle. To do this, hit Cmd-Shift-S, name your script, and set the File Format to "Application Bundle".

Step 3: Go to the newly saved script in Finder. Right click on the app and select "Show Package Contents" from the contextual menu. In the resulting Finder window, open ./Contents/Resources/info.plist in TextEdit.

Step 4: Scroll down to the bottom of the info.plist file and add the following code right above the "</dict>" line.

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>AppleScript Example URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myscript</string>
</array>
<key>LSIsAppleDefaultForScheme</key>
<true/>
</dict>
</array>



Step 5: Edit the "<string>AppleScript Example URL</string>" and "<string>myscript</string>" lines. The first is just a description line, the second is what you put at the beginning of the URL. So, in this example, you would use: "myscript://urlcontent" to run the script.

Step 6: Save the info.plist file using Cmd-S.

Step 7: Assuming that everything went smoothly, you should now be able to go to your preferred web browser and visit the URL you set up. This will launch the AppleScript and perform the actions you outlined in the "open location" handler.

If your URL doesn't work, you might want to try resetting LaunchServices. To do this, run the following terminal command:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

3 comments:

  1. Ok sounds good. Havent tried it yet - have a hypothesis I'd like to run by you.

    Using my sites folder I'd like to set up a PAGE.html with buttons.

    Buttons link to URL to trigger Applescript

    Linked URL redirects back to PAGE.html

    Theory is I can make a control panel for my apps. Just downloaded "Plainview" browser (free) goes fullscreen runs kiosk mode...

    End result - ideally running some type of blog via MAMP - I can make PAGE.html fairly dynamic and have a sweet poor-mans home theatre experience (on my sawtooth running tiger)... could include a go-live button to start internet connect and browse in frame, and thinking of some sort of stop/quit-revert-to-PAGE.html for DVD Player. I digress.

    Thoughts, simpler way?

    Thanks



    Mukura
    ReplyDelete
  2. Ok.

    Tested my hypothesis. Works fine.

    However - Step 4 - Could you clarify? I find two {/dict} lines at end of info.plist

    ALSO - Step 5

    with the strings edit/replace is a bit murky.. do you mean {string}myscriptaddress{/string} and/or {string}myscriptname{/string}

    OR... something else again

    Thanks.
    ReplyDelete
  3. This thing is working great for arbitrary schemes, but not for http and https.

    Do you have any idea how I can handle http:// urls by my script?
    ReplyDelete