I want to open a GUI application (most likely Electron based) periodically on a Mac Book. The period will be configured (daily, hourly etc..) within the application. How would I go about achieving this?
There are numerous approaches you can take.
launchd or Third Party Launcher
Power Manager includes a task covering How to Schedule an Application to Launch on Mac OS X.
You probably want to avoid
cron because it does not operate in the current user's graphical session.
cron is good for command line tools and scripts, but less well suited to graphical Mac applications.
AppleScript and Calendar
A simple AppleScript application combined with a calendar event's Open File as an alarm may be enough. The AppleScript snippet below will open a specified application and bring it to the front:
tell application "My Electron Application" to activate
These related questions go into specifics:
Launchd .plist file
You can create a
plist file that will describe the job to be carried out by
launchd at intervals you can define in the plist. There's some terminology that is easiest to learn by just reading another plist file, such as the one I've written below.
Once you've done that, you can move the plist file into
~/Library/LaunchAgents, which will run the job under your username.
Finally, get the
launchd daemon to
load your configuration file, which will start the job:
launchctl load chri.sk.launchd.plist
From then on, the job specified in the plist file will be carried out at the defined intervals, and will continue being carried out forever, or until you
launchctl unload chri.sk.launchd.plist
It will launch automatically on every system reboot, and re-launch if it is ever killed off.
<?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>chri.sk.launchd</string> <key>ProgramArguments</key> <array> <string>/usr/bin/open</string> <string>-a</string> <string>TextEdit</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>Nice</key> <integer>1</integer> <key>StartInterval</key> <integer>60</integer> <key>StandardErrorPath</key> <string>/Users/CK/Documents/launchd.err.txt</string> <key>StandardOutPath</key> <string>/dev/null</string> </dict> </plist>
This opens up TextEdit every 60 seconds. Here's a pretty useful guide on defining intervals.
Any errors are written to the file path specified by
Make the filename and the
label value the same. Apple's naming convention recommends using a reverse hostname identifier, but in the absence of a domain name that belongs to you, you could simply use something like
launchd.plist man page defines all the property list keys and explains what they each do. Go to Terminal and type
property hour : 3600 property day : 86400 property RunEvery : day property AppName : "An Application" on run idle end run on idle tell application named AppName to activate return RunEvery end idle on quit continue quit end quit
If you save this AppleScript as a stay-open application, and then run it, it will stay open and run the
idle commands every day. Currently, to stop this script from continuing, you'd have to terminate it from the dock or Activity Monitor.