Using iCal 1.5 with Indigo

Posted on
Tue Oct 21, 2003 9:06 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

Using iCal 1.5 with Indigo

This is an attachement script that I use to log motion detector activity to iCal. I can go to my HA machine and see when there was activity and from what motion detector. To use you create a trigger and put this in the applescript area

Code: Select all
logical("The Motion Detector Name")


Here is the code for the attachment

--[url=applescript://com.apple.scripteditor/?action=new&script=property%20iCalendars%20%3A%20%7B%7D%0A%0Aset%20theCals%20to%20%7B%7D%0Aset%20iCalendars%20to%20%7B%7D%0A%0Ausing%20terms%20from%20application%20%22Indigo%22%0A%09on%20logiCal(unitname)%0A%09%09--Set%20the%20Calendar%20Name%20%0A%09%09set%20theCalName%20to%20unitname%0A%09%09set%20eventdate%20to%20current%20date%0A%09%09%0A%09%09--Get%20the%20Calendars%20from%20iCal%20%0A%09%09tell%20application%20%22iCal%22%0A%09%09%09set%20theCals%20to%20calendars%0A%09%09%09repeat%20with%20i%20from%201%20to%20count%20of%20theCals%0A%09%09%09%09copy%20title%20of%20item%20i%20of%20theCals%20to%20end%20of%20iCalendars%0A%09%09%09end%20repeat%0A%09%09%09%0A%09%09%09--%20If%20there%20is%20no%20calendar%20for%20this%20unit%20create%20one%20%0A%09%09%09if%20theCalName%20is%20not%20in%20iCalendars%20then%0A%09%09%09%09create%20calendar%20with%20name%20theCalName%0A%09%09%09%09--%20Refresh%20calendar%20list%20with%20our%20new%20one%CA%20%CA%CA%20%CA%20%0A%09%09%09%09set%20iCalendars%20to%20%7B%7D%0A%09%09%09%09set%20theCals%20to%20%7B%7D%0A%09%09%09%09set%20theCals%20to%20calendars%0A%09%09%09%09repeat%20with%20i%20from%201%20to%20count%20of%20theCals%0A%09%09%09%09%09copy%20title%20of%20item%20i%20of%20theCals%20to%20end%20of%20iCalendars%0A%09%09%09%09end%20repeat%0A%09%09%09end%20if%0A%09%09end%20tell%0A%09%09%0A%09%09--%20Run%20through%20our%20Calendar%20list%20and%20find%20the%20iCal%20id%20of%20theCalName%20%0A%09%09set%20i%20to%201%0A%09%09repeat%20with%20anitem%20in%20iCalendars%0A%09%09%09if%20item%20i%20of%20iCalendars%20is%20equal%20to%20theCalName%20then%0A%09%09%09%09set%20theNum%20to%20i%0A%09%09%09%09exit%20repeat%0A%09%09%09end%20if%0A%09%09%09set%20i%20to%20i%20+%201%0A%09%09end%20repeat%0A%09%09%0A%09%09set%20theSummary%20to%20%22Motion%20Detected%20at%22%20%26%20eventdate%20as%20string%0A%09%09%0A%09%09tell%20application%20%22iCal%22%0A%09%09%09make%20event%20at%20end%20of%20events%20of%20calendar%20theNum%20with%20properties%20%7Bstart%20date%3Aeventdate,%20summary%3AtheSummary%7D%0A%09%09end%20tell%0A%09end%20logiCal%0Aend%20using%20terms%20from%0A]Click here to open this script in a new Script Editor window[/url].

property iCalendars : {}

set theCals to {}
set iCalendars to {}

using terms from application "Indigo"
     on logiCal(unitname)
          --Set the Calendar Name
          set theCalName to unitname
          set eventdate to current date
          
          --Get the Calendars from iCal
          tell application "iCal"
               set theCals to calendars
               repeat with i from 1 to count of theCals
                    copy title of item i of theCals to end of iCalendars
               end repeat
               
               -- If there is no calendar for this unit create one
               if theCalName is not in iCalendars then
                    create calendar with name theCalName
                    -- Refresh calendar list with our new one      
                    set iCalendars to {}
                    set theCals to {}
                    set theCals to calendars
                    repeat with i from 1 to count of theCals
                         copy title of item i of theCals to end of iCalendars
                    end repeat
               end if
          end tell
          
          -- Run through our Calendar list and find the iCal id of theCalName
          set i to 1
          repeat with anitem in iCalendars
               if item i of iCalendars is equal to theCalName then
                    set theNum to i
                    exit repeat
               end if
               set i to i + 1
          end repeat
          
          set theSummary to "Motion Detected at" & eventdate as string
          
          tell application "iCal"
               make event at end of events of calendar theNum with properties {start date:eventdate, summary:theSummary}
          end tell
     end logiCal
end using terms from


-------------------------
[This script was automatically tagged for color coded syntax by Script to Markup Code]

Posted on
Mon Nov 10, 2003 10:08 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

I made an entry to my blog about my adventures with iCal and Indigo. Take a look if your interested.

Posted on
Tue Nov 11, 2003 11:39 am
dalenis offline
Posts: 123
Joined: Apr 02, 2003
Location: Lunenburg, MA

(No subject)

Hi Greg,

I can't get this to work......I must be doing something really stupid! I created a Trigger called "logical" and set it to "Trigger" when a "Devices changes state" to "On", with condition "Always" and a "Action" to "Execute AppleScript"....I pasted your script in the "Embedded" window. The log states the trigger get executed, but nothing is passed to iCal.

Thanks,

Dale

Posted on
Tue Nov 11, 2003 11:43 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

You need to save the script as an attachment, as in a separate complied script.

Then in your embedded applescript use logical("theunitnameyourtracking")

Make sure to reload attachments.

Posted on
Tue Nov 11, 2003 12:34 pm
Guest offline

(No subject)

Greg,

That worked just fine.....thanks.....actually this is really neat!

Now you/Matt have to tell me how this works.....How does the logical("theunitnameyourtracking") embedded script, activate the attachment script.....I know this is very basic but I can't not get this clear in my mind.

I hope I'm not the only one having trouble with this!

Damn I'm glad I don't program/scripting for a living I would go hungry!

Thanks,

Dale

Posted on
Tue Nov 11, 2003 12:40 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

(No subject)

Anonymous wrote:
How does the logical("theunitnameyourtracking") embedded script, activate the attachment script.

All compiled attachment scripts are loaded when Indigo is launched. Then, whenever you call an AppleScript function, Indigo dispatches (sends AppleEvents) to all of the attachment scripts. So, if you've defined a function inside an attachment script, then that function will be available to call from inside any _tell application "Indigo"_ block.

Additionally, attachment scripts are dispatched the X10 commands as they come in from the interface. This provides a low level hook to data coming in from the powerline.

Regards,
Matt

Posted on
Mon Apr 19, 2004 7:24 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

Here's an updated version of my script. Basicly this is what's new:

    It can track when a device is turned on and when one is turned off. If I turn on my living room lights at 5pm and turn them off 8pm it will show an event that starts at 5pm and ends at 8pm. It's a lot easier to to read this information in an iCal type interface then a log file.
    It does some cleaning up so if a device is turned on and Indigo never sees that it was turned off it will remove the event all together. This needs some work.
    It logs motion detectors different from "on/off devices".
    It uses the description field to determine what kind of device it is and what calendar to put it in. I'm pretty consistent with my descriptions which look like this: Switchlinc controlling a lamp in the living room.


There's still work to be done on it. To use it you have to create separate on and off triggers and put the logUnitsIniCal(unitname, AutoManual) in each. the AutoManual parameter isn't used yet.

--[url=applescript://com.apple.scripteditor/?action=new&script=property%20iCalendars%20%3A%20%7B%7D%0A%0Aset%20theCals%20to%20%7B%7D%0Aset%20iCalendars%20to%20%7B%7D%0A%0Ausing%20terms%20from%20application%20%22Indigo%22%0A%09on%20logUnitsIniCal(unitname,%20AutoManual)%0A%09%09tell%20application%20%22Indigo%22%0A%09%09%09set%20UnitDisc%20to%20description%20of%20device%20unitname%0A%09%09%09set%20unitstate%20to%20on%20state%20of%20device%20unitname%0A%09%09%09log%20%22Logging%20a%20units%20In%20Ical%20for%20device%20%22%20%26%20unitname%20%26%20%22,%20%22%20%26%20UnitDisc%20%26%20%22,%20who's%20state%20is%20%22%20%26%20unitstate%0A%09%09end%20tell%0A%09%09--Set%20the%20Calendar%20Name%0A%09%09%0A%09%09if%20UnitDisc%20contains%20%22Motion%20Detector%22%20then%0A%09%09%09set%20theCalName%20to%20%22Motion%20Detectors%22%0A%09%09else%20if%20UnitDisc%20contains%20%22Rain8%22%20then%0A%09%09%09set%20the%20theCalName%20to%20%22Irrigation%20System%22%0A%09%09else%20if%20(UnitDisc%20contains%20%22Switchlinc%22%20and%20UnitDisc%20contains%20%22controlling%20a%20lamp%22)%20or%20(UnitDisc%20contains%20%22Lamp%20Module%22%20and%20UnitDisc%20contains%20%22controlling%20a%20lamp%22)%20then%0A%09%09%09set%20the%20theCalName%20to%20%22Lighting%22%0A%09%09else%0A%09%09%09set%20theCalName%20to%20unitname%0A%09%09end%20if%0A%09%09tell%20application%20%22Indigo%22%0A%09%09%09log%20%22The%20cal%20name%20is%20%22%20%26%20theCalName%0A%09%09end%20tell%0A%09%09set%20eventdate%20to%20current%20date%0A%09%09%0A%09%09--Get%20the%20Calendars%20from%20iCal%0A%09%09tell%20application%20%22iCal%22%0A%09%09%09set%20theCals%20to%20calendars%0A%09%09%09repeat%20with%20i%20from%201%20to%20count%20of%20theCals%0A%09%09%09%09copy%20title%20of%20item%20i%20of%20theCals%20to%20end%20of%20iCalendars%0A%09%09%09end%20repeat%0A%09%09%09--%20If%20there%20is%20no%20calendar%20for%20this%20unit%20create%20one%0A%09%09%09if%20theCalName%20is%20not%20in%20iCalendars%20then%0A%09%09%09%09create%20calendar%20with%20name%20theCalName%0A%09%09%09%09--%20Refresh%20calendar%20list%20with%20our%20new%20one%09%09%0A%09%09%09%09set%20iCalendars%20to%20%7B%7D%0A%09%09%09%09set%20theCals%20to%20%7B%7D%0A%09%09%09%09set%20theCals%20to%20calendars%0A%09%09%09%09repeat%20with%20i%20from%201%20to%20count%20of%20theCals%0A%09%09%09%09%09copy%20title%20of%20item%20i%20of%20theCals%20to%20end%20of%20iCalendars%0A%09%09%09%09end%20repeat%0A%09%09%09end%20if%0A%09%09%09--%20Run%20through%20our%20Calendar%20list%20and%20find%20the%20iCal%20id%20of%20theCalName%0A%09%09%09set%20i%20to%201%0A%09%09%09repeat%20with%20anitem%20in%20iCalendars%0A%09%09%09%09if%20item%20i%20of%20iCalendars%20contains%20theCalName%20then%0A%09%09%09%09%09set%20theNum%20to%20i%0A%09%09%09%09%09exit%20repeat%0A%09%09%09%09end%20if%0A%09%09%09%09set%20i%20to%20i%20+%201%0A%09%09%09end%20repeat%0A%09%09%09%0A%09%09%09set%20CompletedEvent%20to%20false%0A%09%09%09if%20theCalName%20is%20not%20%22Motion%20Detectors%22%20then%0A%09%09%09%09repeat%20with%20theCal%20in%20calendars%0A%09%09%09%09%09if%20title%20of%20theCal%20is%20theCalName%20then%0A%09%09%09%09%09%09repeat%20with%20theevent%20in%20(events%20of%20theCal)%0A%09%09%09%09%09%09%09if%20summary%20of%20theevent%20contains%20(unitname%20%26%20%22%20(started)%22)%20and%20start%20date%20of%20theevent%20%3C%20eventdate%20and%20unitstate%20is%20false%20then%0A%09%09%09%09%09%09%09%09set%20end%20date%20of%20theevent%20to%20eventdate%0A%09%09%09%09%09%09%09%09set%20summary%20of%20theevent%20to%20unitname%0A%09%09%09%09%09%09%09%09set%20CompletedEvent%20to%20true%0A%09%09%09%09%09%09%09end%20if%0A%09%09%09%09%09%09end%20repeat%0A%09%09%09%09%09end%20if%0A%09%09%09%09end%20repeat%0A%09%09%09%09%0A%09%09%09end%20if%0A%09%09%09%0A%09%09%09if%20CompletedEvent%20is%20false%20and%20unitstate%20is%20true%20then%0A%09%09%09%09if%20theCalName%20is%20%22Motion%20Detectors%22%20then%0A%09%09%09%09%09set%20theSummary%20to%20unitname%0A%09%09%09%09else%0A%09%09%09%09%09set%20theSummary%20to%20(unitname%20%26%20%22%20(started)%22)%0A%09%09%09%09end%20if%0A%09%09%09%09--First%20check%20for%20unclosed%20events%20with%20the%20same%20name%20and%20delete%20then%0A%09%09%09%09set%20i%20to%200%0A%09%09%09%09repeat%20with%20theCal%20in%20calendars%0A%09%09%09%09%09if%20title%20of%20theCal%20is%20theCalName%20then%0A%09%09%09%09%09%09repeat%20with%20theevent%20in%20(events%20of%20theCal)%0A%09%09%09%09%09%09%09set%20i%20to%20i%20+%201%0A%09%09%09%09%09%09%09if%20summary%20of%20theevent%20contains%20(unitname%20%26%20%22%20(started)%22)%20then%20delete%20event%20i%20of%20calendar%20theCal%0A%09%09%09%09%09%09end%20repeat%0A%09%09%09%09%09end%20if%0A%09%09%09%09end%20repeat%0A%09%09%09%09--create%20new%20event%0A%09%09%09%09make%20event%20at%20end%20of%20events%20of%20calendar%20theNum%20with%20properties%20%7Bstart%20date%3Aeventdate,%20summary%3AtheSummary%7D%0A%09%09%09end%20if%0A%09%09end%20tell%0A%09end%20logUnitsIniCal%0Aend%20using%20terms%20from%0A]Click here to open this script in a new Script Editor window[/url].

property iCalendars : {}

set theCals to {}
set iCalendars to {}

using terms from application "Indigo"
     on logUnitsIniCal(unitname, AutoManual)
          tell application "Indigo"
               set UnitDisc to description of device unitname
               set unitstate to on state of device unitname
               log "Logging a units In Ical for device " & unitname & ", " & UnitDisc & ", who's state is " & unitstate
          end tell
          --Set the Calendar Name
          
          if UnitDisc contains "Motion Detector" then
               set theCalName to "Motion Detectors"
          else if UnitDisc contains "Rain8" then
               set the theCalName to "Irrigation System"
          else if (UnitDisc contains "Switchlinc" and UnitDisc contains "controlling a lamp") or (UnitDisc contains "Lamp Module" and UnitDisc contains "controlling a lamp") then
               set the theCalName to "Lighting"
          else
               set theCalName to unitname
          end if
          tell application "Indigo"
               log "The cal name is " & theCalName
          end tell
          set eventdate to current date
          
          --Get the Calendars from iCal
          tell application "iCal"
               set theCals to calendars
               repeat with i from 1 to count of theCals
                    copy title of item i of theCals to end of iCalendars
               end repeat
               -- If there is no calendar for this unit create one
               if theCalName is not in iCalendars then
                    create calendar with name theCalName
                    -- Refresh calendar list with our new one          
                    set iCalendars to {}
                    set theCals to {}
                    set theCals to calendars
                    repeat with i from 1 to count of theCals
                         copy title of item i of theCals to end of iCalendars
                    end repeat
               end if
               -- Run through our Calendar list and find the iCal id of theCalName
               set i to 1
               repeat with anitem in iCalendars
                    if item i of iCalendars contains theCalName then
                         set theNum to i
                         exit repeat
                    end if
                    set i to i + 1
               end repeat
               
               set CompletedEvent to false
               if theCalName is not "Motion Detectors" then
                    repeat with theCal in calendars
                         if title of theCal is theCalName then
                              repeat with theevent in (events of theCal)
                                   if summary of theevent contains (unitname & " (started)") and start date of theevent < eventdate and unitstate is false then
                                        set end date of theevent to eventdate
                                        set summary of theevent to unitname
                                        set CompletedEvent to true
                                   end if
                              end repeat
                         end if
                    end repeat
                    
               end if
               
               if CompletedEvent is false and unitstate is true then
                    if theCalName is "Motion Detectors" then
                         set theSummary to unitname
                    else
                         set theSummary to (unitname & " (started)")
                    end if
                    --First check for unclosed events with the same name and delete then
                    set i to 0
                    repeat with theCal in calendars
                         if title of theCal is theCalName then
                              repeat with theevent in (events of theCal)
                                   set i to i + 1
                                   if summary of theevent contains (unitname & " (started)") then delete event i of calendar theCal
                              end repeat
                         end if
                    end repeat
                    --create new event
                    make event at end of events of calendar theNum with properties {start date:eventdate, summary:theSummary}
               end if
          end tell
     end logUnitsIniCal
end using terms from

Posted on
Wed Apr 21, 2004 7:13 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

Looks like iCal will crack under the pressure. I've been logging every motion detector "detections" into iCal and it just causes the whole system to slow down, CPU usage jumps to 100%.

Works just fine for lights and such but cannot handle the motion detectors. Of course this is a 500Mhz iBook with 256MB of ram or less.

Posted on
Wed Apr 21, 2004 7:09 pm
dalenis offline
Posts: 123
Joined: Apr 02, 2003
Location: Lunenburg, MA

(No subject)

Hi Greg,

I had that same problem when you first posted the script and stopped using motion sensor info into iCal at that time.......I meant to post a note to you about that. This was on a Cube....

Dale

Posted on
Wed Apr 21, 2004 7:27 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

I wonder if lots of ram or a super fast processor will help?

Posted on
Wed Apr 21, 2004 7:46 pm
dalenis offline
Posts: 123
Joined: Apr 02, 2003
Location: Lunenburg, MA

(No subject)

I don't think more ram will help.....my Cube has 640 MB.

Dale

Posted on
Sun May 30, 2004 11:07 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

Here's the latest incarnation of my iCal logging script. I've changed it quite a bit.

There's two ways to use it.
1) Use LogDevice(DeviceName) for devices.
2) Use logmessages(MessageGroup, message, EventKind) for non devices.

You set EventKind to true for the start of an event and EventKind to false for the end of a event. Or if you want to just do a single logging use false for EventKind.

This version makes use of the built in iCal properties and filters out Motion Detectors.

Note: I use the description field pretty heavily and this script may require that you have the proper info there.

--[url=applescript://com.apple.scripteditor/?action=new&script=using%20terms%20from%20application%20%22Indigo%22%0A%09on%20logmessages(MessageGroup,%20message,%20EventKind)%0A%09%09set%20MinSummaryLength%20to%205%0A%09%09set%20theLocation%20to%20%22Message%22%0A%09%09set%20eventdate%20to%20current%20date%0A%09%09if%20MessageGroup%20contains%20%22Messages%22%20then%20set%20EventKind%20to%20false%0A%09%09%0A%09%09set%20OldDelims%20to%20AppleScript's%20text%20item%20delimiters%0A%09%09set%20AppleScript's%20text%20item%20delimiters%20to%20%22%20%22%0A%09%09set%20theList%20to%20the%20text%20items%20of%20message%0A%09%09set%20AppleScript's%20text%20item%20delimiters%20to%20OldDelims%0A%09%09set%20x%20to%20count%20of%20items%20in%20theList%0A%09%09if%20x%20is%20greater%20than%20MinSummaryLength%20then%0A%09%09%09set%20i%20to%200%0A%09%09%09repeat%20with%20n%20from%201%20to%20x%0A%09%09%09%09set%20i%20to%20i%20+%201%0A%09%09%09%09if%20i%20is%201%20then%0A%09%09%09%09%09set%20LogTemp%20to%20item%20i%20of%20theList%0A%09%09%09%09else%0A%09%09%09%09%09set%20LogTemp%20to%20LogTemp%20%26%20%22%20%22%20%26%20item%20i%20of%20theList%0A%09%09%09%09end%20if%0A%09%09%09%09if%20i%20is%20MinSummaryLength%20then%20exit%20repeat%0A%09%09%09end%20repeat%0A%09%09%09set%20LogTemp%20to%20LogTemp%20%26%20%22...%22%0A%09%09else%0A%09%09%09set%20LogTemp%20to%20message%0A%09%09end%20if%0A%09%09LogIniCal(MessageGroup,%20LogTemp,%20message,%20theLocation,%20eventdate,%20EventKind)%0A%09end%20logmessages%0A%09%0A%09on%20LogDevice(DeviceName)%0A%09%09tell%20application%20%22Indigo%22%0A%09%09%09log%20%22Log%20Device%3A%20%22%20%26%20DeviceName%0A%09%09%09if%20(name%20of%20device%20DeviceName%20exists)%20then%0A%09%09%09%09set%20HouseMode%20to%20(value%20of%20variable%20%22HouseMode%22)%0A%09%09%09%09set%20eventdate%20to%20current%20date%0A%09%09%09%09--Device%20Info%0A%09%09%09%09set%20UnitDisc%20to%20description%20of%20device%20DeviceName%0A%09%09%09%09set%20unitstate%20to%20on%20state%20of%20device%20DeviceName%0A%09%09%09%09set%20UnitType%20to%20type%20of%20device%20DeviceName%0A%09%09%09%09set%20LogExtended%20to%20%22%22%0A%09%09%09%09set%20theLocation%20to%20UnitType%20%26%20%22%20%22%20%26%20UnitDisc%0A%09%09%09%09%0A%09%09%09%09if%20UnitType%20is%20%22Motion%20Detectors%22%20or%20unitstate%20is%20false%20then%0A%09%09%09%09%09set%20EventKind%20to%20false%0A%09%09%09%09else%0A%09%09%09%09%09set%20EventKind%20to%20true%0A%09%09%09%09end%20if%0A%09%09%09%09%0A%09%09%09%09--Send%20out%20LanOSD%20broadcast%20regardless%20of%20house%20state%20if%20there%20is%20an%20outside%20motion%20detector%20triggered%0A%09%09%09%09if%20UnitType%20is%20%22Motion%20Detector%22%20and%20UnitDisc%20contains%20%22outdoor%22%20then%0A%09%09%09%09%09try%0A%09%09%09%09%09%09tell%20application%20%22LanOSD%22%0A%09%09%09%09%09%09%09message%20kind%20UnitType%20text%20UnitType%20%26%20%22%22%20%26%20UnitDisc%20icon%20%22warning%22%20quadrant%208%20fade%20delay%204%0A%09%09%09%09%09%09end%20tell%0A%09%09%09%09%09end%20try%0A%09%09%09%09end%20if%0A%09%09%09%09(*%20log%20%22HouseMode%3A%20%22%20%26%20HouseMode%0A%09%09%09%09log%20%22eventdate%3A%20%22%20%26%20eventdate%0A%09%09%09%09log%20%22UnitDisc%3A%20%22%20%26%20UnitDisc%0A%09%09%09%09log%20%22unitstate%3A%20%22%20%26%20unitstate%0A%09%09%09%09log%20%22UnitType%3A%20%22%20%26%20UnitType%0A%09%09%09%09log%20%22LogExtended%3A%20%22%20%26%20LogExtended%0A%09%09%09%09log%20%22theLocation%3A%20%22%20%26%20theLocation%20*)%0A%09%09%09%09if%20UnitType%20is%20%22Motion%20Detector%22%20and%20HouseMode%20is%20%22away%22%20then%20LogIniCal(UnitType,%20DeviceName,%20LogExtended,%20theLocation,%20eventdate,%20EventKind)%0A%09%09%09%09if%20UnitType%20is%20not%20%22Motion%20Detector%22%20and%20UnitDisc%20does%20not%20contain%20%22RCS%22%20then%20LogIniCal(UnitType,%20DeviceName,%20LogExtended,%20theLocation,%20eventdate,%20EventKind)%0A%09%09%09end%20if%0A%09%09end%20tell%0A%09end%20LogDevice%0A%09%0A%09on%20LogIniCal(theCalName,%20theSummary,%20theNote,%20theLocation,%20eventdate,%20EventKind)%0A%09%09tell%20application%20%22iCal%22%0A%09%09%09--Get%20the%20Calendars%20from%20iCal%0A%09%09%09repeat%0A%09%09%09%09set%20i%20to%200%0A%09%09%09%09set%20theCalID%20to%200%0A%09%09%09%09repeat%20with%20CurrentCal%20in%20calendars%0A%09%09%09%09%09set%20i%20to%20i%20+%201%0A%09%09%09%09%09if%20title%20of%20CurrentCal%20is%20theCalName%20then%0A%09%09%09%09%09%09set%20theCalID%20to%20i%0A%09%09%09%09%09%09exit%20repeat%0A%09%09%09%09%09end%20if%0A%09%09%09%09end%20repeat%0A%09%09%09%09if%20theCalID%20is%20not%200%20then%0A%09%09%09%09%09exit%20repeat%0A%09%09%09%09else%0A%09%09%09%09%09create%20calendar%20with%20name%20theCalName%0A%09%09%09%09end%20if%0A%09%09%09end%20repeat%0A%0A%09%09%09set%20CompletedEvent%20to%20false%0A%09%09%09set%20EventCount%20to%20count%20of%20(events%20of%20calendar%20theCalID)%0A%09%09%09set%20i%20to%200%0A%09%09%09repeat%20with%20theevent%20in%20(events%20of%20calendar%20theCalID)%0A%09%09%09%09set%20i%20to%20i%20+%201%0A%09%09%09%09if%20summary%20of%20theevent%20is%20(theSummary)%20and%20EventKind%20is%20true%20and%20status%20of%20theevent%20is%20tentative%20and%20(start%20date)%20of%20theevent%20is%20(end%20date)%20of%20theevent%20then%0A%09%09%09%09%09--weve%20got%20an%20extra%20event%20that%20hasn't%20been%20closed%20off.%20Fix%20it%0A%09%09%09%09%09set%20summary%20of%20theevent%20to%20theSummary%0A%09%09%09%09%09--set%20(end%20date%20of%20theevent)%20to%20(start%20date%20of%20theevent)%0A%09%09%09%09%09set%20status%20of%20theevent%20to%20cancelled%0A%09%09%09%09%09set%20note%20of%20theevent%20to%20%22Unclosed%20event%20found,%20closing%20with%20unknown%20end%20date%22%0A%09%09%09%09else%20if%20summary%20of%20theevent%20is%20(theSummary)%20and%20EventKind%20is%20false%20and%20status%20of%20theevent%20is%20tentative%20and%20(start%20date)%20of%20theevent%20is%20(end%20date)%20of%20theevent%20then%0A%09%09%09%09%09--This%20is%20the%20last%20event%20that%20needs%20closed%0A%09%09%09%09%09set%20summary%20of%20theevent%20to%20theSummary%0A%09%09%09%09%09set%20(end%20date%20of%20theevent)%20to%20eventdate%0A%09%09%09%09%09set%20note%20of%20theevent%20to%20theNote%0A%09%09%09%09%09set%20status%20of%20theevent%20to%20confirmed%0A%09%09%09%09%09set%20CompletedEvent%20to%20true%0A%09%09%09%09end%20if%0A%09%09%09end%20repeat%0A%09%09%09if%20EventKind%20is%20true%20then%0A%09%09%09%09--Make%20new%20started%20event%0A%09%09%09%09make%20event%20at%20end%20of%20events%20of%20calendar%20theCalID%20with%20properties%20%7Bstart%20date%3Aeventdate,%20summary%3AtheSummary,%20description%3AtheNote,%20location%3AtheLocation,%20status%3Atentative%7D%0A%09%09%09else%20if%20CompletedEvent%20is%20false%20and%20EventKind%20is%20false%20then%0A%09%09%09%09make%20event%20at%20end%20of%20events%20of%20calendar%20theCalID%20with%20properties%20%7Bstart%20date%3Aeventdate,%20summary%3AtheSummary,%20description%3AtheNote,%20location%3AtheLocation,%20status%3Aconfirmed%7D%0A%09%09%09end%20if%0A%09%09end%20tell%0A%09end%20LogIniCal%0Aend%20using%20terms%20from%0A]Click here to open this script in a new Script Editor window[/url].

using terms from application "Indigo"
     on logmessages(MessageGroup, message, EventKind)
          set MinSummaryLength to 5
          set theLocation to "Message"
          set eventdate to current date
          if MessageGroup contains "Messages" then set EventKind to false
          
          set OldDelims to AppleScript's text item delimiters
          set AppleScript's text item delimiters to " "
          set theList to the text items of message
          set AppleScript's text item delimiters to OldDelims
          set x to count of items in theList
          if x is greater than MinSummaryLength then
               set i to 0
               repeat with n from 1 to x
                    set i to i + 1
                    if i is 1 then
                         set LogTemp to item i of theList
                    else
                         set LogTemp to LogTemp & " " & item i of theList
                    end if
                    if i is MinSummaryLength then exit repeat
               end repeat
               set LogTemp to LogTemp & "..."
          else
               set LogTemp to message
          end if
          LogIniCal(MessageGroup, LogTemp, message, theLocation, eventdate, EventKind)
     end logmessages
     
     on LogDevice(DeviceName)
          tell application "Indigo"
               log "Log Device: " & DeviceName
               if (name of device DeviceName exists) then
                    set HouseMode to (value of variable "HouseMode")
                    set eventdate to current date
                    --Device Info
                    set UnitDisc to description of device DeviceName
                    set unitstate to on state of device DeviceName
                    set UnitType to type of device DeviceName
                    set LogExtended to ""
                    set theLocation to UnitType & " " & UnitDisc
                    
                    if UnitType is "Motion Detectors" or unitstate is false then
                         set EventKind to false
                    else
                         set EventKind to true
                    end if
                    
                    --Send out LanOSD broadcast regardless of house state if there is an outside motion detector triggered
                    if UnitType is "Motion Detector" and UnitDisc contains "outdoor" then
                         try
                              tell application "LanOSD"
                                   message kind UnitType text UnitType & "" & UnitDisc icon "warning" quadrant 8 fade delay 4
                              end tell
                         end try
                    end if
                    (* log "HouseMode: " & HouseMode
                    log "eventdate: " & eventdate
                    log "UnitDisc: " & UnitDisc
                    log "unitstate: " & unitstate
                    log "UnitType: " & UnitType
                    log "LogExtended: " & LogExtended
                    log "theLocation: " & theLocation
*)
                    if UnitType is "Motion Detector" and HouseMode is "away" then LogIniCal(UnitType, DeviceName, LogExtended, theLocation, eventdate, EventKind)
                    if UnitType is not "Motion Detector" and UnitDisc does not contain "RCS" then LogIniCal(UnitType, DeviceName, LogExtended, theLocation, eventdate, EventKind)
               end if
          end tell
     end LogDevice
     
     on LogIniCal(theCalName, theSummary, theNote, theLocation, eventdate, EventKind)
          tell application "iCal"
               --Get the Calendars from iCal
               repeat
                    set i to 0
                    set theCalID to 0
                    repeat with CurrentCal in calendars
                         set i to i + 1
                         if title of CurrentCal is theCalName then
                              set theCalID to i
                              exit repeat
                         end if
                    end repeat
                    if theCalID is not 0 then
                         exit repeat
                    else
                         create calendar with name theCalName
                    end if
               end repeat

               set CompletedEvent to false
               set EventCount to count of (events of calendar theCalID)
               set i to 0
               repeat with theevent in (events of calendar theCalID)
                    set i to i + 1
                    if summary of theevent is (theSummary) and EventKind is true and status of theevent is tentative and (start date) of theevent is (end date) of theevent then
                         --weve got an extra event that hasn't been closed off. Fix it
                         set summary of theevent to theSummary
                         --set (end date of theevent) to (start date of theevent)
                         set status of theevent to cancelled
                         set note of theevent to "Unclosed event found, closing with unknown end date"
                    else if summary of theevent is (theSummary) and EventKind is false and status of theevent is tentative and (start date) of theevent is (end date) of theevent then
                         --This is the last event that needs closed
                         set summary of theevent to theSummary
                         set (end date of theevent) to eventdate
                         set note of theevent to theNote
                         set status of theevent to confirmed
                         set CompletedEvent to true
                    end if
               end repeat
               if EventKind is true then
                    --Make new started event
                    make event at end of events of calendar theCalID with properties {start date:eventdate, summary:theSummary, description:theNote, location:theLocation, status:tentative}
               else if CompletedEvent is false and EventKind is false then
                    make event at end of events of calendar theCalID with properties {start date:eventdate, summary:theSummary, description:theNote, location:theLocation, status:confirmed}
               end if
          end tell
     end LogIniCal
end using terms from


-------------------------
[This script was automatically tagged for color coded syntax by Script to Markup Code]

Posted on
Sat Jun 19, 2004 11:30 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

I've done some interesting things with ical lately. Since I'm logging all this data I've decided to get some average event times to predict how long to automatically turn lights on for.

You can read more about it here.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 7 guests