Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| applescript_snippit [2009/09/22 18:06] – berkinet | applescript_snippit [2026/04/07 18:27] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== AppleScript Snippits ====== | ||
| + | <color red> | ||
| + | --------- | ||
| + | |||
| + | This page is intended to provide a library of Indigo related AppleScript handlers (functions), | ||
| + | ---- | ||
| + | |||
| + | * **Make sure only one instance of an AppleScript is running at a time.**\\ | ||
| + | The following code accomplishes the goal quite simply. If the script is already running, the new instantiation will push the count over 1, the script tells Indigo, and quits. By logically ANDing " | ||
| + | < | ||
| + | set myName to " | ||
| + | set procCount to do shell script "ps axww|grep -e \" | ||
| + | |||
| + | if procCount as integer is greater than 1 then | ||
| + | tell application " | ||
| + | log " | ||
| + | end tell | ||
| + | | ||
| + | end if | ||
| + | |||
| + | -- Since we got here, we are not already running, we can now do something useful | ||
| + | </ | ||
| + | * **Using //curl// to replace //URL Access Scripting// | ||
| + | Many user have experienced problems with AppleScript' | ||
| + | The original code: | ||
| + | < | ||
| + | tell application "URL Access Scripting" | ||
| + | | ||
| + | | ||
| + | -- work around -31040 error: | ||
| + | tell application "URL Access Scripting" | ||
| + | end tell | ||
| + | </ | ||
| + | The new code: | ||
| + | <code applescript> | ||
| + | do shell script "/ | ||
| + | </ | ||
| + | * **Converting seconds to minutes and hours** | ||
| + | Scripts that maintain time counters, like total ON time for a HVAC unit, often need to convert from seconds to hours, minutes and seconds for a useful display. This snippet will do just that: | ||
| + | <code applescript> | ||
| + | -- starttime and endtime are AppleScript dates | ||
| + | set totalSeconds to endtime - starttime | ||
| + | |||
| + | set theHours to (totalSeconds div hours) | ||
| + | set theRemainderSeconds to (totalSeconds mod hours) | ||
| + | set theMinutes to (theRemainderSeconds div minutes) | ||
| + | set theRemainderSeconds to (theRemainderSeconds mod minutes) | ||
| + | |||
| + | set theTimeString to theHours & ":" | ||
| + | </ | ||
| + | * **Testing for a time range**\\ | ||
| + | This little snippet checks to see if the time is between a start and end time. This might be useful in a Trigger Condition. | ||
| + | <code applescript> | ||
| + | set startTime to " | ||
| + | set endTime to " | ||
| + | |||
| + | if (current date) is greater than date startTime and (current date) is less than date endTime then | ||
| + | return true | ||
| + | else | ||
| + | return false | ||
| + | end if | ||
| + | </ | ||
| + | \\ | ||
| + | * **Testing a device state**\\ | ||
| + | Indigo spends a lot of effort making sure that its record of a device state is accurate for built-in devices (getting state information from plugin defined devices requires you to use Python). So, it is usually better to rely on Indigo' | ||
| + | <code applescript> | ||
| + | set brightLevel to brightness of device " | ||
| + | |||
| + | if brightLevel > 50 then | ||
| + | | ||
| + | else | ||
| + | return false | ||
| + | end if | ||
| + | </ | ||
| + | <code applescript> | ||
| + | set onState to on state of device " | ||
| + | |||
| + | if onState = true then | ||
| + | | ||
| + | else | ||
| + | return false | ||
| + | end if | ||
| + | </ | ||
| + | \\ | ||
| + | * **A handler to simplify creating and setting Indigo variables**\\ | ||
| + | Setting Indigo variables is a common task in scripts. This task is made especially more complex if you are developing a script that may be run by others. It is often the case the users forget to create the variables needed for a script, or use incorrect names. This handler solves all of that. | ||
| + | <code applescript> | ||
| + | -- Set the value of a variable. | ||
| + | -- If the variable doesn' | ||
| + | on setVariable(theVariable, | ||
| + | tell application " | ||
| + | if variable theVariable exists then | ||
| + | set value of variable theVariable to theValue | ||
| + | else | ||
| + | make new variable with properties {name: | ||
| + | end if | ||
| + | end tell | ||
| + | end setVariable | ||
| + | </ | ||
| + | \\ | ||
| + | * **A handler to convert HVAC modes to strings**\\ | ||
| + | HVAC modes from a thermostat are enumerations in AppleScript that can't be coerced into strings. So, use this handler to convert an HVAC mode into the equivalent string. | ||
| + | <code applescript> | ||
| + | -- Set the value of a variable. | ||
| + | -- If the variable doesn' | ||
| + | on stringForHVACMode(inMode) | ||
| + | if (inMode = offMode) then | ||
| + | return " | ||
| + | else if (inMode = heatOn) then | ||
| + | return " | ||
| + | else if (inMode = coolOn) then | ||
| + | return " | ||
| + | else if (inMode = heatCoolOn) then | ||
| + | return " | ||
| + | else if (inMode = runProgramHeat) then | ||
| + | return " | ||
| + | else if (inMode = runProgramCool) then | ||
| + | return " | ||
| + | else if (inMode = runProgramAuto) then | ||
| + | return " | ||
| + | end if | ||
| + | end stringForHVACMode | ||
| + | </ | ||
| + | \\ | ||
| + | * **Calculating Moon Phase**\\ | ||
| + | This snippit of AppleScript will calculate the phase of the moon. It sets a variable named moonPhase (create it yourself first) to a number between 0 and 7. 0 is the new moon and 4 is a full moon. You could then create images for a control page starting with 0 as the new moon, progressing from 1-3 as the moon waxing, 4 as a full moon, and finally 5-7 as the moon in various stages of waning until you get back to 0 (new moon). It may be a day or two off and doesn' | ||
| + | |||
| + | <code applescript> | ||
| + | set theMonth to month of (current date) as integer | ||
| + | set theDay to day of (current date) | ||
| + | |||
| + | if (theMonth = 1) then | ||
| + | set theDay to theDay - 1 | ||
| + | else if (theMonth = 2) then | ||
| + | set theDay to theDay + 30 | ||
| + | else | ||
| + | set theDay to theDay + 28 + (theMonth - 2) * 3059 / 100 | ||
| + | end if | ||
| + | set g to (theYear - 1900) mod 19 + 1 | ||
| + | set e to (11 * g + 18) mod 30 | ||
| + | if (((e = 25) and (g > 11)) or (e = 24)) then | ||
| + | set e to e + 1 | ||
| + | end if | ||
| + | set fullness to ((((e + theDay) * 6 + 11) mod 177) / 22) as integer | ||
| + | if (fullness = 8) then set fullness to 0 | ||
| + | set value of variable " | ||
| + | </ | ||