Back to SIGMASIX Apps

Applescripts for automated startup and more

Small tricks


-- wait 10 seconds before the next action
delay 10
			

-- change the sound output (from 0 to 100)
set volume output volume 100
			

-- open an application
tell application "AppName" to activate
			

-- open a file
tell application "Finder" to open POSIX file "/Path/to/file.ext"
			

-- alternative way to open file or application
do shell script "open /Path/to/file.ext"
			

-- hide a specific application
tell application "System Events"
	set visible of process "ProcessName" to false
end tell
			

-- instantly shut down the computer
ignoring application responses
	tell application "Finder"
		shut down
	end tell
end ignoring
			

-- Reboot using shell script with administrator privileges
-- Replace $user by user and $password with password
do shell script "reboot" user name "$user" password "$password" with administrator privileges
			

-- Give focus to an app window
tell application "System Events" to tell process "ProcessName"
	perform action "AXRaise" of window 1
end tell
			

More tricky stuff


-- auto connect network midi

-- 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
			

-- send PJLink command to turn on a video projector

-- see ref here: http://pjlink.jbmia.or.jp/english/
-- for messages ref (starting page 9): http://pjlink.jbmia.or.jp/english/data/5-1_PJLink_eng_20131210.pdf
-- this script doesn't work if the video projector is password protected
set theIp to "127.0.0.1" -- the projector ip
set theMessage to "POWR 1" -- no need for the %1 at the beginning neither the (rc) at eol

-- no need to edit anything below
do shell script "echo \"%1" & theMessage & "\\r\" | nc " & theIp & " 4352"
			

To use exported as application with the option "Stay open after run handler"


-- quit and reopen an app every 45 minutes

-- you might need to change the 3 sec delay if your app takes more time to quit
on idle
	set theApp to "AppName"
	tell application theApp to quit
	delay 3
	tell application theApp to activate
	return 2700
end idle
			

-- reopen an app after it crashed (check every 10 secondes)

-- to avoid the crash reporter to appear, deactivate it using the terminal
-- 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
			

-- alternative way since MacOS 10.9
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
			

WARNING! The following scripts have not been tested properly


-- restart an app that is hang (beach ball) !!NOT TESTED!!
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
			

-- quit an app with too much CPU usage !!NOT TESTED!!
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
			

General Terms & Conditions