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

1

8

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?

Cynic answered 12/11, 2014 at 13:48 Comment(3)
I get the same result using Apple's sample snippet from the doc page (under "Scripting Additions") if I replace .currentApplication() (ie Script Editor) with anything else. I assume this is intentional (can't cause another app to "say" stuff?) but wish it were documented with rationale.Emden
If that were the case, the dialog and alert should be similarly restricted. "Mail Requires Your Password" as a dialog is far more dangerous.Signesignet
Part of the comment by @BenZotto gives the practical answer: User Application.currentApplication() instead of another application to run displayNotification().Signesignet
N
1

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.

Nievelt answered 16/3, 2015 at 2:21 Comment(1)
Why does it work for app.displayAlert() but not for app.displayNotification()?Cynic

© 2022 - 2024 — McMap. All rights reserved.