Mavericks Calendar - exporting calendars as .ics files

Discussion in 'Mac Programming' started by Schubi, Nov 1, 2013.

  1. Schubi macrumors newbie

    Joined:
    Mar 22, 2013
    #1
    Following up on my thread Mountain Lion Calendar - exporting calendars as .ics files I was wondering if anyone could take a look at the Mavericks Calendar.

    The script doesn't seem to work anymore and the failure message I receive is:

    I assume it's a trivial fix but I've been unable to find the problem. Any help is much appreciated. Thanks!
     

    Attached Files:

  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Try this :

    Code:
    -- Selecting calendars in Calendar and export using GUI scripting
    -- Change the backup_folder path!
    -- Don't click anywhere else when the script is running!
    
    set timeStamp to do shell script "date \"+%Y%m%d\""
    set backup_folder to "/Users/kryten/Backup/Calendar/"
    do shell script "mkdir -p " & quoted form of backup_folder & quoted form of timeStamp
    set savePath to backup_folder & timeStamp
    
    -- Begin script
    tell application "Calendar"
    	launch
    	activate
    	delay 1
    	set calNames to name of every calendar
    end tell
    
    tell application "System Events"
    	tell process "Calendar"
    		
    		set frontmost to true
    		
    		-- Left-hand menu of Calendar, containing named calendars	
    		set myOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Calendar"
    		set allRows to rows of myOutline
    		
    		-- List of named calendars
    		set calNameVals to get value of static text of UI element 1 of every row of myOutline
    		
    		-- For all named calendars, check whether it's the same name as one of calNames
    		-- If so, export that calendar.
    		
    		set countLoop to 0
    		repeat with i from 2 to (count calNameVals)
    			set countLoop to countLoop + 1
    			set calName to item 1 of item i of calNameVals
    			repeat with myName in calNames
    				if (myName as string = calName as string) then
    					tell row i of myOutline
    						select
    						delay 1
    					end tell
    					click menu item "Export…" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
    					delay 2
    					if countLoop = 1 then
    						keystroke "g" using {command down, shift down}
    						delay 1
    						keystroke savePath
    						delay 1
    						click button "Go" of sheet 1 of sheet 1 of window 1
    					end if
    					click button "Export" of sheet 1 of window 1
    				end if
    			end repeat
    		end repeat
    	end tell
    end tell
    
    Note : Before running the script please make sure your Birthdays calendar is selected otherwise the script will fail. I know why it fails but don't really have time to fix it.
     

    Attached Files:

  3. Schubi, Nov 2, 2013
    Last edited: Apr 29, 2014

    Schubi thread starter macrumors newbie

    Joined:
    Mar 22, 2013
    #3
    Updated version that includes an additional 'delay' entry that helps with exporting large calendars. Previously, the script would abort with an error, when trying to start working on the next calendar whilst still processing/exporting the current one.

     
  4. Loyd-01a macrumors member

    Joined:
    Oct 10, 2014
    #4
    Hello Schubi,

    I try to use your script (under Mavericks 10.9.5) but I get this error :
    it's impossible to get window "Calendar" of process "Calendar"
    (pls see image attached).

    Could you please guide me how to address it ?

    Thanks in advance for your time.
    Wonderful forum !!!

    Loyd
     

    Attached Files:

  5. kryten2, Oct 11, 2014
    Last edited: Oct 11, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    Try this with window/button/menu item names en français :

    Code:
    -- Basic settings
    set timeStamp to do shell script "date \"+%Y%m%d\""
    set backup_folder to "/Users/kryten/Backup/"
    do shell script "mkdir -p " & quoted form of backup_folder & quoted form of timeStamp
    set savePath to backup_folder & timeStamp
    
    -- Begin script
    tell application "Calendar"
    	launch
    	activate
    	delay 2
    	set calNames to name of every calendar
    end tell
    
    tell application "System Events"
    	tell process "Calendar"
    		set frontmost to true
    		
    		-- Left-hand menu of Calendar, containing named calendars	
    		set myOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "[B]Calendrier[/B]"
    		set allRows to rows of myOutline
    		
    		-- List of named calendars
    		set calNameVals to get value of static text of UI element 1 of every row of myOutline
    		
    		-- For all named calendars, check whether it's the same name as one of calNames, if so export that calendar
    		set countLoop to 0
    		repeat with i from 2 to (count calNameVals)
    			set countLoop to countLoop + 1
    			set calName to item 1 of item i of calNameVals
    			repeat with myName in calNames
    				if (myName as string = calName as string) then
    					tell row i of myOutline
    						select
    						delay 1
    					end tell
    					click menu item "[B]Exporter…[/B]" of menu 1 of menu item "[B]Exporter[/B]" of menu 1 of menu bar item "[B]Fichier[/B]" of menu bar 1
    					delay 1
    					if countLoop = 1 then
    						keystroke "g" using {command down, shift down}
    						delay 1
    						keystroke savePath
    						delay 1
    						click button "[B]Aller[/B]" of sheet 1 of sheet 1 of window 1
    					end if
    					click button "[B]Exporter[/B]" of sheet 1 of window 1
    					delay 15 -- Change the delay time if your computer is running to slow or too fast
    				end if
    			end repeat
    		end repeat
    	end tell
    end tell
    Index reference form :

    Code:
    set myOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window [B]1[/B]
    click menu item [B]1[/B] of menu 1 of menu item [B]7[/B] of menu 1 of menu bar item [B]3[/B] of menu bar 1
    click button [B]1[/B] of sheet 1 of sheet 1 of window 1
    click button [B]1[/B] of sheet 1 of window 1
     
  6. Loyd-01a, Oct 17, 2014
    Last edited: Oct 17, 2014

    Loyd-01a macrumors member

    Joined:
    Oct 10, 2014
    #6
    Thank you Kryten2 !

    I use your suggestions without modifying anything (just the first code not the second "Index reference form :..." wich I do not understand where to include it :))

    I get this second error (pls see image)
    What can I modify to got the script works ?

    Thanks for your time

    Edit : Wrong image attached : corrected !
     

    Attached Files:

  7. kryten2, Oct 18, 2014
    Last edited: Oct 18, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    You are aware that this is for Mavericks right? Try to enable access for assistive devices and applications.

    Info : OS X Mavericks: Enable access for assistive devices and applications and OS X: Using AppleScript with Accessibility and Security features in Mavericks and How to Enable Accessibility on Mac OS X

     
  8. Schubi thread starter macrumors newbie

    Joined:
    Mar 22, 2013
    #8
    Attaching the Mavericks script. Please rename .txt to .scpt in order to run it from the Script Editor.app. Don't forget to enable access for assistive devices and applications.

    If you are looking for a script that runs under 10.10, please check the following thread: Yosemite Calendar - exporting calendars as .ics files
     

    Attached Files:

Share This Page