10
votes

I am using OS X JavaScript for Automation (JXA), and I want to be able to capture the "open location" Apple Event.

Per http://www.macosxautomation.com/applescript/linktrigger/ , I have setup a customer URL handler. How do I do the equivalent of

on open location this_URL
  ...
end open location

with JXA? I tried all of the following, but could not get any of them to execute:

app = Application.currentApplication();
app.includeStandardAdditions = true;

function run() {
   app.displayDialog(JSON.stringify(arguments));
}

function openLocation() {
   app.displayDialog(JSON.stringify(arguments));
}

function openDocuments() {
   app.displayDialog(JSON.stringify(arguments));
}

function onOpenLocation() {
   app.displayDialog(JSON.stringify(arguments));
}

Apple's JXA docs (https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW15 ) don't discuss how to handle the open location event. My script would get opened because I could get an alert to display if I added it outside the functions. I just couldn't get a function to execute and be passed in the URL.

I am working around this by having a AppleScript handler that that then invokes my JXA code, but this is certainly less than ideal.

I also didn't see anything in the JXA Cookbook (https://github.com/dtinth/JXA-Cookbook ) about this.

1
Great question. I look forward to the answer.JMichaelTX
Is the omission of an argument in your function signature a copy paste error? Because it should be function openLocation(thisURL) { // process thisURL } if it is meant to work…kopischke
@kopischke: no, I intentionally omitted the URL parameter (e.g., thisURL). I am using the special "arguments" available to a JS function to determine whether my function was invoked. I believe I tried this with an explicit parameter as you suggested, but should it matter?big lep

1 Answers

0
votes

As you suggest, the trick (for the moment) seems to be to pass control immediately to a second (JavaScript for Automation) script in the same bundle.

on open location strURL
    run script (path to resource "jsHandler.scpt" in directory "Scripts") with parameters {{|URL|:strURL}}
end open location