Scripting iCal's print dialog

I am trying to get iCal to print a specific calendars and ToDo’s in a list. This is all doable via the print dialog, but I want to automate it. I found this script that manipulates iCal print dialog via System Events:

t
ell application "iCal"
	view calendar at (my (current date))
	switch view to day view
	activate
	tell application "System Events"
		keystroke "p" using command down
		delay 1 -- (seconds)
		keystroke return
		delay 2 -- (seconds)
		keystroke return
	end tell
	quit
end tell

The problem I am having is finding out the other properties of the dialog window so that I can call and trigger them (i.e. specific calendars and todos in list format). I tried looking through the scripting libraries for iCal and System Events but can’t figure it out. I’ve used this script for getting a window’s properties:


tell application "Finder"
	get properties of window 1
end tell

Is there some similar trick for getting the properties of a print dialog window? Is there a source where I can find all the scriptable items for an individual dialog box?

Hi, jshock58. Welcome to MacScripter.

No source, per se. The contents of a window in GUI Scripting depend on what version of what app you’re running on what system version in what language! The way I’ve normally looked for things is with exploratory scripts like this .

tell application "System Events"
	tell application process "Blah" -- Where "Blah" the application of interest as seen by System Events.
		tell front window
			return UI elements
		end
	end
end

. which I’ve then expanded by adding anything interesting from the result, say:

tell application "System Events"
	tell application process "Blah"
		tell front window
			return UI elements of splitter group 1 -- Or whatever.
		end
	end
end

But in another thread in this forum, we’ve just knocked together a script which displays the entire contents of an application process’s front window and menu bar in TextEdit. Using that and a bit of experimenting, I came up with the following expansion of your script, which works with iCal 4.0.4 in Mac OS 10.6.8 and where the user language is English:


-- Adjust these properties to your own requirements:
property calendarsToPrint : {"Home", "Birthdays etc."}
property options : {"All-day events", "To do items with due dates", "Details", "Phone numbers", "Mini calendar", "Calendar keys"}
property textSize : "Small"

tell application "iCal"
	view calendar at (my (current date))
	switch view to day view
	activate
end tell

tell application "System Events"
	keystroke "p" using {command down}
	tell application process "iCal"
		tell front window
			-- Wait until the probable "Print" window is frontmost.
			repeat until (pop up button 1 exists)
				delay 0.2
			end repeat
			
			-- Get the checkboxed calendar rows
			set calendarRows to rows of (outline 1 of scroll area 1 of window "Print" of application process "iCal" of application "System Events") whose UI elements is not {}
			-- Click each checkbox whose state is a mismatch for the calendars we want to print.
			repeat with thisRow in calendarRows
				tell thisRow
					if ((value of checkbox 1 is 0) = (value of static text 1 is in calendarsToPrint)) then perform action "AXPress" of checkbox 1
				end tell
			end repeat
			
			-- Click each Options checkbox whose state is a mismatch for what's specified in the script properties.
			repeat with thisCheckBox in checkboxes
				if ((thisCheckBox's value is 0) = (thisCheckBox's name is in options)) then perform thisCheckBox's action "AXPress"
			end repeat
			
			-- Select the text size, if necessary.
			set textSizePopUp to first pop up button whose name is missing value
			tell textSizePopUp
				if (its value is not textSize) then
					perform action "AXPress"
					repeat until (menu 1 exists)
						delay 0.2
					end repeat
					perform action "AXPress" of menu item textSize of menu 1
					repeat while (menu 1 exists)
						delay 0.2
					end repeat
				end if
			end tell
			
			-- Go on to the next Print dialog.
			keystroke return
			repeat until (static text "Printer:" exists)
				delay 0.2
			end repeat
			
			-- The contents of the second dialog probably depend on your printer.
			
		end tell
		
		-- Press return and wait for the dialog to disappear.
		set c to (count windows)
		keystroke return
		repeat until ((count windows) < c)
			delay 0.2
		end repeat
	end tell
end tell

tell application "iCal" to quit

Nigel, this is fantastic! Thanks, this should help alot.

Hello, I am trying to utilize the previously posted script (scripting the print dialog of iCal), with the exception that I would like to use the “Save as PDF…” option in the print dialog. Lion OS. I have tried pasting various scriptlets–pardon the commented out lines.

Attached is my effort–which failed–hanging at the “Print” dialog instead of selecting the “Save as PDF…” option.

Thanks for any advice–


-- Adjust these properties to your own requirements:
property calendarsToPrint : {"BHVH Coronary CT"}
property options : {"All-day events"}
property textSize : "Medium"

tell application "iCal"
	view calendar at (my (current date))
	switch view to month view
	activate
end tell

tell application "System Events"
	keystroke "p" using {command down}
	tell application process "iCal"
		tell front window
			-- Wait until the probable "Print" window is frontmost.
			repeat until (pop up button 1 exists)
				delay 0.2
			end repeat
			
			-- Get the checkboxed calendar rows
			set calendarRows to rows of (outline 1 of scroll area 1 of window "Print" of application process "iCal" of application "System Events") whose UI elements is not {}
			-- Click each checkbox whose state is a mismatch for the calendars we want to print.
			repeat with thisRow in calendarRows
				tell thisRow
					if ((value of checkbox 1 is 0) = (value of static text 1 is in calendarsToPrint)) then perform action "AXPress" of checkbox 1
				end tell
			end repeat
			
			-- Click each Options checkbox whose state is a mismatch for what's specified in the script properties.
			repeat with thisCheckBox in checkboxes
				if ((thisCheckBox's value is 0) = (thisCheckBox's name is in options)) then perform thisCheckBox's action "AXPress"
			end repeat
			
			-- Select the text size, if necessary.
			set textSizePopUp to first pop up button whose name is missing value
			tell textSizePopUp
				if (its value is not textSize) then
					perform action "AXPress"
					repeat until (menu 1 exists)
						delay 0.2
					end repeat
					perform action "AXPress" of menu item textSize of menu 1
					repeat while (menu 1 exists)
						delay 0.2
					end repeat
				end if
			end tell
			
			-- Go on to the next Print dialog.
			
			--  keystroke "p" using {command down}
			
		end tell
		keystroke return
		
		tell window 1
			--tell sheet 1 -- "Print" dialog.
			repeat until (it exists)
				delay 0.2
			end repeat
			tell menu button "PDF" -- "PDF" button.
				click
				tell menu item 2 of menu 1 -- Menu item "Save as PDF.".
					repeat until (it exists)
						delay 0.2
					end repeat
					click
				end tell
				--end tell
				--repeat while (it exists) -- "Print" dialog.
				--	delay 0.2
				--end repeat
				-- When "sheet 1 of window 1" no longer exists, it means that the "Save" dialog is now window 1!
			end tell
			-- Put the rest of your save code here.
		end tell
		
		
		--end tell
		--  if value of checkbox "Calendar Keys" is not 0 then
		
		--   click checkbox "Calendar Keys"
		--keystroke return
		--	print list of file or specifier : The file(s), document(s), or window(s) to be printed.
		--[with properties print settings] : The print settings to use.
		--[print dialog boolean] : Should the application show the print dialog?
		--repeat until (static text "Printer:" exists)
		--	delay 0.2
		--end repeat
		
		
		--tell window 1
		--tell sheet 1 -- "Print" dialog.
		--	repeat until (it exists)
		--	delay 0.2
		--end repeat
		--tell menu button "PDF" -- "PDF" button.
		--click
		--tell menu item 2 of menu 1 -- Menu item "Save as PDF.".
		--	repeat until (it exists)
		--	delay 0.2
		--end repeat
		--click
		--end tell
		--end tell
		--repeat while (it exists) -- "Print" dialog.
		--	delay 0.2
		--end repeat
		-- When "sheet 1 of window 1" no longer exists, it means that the "Save" dialog is now window 1!
		--end tell
		-- Put the rest of your save code here.
	end tell
	--tell window "Print"
	--tell menu button "PDF" of window "Print"
	
	--click
	
	-- delay 1
	--click menu item "Save as PDF." of menu 1
	
	--end tell
	--click menu button "PDF"
	--	delay 1
	--click menu item "Save as PDF."
	--delay 1
	--	keystroke return
	-- The contents of the second dialog probably depend on your printer.
	
end tell


-- Press return and wait for the dialog to disappear.
set c to (count windows)
keystroke return
repeat until ((count windows) < c)
	delay 0.2
end repeat



tell application "iCal" to quit




Model: MacBookPro
AppleScript: Editor 2.4.3
Browser: Firefox 15.0
Operating System: Mac OS X (10.8)

Hi.

See if there is something you can use here. :slight_smile:

tell application "iCal" to activate


tell application "System Events"
	tell application process "iCal"
		keystroke "p" using command down
		--		delay 0.02
		tell button 4 of window 1 to click
		--		delay 0.02
		tell menu button 1 of window 1 to click
		key code 125
		key code 125
		keystroke return
		-- 		keystroke return
	end tell
end tell

How to script the Calendar print dialog text size?

I found the previous posts very helpful as I was trying to automate printing of my daily calendar.

One part that does not seem to work in the current Calendar dialog is the section on selecting the text size.

set textSizePopUp to first pop up button whose name is missing value

For me, that command results in an error:

Using the Accessibility Inspector, it appears that this pop up button (along with others) does not have a label or title to make addressing it easier. The values options are finite so tried this approach:

set textSizePopUp to first pop up button whose value is in {"Small", "Medium", "Big"}

But this also results in an error

I DID find success in a brute-force approach:

set textSizePopUp to first pop up button whose value is "Small" or value is "Medium" or value is "Big"

It seems like there should be more compact approach using is in and a list like {“Small”, “Medium”, “Big”} but I’m stymied by the type specifier error.

Anybody know how to solve this?

Model: MacBook Pro (13" Early 2015)
Browser: Safari 605.1.15
Operating System: macOS 10.14