Example Condition AppleScripts

This How-To will show several common and useful AppleScript Condition scripts. Most will run correctly if you just paste them in to the “If script returns true” text field in the Conditions tab.

  • If the current time is between 8am and 10am OR between 4pm and 6pm
-- Get the current hour
set theCurrentHour to hours of (current date)

-- compare it to see if it's in one of our ranges - between 8am
-- and 10am OR between 4pm and 6pm
--
-- Note, the number that is stored in theCurrentHour will be 0-23
if (((theCurrentHour ≥ 8) and (theCurrentHour < 10)) or ¬
	((theCurrentHour ≥ 16) and (theCurrentHour < 18))) then
	return true
end if
return false
  • If a light named “Office Lamp” is ON
return (on state of device "Office Lamp")
  • If a light named “Office Lamp” is OFF
return not (on state of device "Office Lamp")
  • If the garage door is closed (with the I/O Linc Garage Door Kit)
-- I/O devices can have multiple inputs, so you have to get
-- the list of inputs first. For the I/O link, it only 
-- has one, so we just look at the first one in the
-- list.
set theList to binary inputs of device "Garage Door"
return item 1 of theList
  • If iCal is running
try
	with timeout of 1 second
		tell application "System Events"
			if (get name of every process) contains "iCal" then
				return true
			end if
		end tell
	end timeout
end try
return false 
  • If the time is between 8am and 4pm AND the garage door is closed AND the variable “alarmStatus” is set to “away”
set theCurrentHour to hours of (current date)
if ((theCurrentHour ≥ 8) and (theCurrentHour < 16)) then
	set theList to binary inputs of device "Garage Door"
	if ((item 1 of theList) and (value of variable "alarmStatus" = "away")) then
		return true
	end if
end if
return false
  • If a light named “Office Lamp” is OFF and it's dark outside
return (not (on state of device "Office Lamp") and (value of variable "isDaylight" = "false"))
applescript_conditionals.txt · Last modified: 2014/05/28 16:15 (external edit)
 

© Perceptive Automation, LLC. · Privacy