I’m trying to build a list of every paper size available to any given printer, set from a printer list of existing printers.
I’m using the print dialog box from Text Edit, with the code below.
It’s very slow, so I’m wondering if there’s any way of obtaining the list without opening every paper menu?
Any help appreciated.
on setUpPaperSize(selectedPrinter)
tell application "TextEdit"
activate
tell application "System Events" to tell process "TextEdit"
if not (exists window 1) then click menu item "New" of menu 1 of menu bar item "File" of menu bar 1
set x to 0
repeat until exists window 1
set x to x + 1
if x = 20 then exit repeat
tell current application to delay 0.1
end repeat
select window 1
tell current application to delay 0.1
try
keystroke "P" using command down
end try
set x to 0
repeat until exists sheet 1 of window 1
set x to x + 1
if x = 20 then exit repeat
tell current application to delay 0.1
end repeat
try
click pop up button 2 of sheet 1 of window 1
repeat until exists menu 1 of pop up button 2 of sheet 1 of window 1
tell current application to delay 0.1
end repeat
try
click menu item (selectedPrinter as text) of menu 1 of pop up button 2 of sheet 1 of window 1
on error errmsg
click menu item 1 of menu 1 of pop up button 2 of sheet 1 of window 1
end try
click pop up button 3 of sheet 1 of window 1
repeat until exists menu 1 of pop up button 3 of sheet 1 of window 1
tell current application to delay 0.1
end repeat
set mainPaperList to ""
set menucount to (count of menu items of menu 1 of pop up button 3 of sheet 1 of window 1) as list
repeat with x from 1 to menucount
click menu item x of menu items of menu 1 of pop up button 3 of sheet 1 of window 1
if exists menu 1 of menu item x of menu items of menu 1 of pop up button 3 of sheet 1 of window 1 then
set tempMenu to (every menu item of menu 1 of menu item x of menu 1 of pop up button 3 of sheet 1 of window 1) as list
repeat with y from 1 to count of tempMenu
set mainPaperList to mainPaperList & item y of tempMenu & return as text
end repeat
else
set mainPaperList to mainPaperList & menu item x of menu items of menu 1 of pop up button 3 of sheet 1 of window 1 & return as text
end if
end repeat
repeat while exists menu item 1 of menu 1 of pop up button 3 of sheet 1 of window 1
tell current application to delay 0.1
try
click menu item 1 of menu 1 of pop up button 3 of sheet 1 of window 1
try
click menu item 1 of menu 1 of menu item 1 of menu 1 of pop up button 3 of sheet 1 of window 1
end try
end try
end repeat
on error
set mainPaperList to {}
end try
click button "Cancel" of sheet 1 of window 1
end tell
quit
set paperSizeList to "No Paper Selected" & return as text
repeat with x from 1 to count of mainPaperList
set eachSize to item x of mainPaperList as text
if eachSize as text is not "missing value" and "Custom" as text is not in eachSize as text then set paperSizeList to paperSizeList & eachSize & return as text
end repeat
end tell
return paperSizeList
end setUpPaperSize