Copy a script to Login Items using AppleScript and removing once done

Hi all,

I’m trying to create a script that will run through a software installation which requires a system restart mid way. What I need to figure out is how to copy a second script to Login Items so it runs automatically when OS X logs in again and is also removed once completed.

The second script will be located on the users desktop (/Macintosh HD/user/Desktop/Packages/Software/applicationName/script/secondScript) but I will need to use the POSIX path of (path to desktop) to allow for different usernames on multiple machines.

I hope this makes some sort of sense!

Thanks

Will

Something like this might work

set thePath to POSIX path of (path to desktop)
        tell application "System Events"
            make login item at end with properties {path:thePath, hidden:false}
        end tell

Since the new login item is created at the bottom of the list you can delete it with :

tell application "System Events"
	delete login item -1
end tell

This is not fool proof. If the order changes you will delete another login item… There’s probably a better way.

I’d like to know how to check for the correct one to delete. I would think using a while loop and check the names of the login items until the right one is found.

Thanks for the reply.

Your add a Login Item script seems to be exactly what I need. I did some research after posting this thread and put together this which works but I can’t seem to get it to add an application from the desktop. Any ideas? I’ll try combining the POSIX line to my script and see if I can get it going that way.

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

To delete a Login Item I found this:

tell application "System Events"
	get the name of every login item
	if login item "nameOfItem" exists then
		delete login item "nameOfItem"
	else
		display dialog "That login item doesn't exist"
	end if
end tell

Combined the two scripts and it’s now working perfectly :slight_smile:

set thePath to POSIX path of (path to desktop) & "Packages/Software/applicationName/TestApp.app"
tell application "System Events"
	make login item at end with properties {path:thePath, hidden:false, kind:application, name:"TestApp"}
end tell