3

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..

dsmudger
  • 3,601
  • 1
  • 17
  • 18

3 Answers3

3

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.. :)

dsmudger
  • 3,601
  • 1
  • 17
  • 18
0

I don't use ApplescriptObjC, however in a normal cocoa application see this post which explains how to do it. I'd assume you need to do it like that.

Community
  • 1
  • 1
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Thanks and yeah stuff like that comes up in Google searches, but unfortunately I've no idea how to integrate that with an Applescript application :\ I tried adding an `on getURL_` method but that doesn't fire either - maybe I need to do the 'Register the URL handler method' bit, but no idea how to do that in Applescript.. – dsmudger Jan 30 '13 at 17:02
  • You definitely need the register part. That's what you're missing. Unfortunately I can't help you convert that code into ApplescriptObjC terminology... it can be done though and that's what you need. So ask a question specifically about that. Good luck. – regulus6633 Jan 30 '13 at 17:06
0

I may be running into something similar. I have an Applescript URL handler that launches but doesn't process the passed URL (just as you've described).

What I found was when another program sends a link, everything functions properly.
When I try to manually send a link via the terminal "open" command, the "on run" handler triggers rather than "on open location" - which unfortunately means I can't get the passed URL.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Could you post what you have so far? (a generalised version of, if you prefer). Quite keen to get this working so maybe I can bang on the keyboard a bit more and see if I can get a bit further. In particular curious about your on run handler and the bit where you say "What I found was when another program sends a link, everything functions properly." - sounds further than I got.. (I assume this is all in the XCode world rather than Applescript Studio btw..?) – dsmudger Feb 07 '13 at 19:14
  • I'm discussing it over at macscripter here: http://macscripter.net/viewtopic.php?pid=160028 . I'd post the code directly (and avoid future linkrot), but it far exceeds the character limitations. Oh, and this is straight up, plain-jane AppleScript - as ugly as that can be. – Joshua Ochs Feb 08 '13 at 06:25
  • Great, thanks, I may as well try some of those handlers in Cocoa-Applescript and see if I can get any of them to fire.. incidentally I posted the Cocoa-Applescript version of the question also on Macscripter, no replies so far :) http://macscripter.net/viewtopic.php?id=40260 – dsmudger Feb 08 '13 at 10:49