JXA: Cannot access defaultUserNotificationCenter if osacompile is used

Originator:me
Number:rdar://21400867 Date Originated:2015-06-16
Status:Open Resolved:
Product:OS X Product Version:10.11 (15A178w)
Classification: Reproducible:Always
 
Summary:
To send notifications, the defaultUserNotificationCenter must be accessed. Attempting to access it with $.NSUserNotificationCenter.defaultUserNotificationCenter returns null if you compile the script using osacompile -l JavaScript.

However, if you use the Script Editor app–with language set to JavaScript–$.NSUserNotificationCenter.defaultUserNotificationCenter behaves normally, doesn't return null

Steps to Reproduce:
1. Create a script (main.js)
2. Attempt to access notification center; $.NSLog($.NSUserNotificationCenter.defaultUserNotificationCenter)
3. Compile the script to an application; osacompile -l JavaScript -s -o example.app main.js
4. View log with Console.app; result will be (null)

Expected Results:
$.NSUserNotificationCenter.defaultUserNotificationCenter should return the same value for both compiling scripts with osacompile -l JavaScript and compiling scripts using Script Editor app

Actual Results:
$.NSLog($.NSUserNotificationCenter.defaultUserNotificationCenter) returns null if the script is compiled using osacompile.

Version:
10.11 (15A178w)

Attached files:
https://dl.dropboxusercontent.com/u/271215/defaultnotificationcenterexamples.zip

Comments

Changed Issue

Based off the response from Apple, I updated the issue to be "osacompile should allow a flag for setting a bundle identifier". I suggested something like:

$ osacompile -l JavaScript -s -i "foo.bar" -o example.app main.js

Apple response

I received the following response on my bug report:


This issue behaves as intended based on the following:

The issue here is that osacompile doesn't set a bundle identifier, and NSUserNotificationCenter requires the applet to have a bundle identifier.

You can build a stay-open applet and set your own bundle identifier ("foo.bar", in this example) like so:

$ osacompile -l JavaScript -s -o example.app main.js $ /usr/libexec/PlistBuddy -c 'Add :CFBundleIdentifier string "foo.bar"' example.app/Contents/Info.plist

If you would like bundle-identifier-setting to be a feature of osacompile, please update your bug report with that information.

Please update your bug report to let us know if this is still an issue for you.



Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!