Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As you are mentioning AppleScript, I suppose you are working on Mac OS X.</p> <p>A simple way to register and use a custom URL scheme is to define the scheme in your .plist:</p> <pre><code>&lt;key&gt;CFBundleURLTypes&lt;/key&gt; &lt;array&gt; &lt;dict&gt; &lt;key&gt;CFBundleURLName&lt;/key&gt; &lt;string&gt;URLHandlerTestApp&lt;/string&gt; &lt;key&gt;CFBundleURLSchemes&lt;/key&gt; &lt;array&gt; &lt;string&gt;urlHandlerTestApp&lt;/string&gt; &lt;/array&gt; &lt;/dict&gt; &lt;/array&gt; </code></pre> <p>To register the scheme, put this in your AppDelegate's initialization:</p> <pre><code>[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; </code></pre> <p>Whenever your application gets activated via URL scheme, the defined selector gets called.</p> <p>A stub for the event-handling method, that shows how to get the URL string:</p> <pre><code>- (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent { NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; NSLog(@"%@", url); } </code></pre> <p>Apple's documentation: <a href="http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_handle_AEs/SAppsHandleAEs.html#//apple_ref/doc/uid/20001239-SW10" rel="noreferrer">Installing a Get URL Handler</a></p> <p><strong>Update</strong> I just noticed a problem for sandboxed apps that install the event handler in <code>applicationDidFinishLaunching:</code>. With enabled sandboxing, the handler method doesn't get called when the app is launched by clicking a URL that uses the custom scheme. By installing the handler a bit earlier, in <code>applicationWillFinishLaunching:</code>, the method gets called as expected:</p> <pre><code>- (void)applicationWillFinishLaunching:(NSNotification *)aNotification { [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(handleURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; } - (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent { NSString* url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; NSLog(@"%@", url); } </code></pre> <hr> <p>On the iPhone, the easiest way to handle URL-scheme activation is, to implement UIApplicationDelegate's <code>application:handleOpenURL:</code> - <a href="http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleOpenURL:" rel="noreferrer">Documentation</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload