Coding for Installations

Sometimes you need to control a computer for an installation project to do many things over the course of a day. Each installation is different, but the code below was used for a particularly challenging project. The applications were Java files, built from Processing that relied on openTSPS for people tracking, an Arduino hooked up to sensors and a few apps also used Processing’s built-in video camera, which caused a conflict if opentTSPS was open. In the end, I found the best way to deal with all the issues was to simply create a single AppleScript that would open up each app, wait for 10 minutes, then move on to the next.

Since the tasks were similar, I created custom functions that could be called – “open_java”, “open_processing” and “close_if_open”. I pass a string for the name of the app that I want to open, close, etc. and a common wait time for testing. During the testing phase I set longWait to 10-30 to make sure it cycled through them all. The checkUserNotification() function is an attempt to catch and gather all those system warnings that can pop up over days of running a computer non-stop. It tries to close/cancel the warning and does a check after we quit each app.

Using AppleScript I compiled the script below into a stand-alone app. Then, I set the computer to run the app on Startup by going into the Account Settings in the Mac Preferences and making the app a Login item.

 
set shortWait to 5
set longWait to 600

delay 30

repeat
	close_if_open({"Processing"})

	tell application "openTSPS"
		activate
	end tell
	open_java("Burst_Your_Bubble", shortWait, longWait)

	close_if_open({"openTSPS", "Processing"})

	open_java("Branden_Carly_Brandonutyfuvty_final", shortWait, longWait)

	close_if_open({"openTSPS", "Processing"})

	open_processing("YouMoodMe.pde", "YouMoodMe", shortWait, longWait)

	close_if_open({"openTSPS", "Processing"})

	open_processing("colorcollider.pde", "colorcollider", shortWait, longWait)

	close_if_open({"openTSPS", "Processing"})

	open_processing("HYPOT_PROCESSING.pde", "HYPOT_PROCESSING", shortWait, longWait)

	close_if_open({"Processing"})

	tell application "openTSPS"
		activate
	end tell
	open_java("Flippy_Boxes_Final_A", shortWait, longWait)

	close_if_open({"Processing"})

	tell application "openTSPS"
		activate
	end tell
	open_java("Project3Final_Ruben_Andrew_Sterling", shortWait, longWait)

	close_if_open({"Processing"})

	tell application "openTSPS"
		activate
	end tell
	open_java("Surreal_Walk_3", shortWait, longWait)

end repeat

on close_if_open(_items)
	set appList to _items
	tell application "System Events"
		set procList to (name of every process)
	end tell

	repeat with appName in appList
		if (procList contains appName) is true then
			tell application appName to quit
			delay 1
		end if
	end repeat
	delay 3
end close_if_open

on open_processing(_item, _folder, shortWait, longWait)
	tell application "Finder"
		--activate
		open document file _item of folder _folder of folder "Processing" of folder "Documents" of folder "labadmin" of folder "Users" of startup disk
		delay shortWait
		tell application "System Events"
			keystroke "r" using {command down, shift down}
		end tell
		delay longWait
	end tell
	checkUserNotification()
end open_processing

on open_java(appName, shortWait, longWait)
	tell application appName
		reopen
		activate
		delay longWait
	end tell
	tell application appName to quit
	checkUserNotification()
end open_java

on checkUserNotification()
	tell application "System Events"
		if "UserNotificationCenter" is in (get name of processes whose background only is true) then
			get windows of process "UserNotificationCenter"
			repeat (count result) times
				tell application "UserNotificationCenter" to activate
				delay 1
				keystroke return
				delay 1
				keystroke "." using {command down}
			end repeat
		end if
	end tell
end checkUserNotification

Here’s a video tutorial for getting started with AppleScript for installations:

Leave a Reply