Adding and deleting login items using AppleScript
It’s a fairly simple process to script adding and deleting login items using applescript. You can save these scripts as applications and send them out to users. Then they can run it themselves and add or remove a login item. You can have applications open on login or volumes mount on login.
Adding a login item
For adding a login item we’ll use the example of mounting a remote volume. I usually find that it works best to actually mount the item first and then add the login item. For this example we’ll assume we’re connecting to an SMB share named “home” on a Windows server.
[codesyntax lang=”applescript” lines=”no”]
mount volume “smb://server.example.com/home”
tell application “Finder”
--check to see if the volume mounted
if disk “home” exists then
display dialog “Success”
--this “tell” statement makes the actual login item
tell application “System Events”
make login item at end with properties {path:”/Volumes/home”, kind:volume}
end tell
else
--if it doesn’t work let the user know
display dialog “Oops! This problem happened:” & return & errmsg
end if
end tell
[/codesyntax]
So, in the example above we mount the server share first. Then, we check to see if it’s actually there. If so we tell the user we’ve succeeded with a dialog box and make the login item. If not we let them know there was a problem and display the error message so they can tell tech support.
Notice the “kind” section of that line. You need to tell it what you’re adding (a volume, an application, etc.).
Deleting a login item
Deleting a login item is even easier.
[codesyntax lang=”applescript” lines=”no”]
tell application “System Events”
--Find out what login items we have
get the name of every login item
--see if the item we want exists. If so then delete it
if login item “home” exists then
delete login item “home”
else
display dialog “That login item doesn’t exist”
end if
end tell
[/codesyntax]
We check to see what login items we have. If the one we want to delete exists we go ahead and nuke it.
admin :: Jun.29.2007 :: Applescript, Scripts :: 8 Comments »
8 Responses to “Adding and deleting login items using AppleScript”
Thanks for the example, exactly what I was looking for.
Thanks for the tip.
I too was looking for this.
Ok so the problem is probably not with your coding but with my lack of programming experience but either way i can not seem to get this to work. I am attempting to add a script to my login items that will unmount a volume called .Secondary . I want it to be as far removed from the normal functioning of the computer as i can make it, but people keep telling me that this is not possible due to the partitions location on my internal drive.
I would really appreciate any recommendations!
thanks 🙂
You’re on the right track but this script won’t do it for you. You need to use something like “disktutil” at the command line.
My suggestion is open Terminal and type “disktutil list” and get the path to the volume you want to unmount. It will probably be something like /dev/disk1s2.
The do a one-line applescript like this:
do shell script “/usr/sbin/diskutil unmount /dev/disk1s2”
Save that as a script or application and add it to your login items.
This is great, but I would like a script to add the currently running app to my login items. Can this be done? Thanks so much!
Is there any way I can get System Events to add/remove a login item for all the users? I can add it using “defaults write /Library/Preferences/loginwindow …” but there is no easy solution to remove it. Any ideas?
[…] can search online to find samples of more comprehensive AppleScript SMB scripts that include error checking, conditional logic, and so […]
Thanks for this example! I don’t know AppleScript, but from looking at your example + Googling for a couple of other commands, I was able to write what I needed. This was really helpful, thanks again. (And in particular, thanks for commenting your code! That made it much easier to figure out what I needed to do.)