Javascript for Automation (OSA) Yosemite: privilege error for certain StandardAddition commands

1.6k views Asked by At

In Yosemite it now is possible to use JavaScript for automation as well as Applescript. I am having trouble with certain StandardAdditions commands. E.g. from the Contacts application I can use displayAlert, but not displayNotification. Both are in the StandardsAdditions dictionary. When running these commands through the ScriptEditor I don't get these issues.

For the commands that fail I get at runtime: Error -10004: A privilege violation occurred.

Example code in JavaScript:

ScriptEditor = Application("Script Editor");
ScriptEditor.includeStandardAdditions = true;

app = Application("Contacts"); // or e.g. "Calendar", "System Events", "Finder"
app.includeStandardAdditions = true;

// -- testing: displayAlert()
ScriptEditor.displayAlert("Hello world!"); 
app.displayAlert("Hello world!"); // success, no privilege error

// -- testing: displayNotification()
ScriptEditor.displayNotification("Hello world!"); 
//app.displayNotification("Hello world!"); // Error -10004: A privilege violation occurred.

// --- testing: say()
ScriptEditor.say("Hello world!");
//app.say("Hello world"); // Error -10004: A privilege violation occurred.

// --- testing: beep()
ScriptEditor.beep(1);
//app.beep(1); // Error -10004: A privilege violation occurred.

When using the equivalent code in AppleScript I do not get privilege violation errors:

tell application "Script Editor" to display alert "from Script Editor" -- with Script Editor icon
tell application "Contacts" to display alert "from contacts" -- with Contacts icon

tell application "Script Editor" to display notification "from Script Editor" -- with Script Editor icon
tell application "Contacts" to display notification "from contacts" -- with Script Editor icon (!)

What I notice in that case though is that the Contacts alert shows up with the Contacts icon (and Contacts app is activated), but the Contacts notification shows up with the Script Editor icon (and the Contacts app is not activated).

Using Yosemite 10.10. Is this a bug or am I missing something?

1

There are 1 answers

1
William T Froggard On

The reason is that AppleScript is using inheritance. You can tell any application to "display notification", but the call ends up getting passed up the hierarchy to Script Editor (or a script applet) which understands the message. The JavaScript implementation doesn't support inheritance to my knowledge. I'm not really well-versed in the JavaScript side of the OSA world. :)

tl;dr: Contacts can't actually do what you're trying to make it do, it just works in AppleScript because AppleScript is that amazing. :)

If you look at the replies log in Script Editor, you can see the inheritance happen live.