Javascript for Automation custom URL in Yosemite

415 views Asked by At

I'm trying to make a script, saved as an application bundle, that handle custom URL schemes.

I know how to handle it in Applescript:

on open location theURLtext
    do things here
end open location

But I don't know how to do it with javascript, there's not much documentation about JS for automation yet.

Thank you and excuse my bad english.

2

There are 2 answers

0
John Christopher Jones On

The equivalent to the open location handler is to define a function with a specific name.

function onOpenLocation (theUrl) {
    Do something here.
}

The release notes for JavaScript for Automation do not specifically mention this handler, but this is what it should be called based on the naming convention for translating AppleScript to JSA.

0
houthakker On

Well, openLocation() would be more consistent with the other handler names, but I don't think they have implemented it yet.

You can, however, make use of JavaScript for Automation's much better url processing functions by just passing control straight to another script in the .app bundle:

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

Where jsHandler.scpt begins work with something like:

function run(argv) {
    var strURL = argv[0].URL;
    // ...
}