Differences

This shows you the differences between two versions of the page.

Link to this comparison view

common_tasks_in_applescript [2014/06/06 02:47]
common_tasks_in_applescript [2019/01/26 00:10] (current)
Line 1: Line 1:
 +====== Completing Common Tasks in AppleScript ======
 +
 +This How-To will show you how to perform common tasks in AppleScript. These lines will run as is if they are run from an [[applescript|embedded AppleScript]]. If you want to run them from outside Indigo (i.e. Script Editor), then enclose them in a tell application block like this:
 +<​code>​
 +tell application "​IndigoServer"​
 +    turn on "​Office Lamp"
 +end tell</​code>​
 +
 +  * Turn on/off a light named "​Office Lamp"
 +<​code>​
 +turn on "​Office Lamp"
 +turn on "​Office Lamp" for 60       -- leaves the light on for one minute (60 seconds) then turns it back off
 +turn on "​Office Lamp" in 60        -- turns the light on after delaying one minute (60 seconds)
 +turn on "​Office Lamp" in 60 for 60 -- combines the above two
 +-- the previous options work as well with "turn off" and "​toggle"​
 +</​code>​
 +  * Brighten/​dim a light named "​Office Lamp"
 +<​code>​
 +-- Note: all brighten/​dim numbers are 0-100 (0 being off and 100 being all the way on)
 +brighten "​Office Lamp" to 85      -- sets the brightness to 85 regardless of previous setting
 +brighten "​Office Lamp" by 25      -- brightens the lamp by 25 from it's current setting
 +brighten "​Office Lamp" by 25 in 5 -- delays the brighten command by 5 seconds
 +dim "​Office Lamp" to 25           -- sets the brightness to 25 regardless of previous setting
 +dim "​Office Lamp" by 25           -- dims the lamp by 25 from it's current setting
 +dim "​Office Lamp" by 25 in 5      -- delays the dim command by 5 seconds
 +</​code>​
 +  * disable/​enable time/date or trigger action
 +<​code>​
 +-- Each of the below enable statements has a corresponding disable command
 +enable time date action "Test T/D Action" ​                -- enables a time/date action named "Test T/D Action"​
 +enable time date action "Test T/D Action"​ for 60*60       -- same as above, but leaves the t/d action enabled only for 1 hour (60 seconds * 60 minutes)
 +enable time date action "Test T/D Action"​ in 60           -- enables a time/date action named "Test T/D Action"​ but delays one minute (60 seconds) first
 +enable time date action "Test T/D Action"​ in 60 for 60*60 -- combines the above two
 +
 +-- Each of the below enable statements has a corresponding disable command
 +enable trigger action "Test Trigger Action" ​                -- enables a trigger action named "Test Trigger Action"​
 +enable trigger action "Test Trigger Action"​ for 60*60       -- same as above, but leaves the t/d action enabled only for 1 hour (60 seconds * 60 minutes)
 +enable trigger action "Test Trigger Action"​ in 60           -- enables a trigger action named "Test Trigger Action"​ but delays one minute (60 seconds) first
 +enable trigger action "Test Trigger Action"​ in 60 for 60*60 -- combines the above two
 +</​code>​
 +  * Execute an Action Group named "Test Action Group"
 +<​code>​
 +execute group "Test Action Group" ​       -- execute the group
 +execute group "Test Action Group" in 60  -- delay the group execution by one minute (60 seconds)
 +</​code>​
 +  * Execute an AppleScript in a separate thread ([[applescript|why?​]])
 +<​code>​
 +execute script file "​MyDisk:​Path:​to:​the:​script:​file.scpt" ​
 +execute script POSIX file "/​Path/​to/​the/​script/​file.scpt"​
 +</​code>​
 +  * Getting information from a thermostat named "First Floor"
 +<​code>​
 +-- Getting the temperature and humidity from a thermostat is a little complex
 +-- because the values of those properties are actually lists - it's possible to get
 +-- multiple temperatures and humidity readings with the right equipment (with external
 +-- sensors, etc.)
 +set tempList to temperatures of device "First Floor"
 +-- tempList is now an AppleScript list - for a standard thermostat it will be something like {74}
 +-- with just one item. To get each item, you need to refer to it by number:
 +set theTemp to item 1 of tempList
 +-- After this line executes, theTemp will be equal to 74.
 +-- Humidity values work exactly the same way. If you're using an IO device that has multiple inputs and outputs,
 +-- they will also be presented as lists.
 +</​code>​
 +  * Adjust a Thermostat named "First Floor" (these are just a few examples, look at the [[http://​static.indigodomo.com/​www/​html/​02300_indigo_as_dict.html#​class_device__Indigo_Main_Suite|device]] dictionary for details)
 +<​code>​
 +set hvac mode of device "First Floor" to heatCoolOn ​ -- operation based on the heat and cool setpoints
 +set hvac mode of device "First Floor" to runProgramAuto --  automatic operation of the Auto program that you create on the thermostat
 +set heat setpoint of device "First Floor" to 72 -- used when heatOn or heatCoolOn are set as hvac mode
 +set cool setpoint of device "First Floor" to 78 -- used when coolOn or heatCoolOn are set as hvac mode
 +set fan mode of device "First Floor" to fanAlwaysOn ​ -- keeps the fan running all the time - fanAutoOn is the other option
 +</​code>​
 +  * Control a sprinkler named "​Sprinkler"​
 +<​code>​
 +set sprinkler zone of device "​Sprinkler"​ to 0 -- turns the sprinkler off
 +set sprinkler zone of device "​Sprinkler"​ to 1 -- turns on zone one which will run for the max duration
 +</​code>​
 +  * Creating/​Getting/​Setting Variables
 +<​code>​
 +-- Create a variable named "​Test_Variable"​
 +make new variable with properties {name:"​Test_Variable",​ value:"​Some Value Here"}
 +
 +-- Get the value of a variable named "​Test_Variable"​ into an AppleScript variable named theVarValue
 +set theVarValue to value of variable "​Test_Variable"​
 +
 +-- Set the value of a variable named "​Test_Variable"​ to "Hello World!"​
 +set value of variable "​Test_Variable"​ to "Hello World!"​
 +</​code>​
 +  * Log something into the Indigo Event Log
 +<​code>​
 +-- The following line
 +log "This is a typical log entry"
 +-- results in " ​ Script ​            This is a typical log entry" being inserted into the log
 +-- to change "​Script"​ to something more useful, use the following line
 +log "This is a typical log entry" using type "​MyTestScript
 +-- results in " ​ MyTestScript ​      This is a typical log entry" being inserted into the log
 +</​code>​
 +  * Remove Delayed Actions - you know, when you set "Delay by" in an action, or use "​in"​ when turning on/off a device. When you do that, Indigo creates a time/date action with a funky name and you may not know which action or device is associated with it. Here's how you do it in AppleScript:​
 +<​code>​
 +-- This one will remove any delayed actions associated with the device named "​Office Lamp"
 +remove delayed actions for device "​Office Lamp" ​
 +
 +-- This will remove any delayed actions from a trigger named "My Test Trigger" ​
 +-- (this can be either a time/date action or a trigger action)
 +remove delayed actions for trigger "My Test Trigger"​
 +
 +-- This will remove all delayed actions
 +remove delayed actions
 +</​code>​
 +  * Supress logging - when you run scripts that operate devices, all of those changes get inserted into the log. In any given script, you may not want any of the results logged - or perhaps you only want one log entry for the entire script. Here's how you would do that.
 +<​code>​
 +-- NOTE: for this to work correctly, you MUST have the with transaction block INSIDE a 
 +-- tell application "​IndigoServer"​ even if this is being run embedded.
 +tell app "​IndigoServer" ​
 +   with transaction ​
 +      transaction suppress logging -- Anything after this line before the end transaction line will NOT be logged
 +      turn on "​Office Sconces" ​
 +      turn off "​Office LampLinc" ​
 +      set brightness of device "​Office Overhead"​ to 35 
 +   end transaction
 +   log "​Office scene created"​ using type "​Office Script"​
 +end tell
 +</​code>​
 +  * Supress state change sending - some users have found that creating a "​virtual"​ device - a device that doesn'​t actually have a hardware target, is useful for representing state in Indigo. However, Indigo requires that when you define a device you specify an address of some type for the device (X10 for instance). So, you can control those devices through scripts without Indigo trying to actually send the command:
 +<​code>​
 +-- NOTE: for this to work correctly, you MUST have the with transaction block INSIDE a 
 +-- tell application "​IndigoServer"​ even if this is being run embedded.
 +tell application "​IndigoServer"​
 + with transaction
 + transaction suppress state change sending
 + -- every state change command after the above suppress verb 
 + -- that is inside this "with transaction"​ block will not send out any 
 + -- interface commands ​
 + set brightness of device "​Virtual Volume Controller"​ to 15
 + turn on "​Virtual Attic Fan"
 + end transaction
 +end tell
 +</​code>​
 +  * Use multiple conditions in a single trigger (specify no conditionals in the conditions tab and use the following as an Execute AppleScript action)
 +<​code>​
 +if ((value of variable "​houseMode"​ = "​night"​) and (value of variable "​houseStatus"​ = "​vacation"​)) then
 + -- do whatever you need to do here
 +end if
 +</​code>​
  
 

© Perceptive Automation, LLC. · Privacy