Wait before the next action
Pause the script for a given number of seconds.
-- wait 10 seconds before the next action
delay 10
A handful of useful scripts for automated startup and stand-alone Mac installations.
Snippets and small scripts collected over years of running Macs as stand-alone installation hosts. Pick what you need, copy, adapt.
If you've ever set up a Mac mini to run an exhibition for six months, you've written scripts like these. Auto-launch on login. Hide the cursor. Restart on a schedule. Reconnect MIDI on boot.
We collected the lot. They're documented, simple, and battle-tested in galleries and showrooms across Europe.
One-liners and short snippets to drop into your scripts.
Pause the script for a given number of seconds.
-- wait 10 seconds before the next action
delay 10
Value goes from 0 to 100.
-- change the sound output (from 0 to 100)
set volume output volume 100
tell application "AppName" to activate
tell application "Finder" to open POSIX file "/Path/to/file.ext"
Alternative way using the shell.
do shell script "open /Path/to/file.ext"
tell application "System Events"
set visible of process "ProcessName" to false
end tell
Instant shutdown, ignores app responses.
ignoring application responses
tell application "Finder"
shut down
end tell
end ignoring
Replace $user and $password with actual credentials.
do shell script "reboot" user name "$user" password "$password" with administrator privileges
tell application "System Events" to tell process "ProcessName"
perform action "AXRaise" of window 1
end tell
Multi-step scripts for installation setups.
Tries to connect two Macs via Audio MIDI Setup, retrying every 10 seconds until the other host responds.
-- this will try to connect two computers with the Audio MIDI Setup utility.
-- it will keep trying until the other computer has been detected and connected.
repeat
tell application "Audio MIDI Setup" to activate
try
tell application "System Events" to tell process "Audio MIDI Setup"
select row 1 of table 1 of scroll area 1 of group 2 of window "MIDI Network Setup"
click button "Connect" of group 2 of window "MIDI Network Setup"
end tell
exit repeat
return
on error
delay 10
end try
end repeat
PJLink reference: pjlink.jbmia.or.jp · commands PDF. Does not work on password-protected projectors.
set theIp to "127.0.0.1" -- the projector ip
set theMessage to "POWR 1" -- no need for the %1 prefix nor the (rc) at end of line
-- no need to edit anything below
do shell script "echo \"%1" & theMessage & "\\r\" | nc " & theIp & " 4352"
Export these as applications with the option Stay open after run handler enabled.
Adjust the 3-second delay if your app takes longer to quit.
on idle
set theApp to "AppName"
tell application theApp to quit
delay 3
tell application theApp to activate
return 2700
end idle
Checks every 10 seconds. To prevent the crash reporter from appearing: defaults write com.apple.CrashReporter DialogType none
on idle
try
tell application "System Events"
if not (exists process "ProcessName") then
-- open a file
tell application "Finder" to open POSIX file "/Path/to/file.ext"
-- or open an app
-- tell application "AppName" to activate
end if
end tell
end try
return 10
end idle
Cleaner syntax for modern macOS versions.
on idle
try
if application "AppName" is not running then
tell application "Finder" to open POSIX file "/Path/to/file.ext"
-- tell application "AppName" to activate
end if
end try
return 10
end idle
Use at your own risk.
Detects zombie state via shell ps and restarts the app.
on idle
set theApp to "AppName"
set state to ""
tell application "System Events"
try
set PID to unix id of process theApp as Unicode text
set state to paragraph 2 of (do shell script "ps -p " & quoted form of PID & " | awk '{ print $3 }'")
end try
end tell
if state = "Z" then
do shell script "kill " & PID
delay 3
tell application theApp to activate
--tell application "Finder" to open POSIX file "/Path/to/file.ext"
end if
return 10
end idle
Threshold set to 52% in this example.
on idle
set theApp to "AppName"
tell application "System Events"
try
set PID to unix id of process theApp as Unicode text
end try
end tell
set cpuUsage to (do shell script "ps -c -o %cpu='' -p " & PID) as number
if cpuUsage is greater than "52.0" then
tell application theApp to quit
end if
return 10
end idle
Released under the MIT license. Use them, modify them, ship them with your installations. No strings, no warranty.