Add application to login items

Sample syntax to add an item to login items

OS version: OS X

set appPath to POSIX path of alias "path:to:app"
tell application "System Events"
	make login item at end with properties {path:appPath, hidden:false}
end tell

Applies only to 10.5 and up I believe… but really usefull.

I have created this simple universal routine that I call everytime any of my Apps are started. I use it in applescript studio, although I think it could be easily adapted to a standard script made with Script Editor:


on checkIfAppIsInLoginItems(appName, pathOfApp)
	try
		tell application "System Events" to set allLoginItems to name of every login item as string
		if appName is in allLoginItems then
			--do nothing
		else
			set appPath to POSIX path of alias pathOfApp
			tell application "System Events"
				make login item at end with properties {path:appPath, hidden:false}
			end tell
		end if
	on error the error_message number the error_number
		display alert "Error!" as warning message ("An error occured!" & return & return & error_number & " " & error_message) as text
	end try
end checkIfAppIsInLoginItems

where appName would be your app’s name as it would appear in the list of login items, and pathOfApp is the path (finder kind, not posix).

I then use these two commands in the “on launched” handler to check if my app is in the login items already:


set my_path to (path to me as string)
my checkIfAppIsInLoginItems("My app name", my_path)

I use PackageMaker to create an installer package, and in the postinstall steps I use the “open file” to run the app, thus I am sure it is in the login items when it is installed.

Hope this is usefull to someone!

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

A few developers offer free objc source to do this, however wincent who makes Synergy has a very comprehensive opensource library to handle nearly everything for adding to login items

Interesting. Do you have links to wincent’s website? I cant find it using google…

Thanks for script. It became useful, when I make some corrections:

  1. No need pass 2-nd parameter (pathOfApp) as the script automatically retrieves it from app name.
  2. No need convert path to Posix path, as the System Events works with alias paths fine.
  3. No need coerce allLoginItems list to string.
  4. You can tell to System Events once.

set appName to name of (choose application)
set appPath to (path to application appName)

tell application "System Events"
	try
		set loginItems to name of every login item
	on error
		set loginItems to {}
	end try
	if appName is not in loginItems then make login item at end with properties {path:appPath, hidden:false}
end tell

In a handler form:


on addAppToLoginItemsAfterCheckingInOnes(appName)
	
	try
		set appPath to (path to application appName)
	on error
		display notification "Application with this name NOT founded" sound name "Frog"
		return
	end try
	
	tell application "System Events"
		try
			set allLoginItems to name of every login item as string
		on error
			set allLoginItems to {}
		end try
		if appName is not in allLoginItems then tell application "System Events" to make login item at end with properties {path:appPath, hidden:false}
	end tell
	
end addAppToLoginItemsAfterCheckingInOnes