MacOS – Auto run a UI app and click a button on startup

macmacosstartup

I want to do two things when my Mac boots up:

1) Run an app
2) Click a button on that app

Any suggestions for how I can accomplish this?
FWIW, this is app I haven't written so I can't change source or pass in command line params.

Best Answer

Save a script like this as ~/Library/Scripts/test.scpt in AppleScript Editor:

tell application "Grapher"
    activate
    reopen
end tell
tell application "System Events" to tell process "Grapher"
    click radio button 2 of tab group 1 of window 1
    -- UI elements of window 1
end tell

Save this as ~/Library/LaunchAgents/test.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Users/username/Library/Scripts/test.scpt</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

More information about UI scripting and launchd: