(OSX) task to add app to Login Items

I have an app on our macs and noticed that the software is not automatically adding itself to login items on installation. I was trying to make a small script that fixes this. I managed to get it to work when running it directly from the computer but BigFix task is not running it properly (Exit Code is 1 and nothing is added into the Login Items).

Script:

osascript -e ‘tell application “System Events” to make login item at end with properties {path:"/Applications/MySoftware.app", hidden:false, name:“MySoftware”}’

I tried to make the task as sh and AppleScript with no luck. Script itself is not asking for any admin credentials when I run it directly from the computer so I assume the issue is not credentials when trying to run the task from Console.

My test laptop currently runs Yosemite.

Are you specifying a user? Remember the agent runs as root so you would need to do this to a specific user to make it apply to their login so not sure you can do this with this script.

I’m also not sure you have the complete AppleScript. This is my understanding but maybe it defaults?

tell application "System Events"
 if (login item "MySoftware" exists) is false then make new login item at end of login items with properties {path:"/Applications/MySoftware.app", hidden:true, kind:application, name:"MySoftware"}
end tell

You are propably right. Any idea how I could make it run to a specific user?

edit: for example the user who is currently logged on.

I’ll credit @atlauren for this one as its easy on the Mac to do this

wait /usr/bin/su -l {name of current user} -c “the other stuff"

I’d be leery of using osascript -e in this case, because the actual AppleScript will be in quote-escaping hell. Instead, prefetch a .scpt file, and then:

wait sh -l {name of current user} -c "/usr/bin/osascript __Download\file.scpt"

Alternatively, drop in a launchd plist file with a ‘RunAtLoad’ trigger. That way it’ll run at each user login, and it won’t be exposed in the Users & Groups control panel.

Thanks for the help everyone. I managed to run the script by zipping it and curling it directly to the laptop and running chmod 775 on the .sh file and the directory. Seems to work so I’ll leave it to that. Cheers!