Skip navigation

How to use Applescript to print out a certain calendar at 7am every morning?

945 Views 3 Replies Latest reply: Aug 14, 2013 2:52 PM by Bernard Harte RSS
ksm2587 Calculating status...
Currently Being Moderated
Aug 8, 2013 8:40 AM

Hey guys,

 

I am trying to use AppleScript to print out one of my calendars each day at 7am every morning.

 

I have come up with this so far:

 

tell application "Calendar"

       view calendar "My Appointments" at (my (current date))

       switch view to day view

       activate

                    tell application "System Events"

                 keystroke "p" using command down

                 delay 1 -- (seconds)

                 tell process "Calendar"

                                                  tell window "Print"

                                                                 if value of checkbox "Calendar Keys" is not 0 then

                                  click checkbox "Calendar Keys"

                           end if

                                                  end tell

                                   end tell

                    end tell

end tell

 

 

The problem with this is that it will print out whatever calendars are currently viewable. I can't figure out how to tell it to deselect other calendars and just select the one I want. I also want to tell it to print this out at 7am every morning.

 

Any help would be very appreciated!

MacBook Pro, OS X Mountain Lion (10.8.4), AppleScript
  • Bernard Harte Level 4 Level 4 (3,025 points)

    Instead of trying to drive the Calendar interface through AppleScript, you would be better off getting the events for the day from the app's database, putting those into a (temporary) file and then printing it.

     

    The script below does just that.  I have annotated a bit so that you can see what's happening.  It's a bit convoluted because events are stored in the order they are created - rather than chronologically - so once they're retrieved they have to be sorted.  Start and end times are derived from the date objects and formatted before being added to the end of the file.

     

    You can get it to execute each day at a certain time either by saving as an app and using Calendar to trigger it (probably from a dedicated calendar you set up for that purpose with a repeating event each day at 7am), launchd (Google it), or you can use an app like Lingon X to give you a GUI to launchd.

     

    There is no error handing, and although the script should not be destructive you should make sure you have a backup before running it.

     

    --opens a temporary file

    set fileRef to open for access ((path to desktop) as string) & "meetings.txt" with write permission

    --effectively empties the file

    set eof fileRef to 0

    tell application "Calendar"

              set startList to {}

      --get's today's date

      --set startRange to ((current date) + 12 * days) -- I used this for testing, to point to a day that had many appointments.  I've left it so you can use it, too.

              set startRange to current date

      --set the start of the range to previous midnight

              set hours of startRange to 0

              set minutes of startRange to 0

              set seconds of startRange to 0

      --set end of the range to next midnight

              set endRange to (startRange + 1 * days)

      --get today's events

              set todaysEvents to get events of calendar "My Appointments" whose (start date is greater than startRange) and (end date is less than endRange)

      --make a new list with start time of the event for sort purposes

              repeat with theEvent in todaysEvents

                        set end of startList to {startdate:start date of theEvent, eventID:theEvent}

              end repeat

     

      --sort using a bubble sort (suitable for short lists)

              repeat with i from 1 to (count of startList) - 1

                        repeat with j from i + 1 to count of startList

                                  if startdate of item j of startList < startdate of item i of startList then

                                            set temp to item i of startList

                                            set item i of startList to item j of startList

                                            set item j of startList to temp

                                  end if

                        end repeat

              end repeat

     

      --empty the original list

              set todaysEvents to {}

      --repopulate the list in correct chronological order

              repeat with theEvent in startList

                        set end of todaysEvents to eventID of theEvent

              end repeat

     

      --write today's date to the file

      write (date string of startRange & return & return) to fileRef

     

      --if there are no events, write this to the file as well

              if length of todaysEvents is 0 then

                        write "No events" to fileRef

              else

      --process each event into format: hh:mm-hh:mm/new line/event summary

     

                        repeat with theEvent in todaysEvents

                                  set startdate to start date of theEvent

                                  set enddate to end date of theEvent

                                  set startHours to hours of startdate as string

                                  if length of startHours is 1 then set startHours to "0" & startHours

                                  set startMins to minutes of startdate as string

                                  if length of startMins is 1 then set startMins to "0" & startMins

                                  set starttime to startHours & ":" & startMins

                                  set endHours to hours of enddate as string

                                  if length of endHours is 1 then set endHours to "0" & endHours

                                  set endMins to minutes of enddate as string

                                  if length of endMins is 1 then set endMins to "0" & endMins

                                  set endTime to endHours & ":" & endMins

      --write the event to the file

                                  set theSummary to (starttime & "-" & endTime & return & summary of theEvent & return & return) as string

      write theSummary to fileRef

                        end repeat

              end if

    end tell

    --close the file

    close access fileRef

     

    --print the file

    set theFile to (((path to desktop) as text) & "meetings.txt") as alias

    tell application "TextEdit"

      print theFile without print dialog

    end tell

  • Bernard Harte Level 4 Level 4 (3,025 points)

    If you wanted to do the printout over multiple days, you would be best to loop through each day's events in the way the current script does, but to put a new loop around it that gets each new day's data:

     

    --opens a temporary file

    set fileRef to open for access ((path to desktop) as string) & "meetings.txt" with write permission

    --effectively empties the file

    set eof fileRef to 0

    tell application "Calendar"

              set dayCount to 9 --this will be the number of days to look forward (not including today)

              repeat with currentDay from 0 to dayCount

                        set startList to {}

      --get's today's date

      --set startRange to ((current date) + 12 * days) -- I used this for testing, to point to a day that had many appointments.  I've left it so you can use it, too.

                        set startRange to ((current date) + (currentDay * days))

      --set the start of the range to previous midnight

                        set hours of startRange to 0

                        set minutes of startRange to 0

                        set seconds of startRange to 0

      --set end of the range to next midnight

                        set endRange to (startRange + 1 * days)

      --get today's events

                        set todaysEvents to get (events of calendar "My Appointments" whose (start date is greater than startRange) and (end date is less than endRange))

      --make a new list with start time of the event for sort purposes

                        repeat with theEvent in todaysEvents

                                  set end of startList to {startdate:start date of theEvent, eventID:theEvent}

                        end repeat

     

      --sort using a bubble sort (suitable for short lists)

                        repeat with i from 1 to (count of startList) - 1

                                  repeat with j from i + 1 to count of startList

                                            if startdate of item j of startList < startdate of item i of startList then

                                                      set temp to item i of startList

                                                      set item i of startList to item j of startList

                                                      set item j of startList to temp

                                            end if

                                  end repeat

                        end repeat

     

      --empty the original list

                        set todaysEvents to {}

      --repopulate the list in correct chronological order

                        repeat with theEvent in startList

                                  set end of todaysEvents to eventID of theEvent

                        end repeat

     

      --write today's date to the file

      write (date string of startRange & return & return) to fileRef

     

      --if there are no events, write this to the file as well

                        if length of todaysEvents is 0 then

                                  write "No events" & return & return to fileRef

                        else

      --process each event into format: hh:mm-hh:mm/new line/event summary

     

                                  repeat with theEvent in todaysEvents

                                            set startdate to start date of theEvent

                                            set enddate to end date of theEvent

                                            set startHours to hours of startdate as string

                                            if length of startHours is 1 then set startHours to "0" & startHours

                                            set startMins to minutes of startdate as string

                                            if length of startMins is 1 then set startMins to "0" & startMins

                                            set starttime to startHours & ":" & startMins

                                            set endHours to hours of enddate as string

                                            if length of endHours is 1 then set endHours to "0" & endHours

                                            set endMins to minutes of enddate as string

                                            if length of endMins is 1 then set endMins to "0" & endMins

                                            set endTime to endHours & ":" & endMins

      --write the event to the file

                                            set theSummary to (starttime & "-" & endTime & return & summary of theEvent & return & return) as string

      write theSummary to fileRef

                                  end repeat

                        end if

      write return to fileRef

              end repeat

    end tell

    --close the file

    close access fileRef

     

    --print the file

     

    (*

    set theFile to (((path to desktop) as text) & "meetings.txt") as alias

    tell application "TextEdit"

              print theFile without print dialog

    end tell

    *)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.