Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| common_tasks_in_applescript [2009/03/05 21:08] – jay | common_tasks_in_applescript [2026/04/07 18:27] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| 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: | ||
| + | < | ||
| + | tell application " | ||
| + | turn on " | ||
| + | end tell</ | ||
| + | |||
| + | * Turn on/off a light named " | ||
| + | < | ||
| + | turn on " | ||
| + | turn on " | ||
| + | turn on " | ||
| + | turn on " | ||
| + | -- the previous options work as well with "turn off" and " | ||
| + | </ | ||
| + | * Brighten/ | ||
| + | < | ||
| + | -- Note: all brighten/ | ||
| + | brighten " | ||
| + | brighten " | ||
| + | brighten " | ||
| + | dim " | ||
| + | dim " | ||
| + | dim " | ||
| + | </ | ||
| + | * disable/ | ||
| + | < | ||
| + | -- Each of the below enable statements has a corresponding disable command | ||
| + | enable time date action "Test T/D Action" | ||
| + | enable time date action "Test T/D Action" | ||
| + | enable time date action "Test T/D Action" | ||
| + | enable time date action "Test T/D Action" | ||
| + | |||
| + | -- Each of the below enable statements has a corresponding disable command | ||
| + | enable trigger action "Test Trigger Action" | ||
| + | enable trigger action "Test Trigger Action" | ||
| + | enable trigger action "Test Trigger Action" | ||
| + | enable trigger action "Test Trigger Action" | ||
| + | </ | ||
| + | * Execute an Action Group named "Test Action Group" | ||
| + | < | ||
| + | execute group "Test Action Group" | ||
| + | execute group "Test Action Group" in 60 -- delay the group execution by one minute (60 seconds) | ||
| + | </ | ||
| + | * Execute an AppleScript in a separate thread ([[applescript|why? | ||
| + | < | ||
| + | execute script file " | ||
| + | execute script POSIX file "/ | ||
| + | </ | ||
| + | * Getting information from a thermostat named "First Floor" | ||
| + | < | ||
| + | -- 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. | ||
| + | </ | ||
| + | * Adjust a Thermostat named "First Floor" (these are just a few examples, look at the [[http:// | ||
| + | < | ||
| + | set hvac mode of device "First Floor" to heatCoolOn | ||
| + | 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 | ||
| + | </ | ||
| + | * Control a sprinkler named " | ||
| + | < | ||
| + | set sprinkler zone of device " | ||
| + | set sprinkler zone of device " | ||
| + | </ | ||
| + | * Creating/ | ||
| + | < | ||
| + | -- Create a variable named " | ||
| + | make new variable with properties {name:" | ||
| + | |||
| + | -- Get the value of a variable named " | ||
| + | set theVarValue to value of variable " | ||
| + | |||
| + | -- Set the value of a variable named " | ||
| + | set value of variable " | ||
| + | </ | ||
| + | * Log something into the Indigo Event Log | ||
| + | < | ||
| + | -- The following line | ||
| + | log "This is a typical log entry" | ||
| + | -- results in " | ||
| + | -- to change " | ||
| + | log "This is a typical log entry" using type " | ||
| + | -- results in " | ||
| + | </ | ||
| + | * Remove Delayed Actions - you know, when you set "Delay by" in an action, or use " | ||
| + | < | ||
| + | -- This one will remove any delayed actions associated with the device named " | ||
| + | remove delayed actions for device " | ||
| + | |||
| + | -- 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 | ||
| + | </ | ||
| + | * Suppress 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. | ||
| + | < | ||
| + | -- NOTE: for this to work correctly, you MUST have the with transaction block INSIDE a | ||
| + | -- tell application " | ||
| + | tell app " | ||
| + | with transaction | ||
| + | transaction suppress logging -- Anything after this line before the end transaction line will NOT be logged | ||
| + | turn on " | ||
| + | turn off " | ||
| + | set brightness of device " | ||
| + | end transaction | ||
| + | log " | ||
| + | end tell | ||
| + | </ | ||
| + | * Suppress state change sending - some users have found that creating a " | ||
| + | < | ||
| + | -- NOTE: for this to work correctly, you MUST have the with transaction block INSIDE a | ||
| + | -- tell application " | ||
| + | tell application " | ||
| + | with transaction | ||
| + | transaction suppress state change sending | ||
| + | -- every state change command after the above suppress verb | ||
| + | -- that is inside this "with transaction" | ||
| + | -- interface commands | ||
| + | set brightness of device " | ||
| + | turn on " | ||
| + | end transaction | ||
| + | end tell | ||
| + | </ | ||
| + | * Use multiple conditions in a single trigger (specify no conditionals in the conditions tab and use the following as an Execute AppleScript action) | ||
| + | < | ||
| + | if ((value of variable " | ||
| + | -- do whatever you need to do here | ||
| + | end if | ||
| + | </ | ||