6 Replies Latest reply on May 3, 2014 4:47 PM by sporobolus

    Help speed up Applescript

    KylePutzier

      I don't know enough about Applescript so I've made up a calculated applescript from examples I've found on the web.

      It is very simple.

      It hides the OS X Calendar.app window.

      Deletes an event if it exist.

      And then makes a new event.

       

      It's pretty slow, about 1.5 seconds per event.

      Is there a more efficient way to script it. I suspect it is the delete event that is the issue.

       

       

       

      tell application "Calendar"

      activate

      set visible of every window to false

      delete (every event of calendar "Inventive Label" whose summary contains "1418")

      make new event at end of events of calendar "Inventive Label" with properties {allday event:false, start date:date "5/1/2014 2:30 PM", end date:date "5/1/2014 5:30 PM", summary:"1418; 157419-1 Rev A", location:"Press 1", description:"Acme Corp."}

      end tell

        • 1. Re: Help speed up Applescript
          mikebeargie

          1.5 seconds actually seems reasonable for it to search the entire calendar for the matching event to delete.

           

          I would say you'll see a larger performance gain from clearing out/archiving past events in the user's calendars (IE smaller search segment) than retooling your applescript.

           

          How many event records are in the calendar you are testing on?

          • 2. Re: Help speed up Applescript
            KylePutzier

            There are probably only a few thousand entries in the calendar. Can a shell script be used to Delete / Create events?

            • 3. Re: Help speed up Applescript
              mikebeargie

              Sorry, not really deep enough into shell scripts to be able to tell you.

              • 4. Re: Help speed up Applescript
                sporobolus

                on 2014-05-02 13:16 KylePutzier wrote

                There are probably only a few thousand entries in the calendar. Can a shell script be used to Delete / Create events?

                 

                there's not a native way, other than a call to osascript to run an AppleScript,

                which i doubt would be faster; someone may have written shell tools to access

                the calendar data, but i doubt you'd find one specific enough to delete events

                based on text search criteria

                 

                your script is written reasonably; on the face of it i agree with Mike that 1.5

                seconds is not too surprising; AppleScript performance is poor in many of

                Apple's own apps

                 

                but in fact it is silly-slow, and it appears to be the query, not the delete,

                which is slow; i rewrote the script in an "unreasonable" way for a surprising

                result; here is a comparison of two methods to access 33 events in a calendar

                with 1496 items; the brute force method is much faster:

                 

                 

                -- timings for two different approaches to searching for calendar events
                
                -- the natural approach — allow iCal to process the whose query
                set start1 to time of (current date)
                tell application "iCal"
                   tell calendar "shared"
                     set l1 to uid of every event whose summary contains "$"
                   end tell
                end tell
                set end1 to time of (current date)
                
                -- brute force, non-AppleScripty way — get a list of all the summaries and 
                search them individually
                set start2 to time of (current date)
                tell application "iCal"
                   tell calendar "shared"
                     set es to {summary, uid} of every event
                          
                     set l2 to {}
                     set c to count of item 1 of es
                     repeat with i from 1 to c
                       if item i of item 1 of es contains "$" then
                         set end of l2 to {item i of item 2 of es}
                       end if
                     end repeat
                   end tell
                end tell
                set end2 to time of (current date)
                
                -- show the timings
                return "natural approach: " & end1 - start1 & " seconds; brute force: " & end2 
                - start2 & " seconds"
                
                --> "natural approach: 10 seconds; brute force: 1 seconds"
                

                 

                i didn't do any deletions on my calendar, but i suspect using method 2, then

                deleting as follows, will speed things up (not tested)

                 

                repeat with event_id in l2
                   tell application "iCal"
                     tell calendar "shared"
                       delete event id event_id
                     end tell
                   end tell
                end repeat
                

                 

                • 5. Re: Help speed up Applescript
                  Malcolm

                  There are probably only a few thousand entries in the calendar. Can a shell script be used to Delete / Create events?

                   

                  The backend db is sqlite.

                   

                  malcolm

                  • 6. Re: Help speed up Applescript
                    sporobolus

                    on 2014-05-02 19:34 Malcolm wrote

                    The backend db is sqlite.

                     

                    good thought - i'd be careful to make sure iCloud will automatically sync any

                    changes you make directly to the db