Search code examples
cocoaapplescriptapplescript-objc

How to register a Cocoa-Applescript application as a URL handler? (equivalent of "on open location" in Applescript Studio)


Should be relatively straightforward, but I can't seem to find anything on this - I'm trying to add a URL handler to a Cocoa-Applescript application, as described here: http://www.macosxautomation.com/applescript/linktrigger/index.html

Except that example doesn't seem to work in an Xcode/Cocoa-Applescript application.

In my AppDelegate.applescript, after applicationDidFinishLaunching_ I've got:

 on open location this_URL
   tell me to log "this_URL: " & this_URL
 end open location

And I've added all the CFBundleURLTypes/CFBundleURLschemes stuff in info.plist.

The latter seems to be working, as my app activates when I click a myAppScheme:// link.

But the log statement doesn't fire.

I also tried stuff like on handleGetURLEvent_(this_URL) but I'm kind of just guessing at this point :)

Any help much appreciated..


Solution

  • Solved! (The 'magic number' below is due to a bug in ASObjc stuff).

      on applicationDidFinishLaunching_(aNotification)
      -- Insert code here to initialize your application before any files are opened
          -- Register the URL Handler stuff
          tell current application's NSAppleEventManager's sharedAppleEventManager() to setEventHandler_andSelector_forEventClass_andEventID_(me, "handleGetURLEvent:", current application's kInternetEventClass, current application's kAEGetURL)
    
      -- all your other application startup stuff..
    
      end applicationDidFinishLaunching_
    
    
      -- handler that runs when the URL is clicked
      on handleGetURLEvent_(ev)
          set urlString to (ev's paramDescriptorForKeyword_(7.57935405E+8)) as string
          tell me to log "urlString: " & urlString
    
          -- do stuff with 'Set AppleScript's text item delimiters to "foo="', and then "&" etc, to parse your parameters out of the URL String
    
      end handleGetURLEvent_
    

    And your info.plist needs to be in the form:

     <key>CFBundleIdentifier</key>
      <string>com.myappname</string>
    ...
      <key>CFBundleURLTypes</key>
    <array>
      <dict>
      <key>CFBundleURLName</key>
      <string>My App Name Helper</string>
      <key>CFBundleURLSchemes</key>
      <array>
      <string>myappscheme</string>
      </array>
      </dict>
    </array>
    

    Which allows your app to take input from URLs in the form:

    myappscheme://com.myappname/?foo=stuff&bar=otherstuff

    The whole URL is passed in and and ends up in your 'urlString' variable, so you can parse it however you like. Which means you don't even necessarily have to follow the standard URL conventions like I have above, but you might find it convenient to do so, so that potentially you can support parsing let's say Facebook or Google Maps URLs in your app with a simple change of http:// to myappscheme://

    Enjoy/Hope that helps.. :)