Calling JXA functions with named parameters

What am I missing in this tiny exercise?

In Script Editor's display of Safari's dictionary:

But in the shell:

=> Application("Safari")
>> app.searchTheWeb({for: "JXA"})
!! Error on line 1: Error: Parameter is missing.

Do I have the syntax for named parameters wrong?

In this pattern, the first argument is specifier (can be undefined in this case), followed by the key-value dictionary:

function run() {
    var sf = Application('Safari');
    sf.searchTheWeb(undefined, {for:'JXA'})

Thanks for sharing that, Rob.

It is very curious. Do you have an example of where specifier would not be undefined ?

Appears undocumented at first, but my guess is that there might have been an intention to allow specification of a search engine.

Choice of search engine seems unaffected by the value passed, though there may be a set of search engine identifiers somewhere.

This seems to make no difference:

function run() {
    var sf = Application('Safari');
    sf.searchTheWeb('DuckDuckGo', {for:'Nudnik'})

Returning to the topic, however, that pattern of one simple parameter followed by a key:value dictionary for further options is a fairly general pattern in the JavaScript for Automation interfaces.

Looked that way to me too. I suspected it was the equivalent of an AppleScript direct object, which is in fact called an “object specifier”, but I couldn’t make any sense of the specifier specification so I guess I just ignored it.

1 Like

Typically poor dictionary description on Apple’s part.  :frowning:


Well, it’s fun to try to figure it all out …

Easier than cuneiform, or the Indus valley script :slight_smile: