reitermarkus reitermarkus - 2 months ago 17
AppleScript Question

Application.make in JavaScript for Automation (JXA)

I'm trying to convert this existing AppleScript script:

tell application "System Events"
make login item with properties {path:"/Applications/Application.app", hidden:false"}
end tell
return


Here is what I have so far:

#!/usr/bin/env osascript -l JavaScript

'use strict';

ObjC.import('stdlib')

function run(argv) {

var systemEvents = Application('System Events')

systemEvents.make({
new: 'LoginItem',
at: systemEvents.loginItems,
withProperties: {
name: "Application",
path: "/Applications/Application.app",
hidden: false,
kind: "Programm"
}
})

$.exit(0)
}


I am stuck at the following line however, as I don't actually know what to pass to the
new:
property, I think the other properties are correct.

new: 'LoginItem',


I hope someone can point me in the right direction.

Answer

Make new doesn't work in JXA.

Use theApp.TheClassName({somePropertyName: "someValue"}) to create object.

After that, use theApp.theClassNameWith_s.push(theObject) to append the object to the array

Example:

var systemEvents = Application('System Events')
logItemObj = systemEvents.LoginItem({path: "/Applications/Preview.app", hidden: false})
systemEvents.loginItems.push(logItemObj)
  • Don't use kind and name, these properties are read only.
Comments