Hi @josh welcome to the forum!
I’ve never done this before, but it should be possible from Python. When trying to do something like this it’s often a good idea to see how you would go about doing it from the command line.
If you search for adding a startup application from the Terminal you can find this answer on Stack Exchange. That describes launch daemons (start at boot) and launch agents (start at login) and gives instructions for the former, but it’s the latter you want for user applications. Rather than add these to /System/Library
they need to go in the per-user library folder, under /Users/<username>/Library/LaunchAgents
.
If you have an existing .plist
you can copy and edit that, or just use the following. This is a .plist
which will run the Moonsweeper example app installed in the /Applications
folder.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.learnpyqt.moonsweeper</string>
<key>LimitLoadToSessionType</key>
<string>Aqua</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Moonsweeper.app/Contents/MacOS/Moonsweeper</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>
Save this in the users launch agent folder, e.g. as ~/Library/LaunchAgents/com.learnpyqt.moonsweeper
and it will launch the specified app at startup.
fyi – If your application isn’t signed, this might not work. For development you can disable Gatekeeper with sudo spctl --master-disable
– fine for development, but not something for your users.
How to do this from an application?
If you want to make it possible for your users to toggle “Run at startup” in the interface, the approach is identical to what’s described here – you just just need to write/delete the file from within your app. Make sure you’re writing to the user’s ~/Library
folder, not the system one (you’d need permissions for that).
It doesn’t actually matter what you use for the filename for the .plist
but its a good idea to keep this consistent with your application namespace. That way you know only you would be writing the file, so can delete it if it’s there without any worries.