// If the Shift key is pressed while clicking the button then the current
// automation script is called else the automation script is loaded first
Variable Global SaveScript
SaveScript = "False"
If ShiftKey AND CommandKey AND OptionKey
	SaveScript = "True"
End If
If ShiftKey AND (NOT CommandKey) AND (NOT OptionKey)
	Call "Automation"
Else
	Call Scheduled
End If

On Startup
	Button.Text = Process.Front
	Button.Color = 0 // White
	Button.Update
	If NOT Button("Automation").Exists
		Button("Automation").New
	End If
	If File("REDMCSB:SOURCE:AUTORUN.TXT").Exists
		Call Scheduled
	End If
End Startup

On Scheduled
	Variable Global SaveScript
	Variable ScriptFileName
	// Automation script file name based on front application name
	ScriptFileName = "REDMCSB:SOURCE:" & Button.Text & ".TXT"
	// If the Command key, the Shift key and the Option key are all
	// pressed while clicking the button then the current automation
	// script is saved if it is not empty
	If (SaveScript = "True") AND NOT (Button("Automation").Script = "")
		File(ScriptFileName).Text = Button("Automation").Script
	End If
	// Load automation script
	If File(ScriptFileName).Exists
		Button("Automation").Script = File(ScriptFileName).Text
	Else
		Button("Automation").Script = ""
	End If
	// Update button color and run automation script if present
	If Button("Automation").Script = ""
		Button.Color = 36 // Red
		Button.Update
	Else
		Button.Color = 0 // White
		Button.Update
		Call "Automation"
	End If
End Scheduled
