Login items using AppleScript (on Sonoma)

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 1064
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Login items using AppleScript (on Sonoma)

Post by trevix » Thu Mar 27, 2025 12:56 pm

Hello.

Having created a small standalone that installs in the MacStatusMenu, I would like, on first launch, to place the standalone in the "login item" of the OSX.
In order to do that, I found on the web the following AppleScript that, running on the script editor, returns the following error:

Code: Select all

login item "UNKNOWN" of application "System Events"
This is the script:

Code: Select all

try
	set appName to name of (choose application)
	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
Is the code wrong or is there something on OSX Sonoma that doesn't fit anymore with this script?

Thanks
Trevix
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

trevix
Posts: 1064
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Login items using AppleScript (on Sonoma)

Post by trevix » Thu Mar 27, 2025 4:54 pm

I refined the code.
No more error but the application name is not added to the OS Login items list.

Code: Select all

set appPath to "/Users/trevix/Desktop/StickyFinder.app"
set appName to "StickyFinder"

tell application "System Events"
	--return appName & " " & appPath
	-- Check if the app is already in login items
	set existingItems to get name of every login item
	if appName is not in existingItems then
		set appName to appName & ".app"
		make new login item at end with properties {name:appName, path:appPath, hidden:false}
	end if
end tell
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

trevix
Posts: 1064
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Login items using AppleScript (on Sonoma) SOLVED

Post by trevix » Thu Mar 27, 2025 11:19 pm

Above works (I was just using a wrong path :roll: )
Trevix
OSX 14.6.1 xCode 15 LC 10 RC1 iOS 15> Android 7>

Post Reply