Exporting from Calendar app

Not done a lot of scripting but been OK with a few so far but can’t work this one out.

All I want to do is have a script that will tell the Calendar app to export a calendar. I think my problem is when the export dialog appears and needs “Export” button pressed, I’m on the wring window. Anyone got any tips? Thanks!

I think that you may found all what you need below.

my select_SubMenu("Calendar", 3, 7, 1)

#=====
(*
useful to get the indexs of the triggered item
my select_SubMenu("Numbers", 6, 14, 3) (* Table > Footer rows > 2 *)
*)
on select_SubMenu(theApp, mt, mi, ms)
	tell application theApp
		activate
		get version # ADDED
		log result (*8.0*)
	end tell
	tell application "System Events" to tell process theApp
		set frontmost to true
		tell menu bar 1
			get name of menu bar items
			log result
			(*{
01 - "Apple", 
02 - "Calendrier", 
03 - "Fichier", 
04 - "Édition", 
05 - "Présentation", 
06 - "Fenêtre", 
07 - "Aide"}
*)
			get name of menu bar item mt
			log result
			-- {"Fichier"}
			tell menu bar item mt to tell menu 1
				get name of menu items
				log result
				(* {
01 - "Nouvel événement", 
02 - "Nouveau calendrier", 
03 - "Nouveau groupe de calendriers", 
04 - "Abonnement à un calendrier.", 
05 - missing value, 
06 - "Importer.", 
07 - "Exporter", 
08 - "Fermer", 
09 - "Tout fermer", 
10 - missing value, 
11 - "Imprimer."}
*)
				get name of menu item mi
				log result
				--> "Exporter"
				tell menu item mi to tell menu 1
					get name of menu items
					log result
					(* {
1 - "Exporter.", 
2 - "Archive de Calendrier."}
*)
					get name of menu item ms
					log result
					--> "Exporter."
					click menu item ms
				end tell -- menu item.
			end tell
		end tell -- menu bar.
	end tell -- System Events
end select_SubMenu

#=====

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) dimanche 7 février 2016 20:57:49

Thanks. That still returns an error thus:

error “System Events got an error: Can’t get menu 1 of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process "Calendar". Invalid index.” number -1719 from menu 1 of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”

I’m missing something obvious I think but, I can’t see it!!!

Here it works flawlessly.

May you make an other try with the version available above?
I added some code to get the version of the running Calendar.
Here the Events log is :
]tell application “Calendar”
activate
u[/u]
end tell
tell application “System Events”
set frontmost of process “Calendar” to true
get name of every menu bar item of menu bar 1 of process “Calendar”
(Apple, Calendrier, Fichier, Édition, Présentation, Fenêtre, Aide)
get name of menu bar item 3 of menu bar 1 of process “Calendar”
(Fichier)
get name of every menu item of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”
(Nouvel événement, Nouveau calendrier, Nouveau groupe de calendriers, Abonnement à un calendrier., missing value, Importer., Exporter, Fermer, Tout fermer, missing value, Imprimer.)
get name of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”
u[/u]
get name of every menu item of menu 1 of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”
(Exporter., Archive de Calendrier.)
get name of menu item 1 of menu 1 of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”
(Exporter.)
click menu item 1 of menu 1 of menu item 7 of menu 1 of menu bar item 3 of menu bar 1 of process “Calendar”
end tell

I highlighted the meaningful informations.
If you aren’t running 10.11.x, these infos may be different and, as I’m not a sooth sayer I can’t guess which is the system in use on your machine.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 8 février 2016 11:26:05

Thanks, it is working now. I am running 10.11.3 but in English.

I now need to work out how to select a particular calendar to backup. Can you help with that also? :-))

Here is a script allowing you to select a calendar and export it to the Desktop.
I tested it under 10.11.3 in French but as far as I know, the language doesn’t matter here.

tell application "Calendar"
	set itsVersion to version
	if itsVersion < "6.0" then error "Don't know the content of File menu in Calendar older than 6.0"
	# Get the list of available calendars
	set theCalendars to name of calendars
end tell

# Choose one calendar
set aCalendar to choose from list theCalendars
# Check if we choose to cancel
if aCalendar is false then error number -128
# choose from list returns a list so extract the first (unique) item of the list
set aCalendar to aCalendar's item 1

set maybe to my selectCalendar(aCalendar)
if not maybe then error "Don't find calendar : " & aCalendar

if itsVersion < "8.0" then
	my select_SubMenu("Calendar", 3, 6, 1) # if Calendar v7.x
else
	my select_SubMenu("Calendar", 3, 7, 1) # if Calendar v8.x
end if

tell application "System Events" to tell process "Calendar"
	set frontmost to true
	tell window 1
		(*
class of UI elements
log result (*splitter group, button, button, button, toolbar, sheet*)
		*)
		tell sheet 1
			# set destination folder to Desktop
			keystroke "d" using {command down}
			(*
class of UI elements
log result (*button, button, button, group, checkbox, text field, static text, static text, text field*)
name of buttons
log result (*Exporter, Nouveau dossier, Annuler*)
			*)
			click button 1
		end tell
	end tell
end tell

#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#

on selectCalendar(theCalendar)
	tell application "System Events" to tell process "Calendar"
		set frontmost to true
		tell window 1
			(*
class of UI elements
log result (*splitter group, button, button, button, toolbar*)
			*)
			tell first splitter group
				(*
class of UI elements
log result (*splitter group, splitter, group, splitter*)
				*)
				tell splitter group 1
					(*
class of UI elements
log result (*scroll area, splitter, static text, button, button, UI element*)
					*)
					tell scroll area 1
						(*
class of UI elements
log result (*outline*)
						*)
						tell outline 1
							(*
class of UI elements
log result (*row, row, row, row, row, row, row, column*)
							*)
							set nbRows to (count rows)
							set gotIt to false
							repeat with aRow from 1 to nbRows
								tell row aRow
									(*
class of UI elements
log result (*UI element*)
									*)
									tell UI element 1
										(*
class of UI elements
log result
(*text field, button, checkbox*)
(*text field, checkbox*)
(*static text, checkbox*)
										*)
										set aName to value of UI element 1
										-- log result
										if aName = theCalendar then
											set gotIt to true
											exit repeat
										end if
									end tell
								end tell # row arow
							end repeat
							if gotIt then
								tell row aRow
									set {x, y} to position
									set {w, h} to size
								end tell
								do shell script "/Users/admin/bin/cliclick c:" & (x + w div 2) & "," & (y + h div 2)
								return true
							else
								return false
							end if
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end selectCalendar

#=====

on select_SubMenu(theApp, mt, mi, ms)
	tell application theApp
		activate
		get version
		log result (*8.0*)
	end tell
	tell application "System Events" to tell process theApp
		set frontmost to true
		tell menu bar 1
			(*
get name of menu bar items
log result
			*)
			(*{
01 - "Apple", 
02 - "Calendrier", 
03 - "Fichier", 
04 - "Édition", 
05 - "Présentation", 
06 - "Fenêtre", 
07 - "Aide"}
*)
			get name of menu bar item mt
			log result
			-- {"Fichier"}
			tell menu bar item mt to tell menu 1
				(*
				name of menu items
				log result 
				*)
				(* {
01 - "Nouvel événement", 
02 - "Nouveau calendrier", 
03 - "Nouveau groupe de calendriers", 
04 - "Abonnement à un calendrier.", 
05 - missing value, 
06 - "Importer.", 
07 - "Exporter", 
08 - "Fermer", 
09 - "Tout fermer", 
10 - missing value, 
11 - "Imprimer."}
*)
				(*
name of menu item mi
log result(*Exporter*)
				*)
				tell menu item mi to tell menu 1
					(*
name of menu items
log result 
					*)
					(* {
1 - "Exporter.", 
2 - "Archive de Calendrier."}
*)
					(*
name of menu item ms
log result (*Exporter.*)
					*)
					click menu item ms
				end tell -- menu item.
			end tell
		end tell -- menu bar.
	end tell -- System Events
end select_SubMenu

#=====

Edited so that :
it refuse to drive Calendar older than v7.0
it makes the difference between Calendar v8.x (Export is menu item 6) and v8.x (Export is menu item 7)

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 8 février 2016 16:47:57

That looks like it’s great - just a small issue, I am missing a script that you seem to be calling:

do shell script “/Users/admin/bin/cliclick c:” & (x + w div 2) & “,” & (y + h div 2)

Is cliclick something you’ve created?

Thanks very much!

No, clicclick is not one product of mine.

You may get it at :
http://www.bluem.net/en/mac/cliclick/

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) lundi 8 février 2016 17:18:14

Thanks! That’s it working now. Superb!

I edited the script so that :
it refuses to drive Calendar older than v7.0
it makes the difference between Calendar v8.x (Export is menu item 6) and v8.x (Export is menu item 7).

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) mercredi 10 février 2016 15:34:09