I'm trying to improve a working macro which uses found image clicking and keystrokes to create a PDF of the current sheet and open it in Preview. To allow this, the Numbers print dialog has a handy menu of PDF-related action at the the bottom.
I can successfully click the right-hand portion (down arrow) to expose the dropdown menu...
...and select the "Open in Preview" option from there with "Click at Found Image" actions, however I'm wondering if there's another way to get that to happen.
The "Press Button" action presses the "PDF" portion of the button (I guess they are two separate buttons), bringing up a save dialog. For reasons I won't get into, I'd prefer to open it directly in Preview. Anyone have an idea?
Once you have that print dialog open, the following AppleScript should do the trick (I’m on Ventura, so if you’re on a different OS, there’s a possibility that menu item differences will cause it to fail).
AppleScript to open in preview (click to expand/collapse)
tell application "System Events"
tell application process "Numbers"
click menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1
repeat until exists menu item 1 of menu 1 of group 2 of splitter group 1 of sheet 1 of window 1
delay 0.1
end repeat
click menu item 1 of menu 1 of group 2 of splitter group 1 of sheet 1 of window 1
end tell
end tell
Technically, you can replace your entire macro with AppleScript...
AppleScript full replacement (click to expand/collapse)
tell application "System Events"
tell application process "Numbers"
# click print without preview menu item
click menu item "Print Without Preview…" of menu 1 of menu bar item "File" of menu bar 1
# wait for the PDF dropdown arrow to appear
repeat until exists menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1
delay 0.1
end repeat
# click the PDF dropdown arrow
click menu button 1 of group 2 of splitter group 1 of sheet 1 of window 1
# wait for the Open in Preview option to appear
repeat until exists menu item 1 of menu 1 of group 2 of splitter group 1 of sheet 1 of window 1
delay 0.1
end repeat
# click the Open in Preview option
click menu item 1 of menu 1 of group 2 of splitter group 1 of sheet 1 of window 1
end tell
end tell
If you have questions about either one of those scripts, or how to implement/use them, just let me know and I’d be happy to help further.
I don't even know what techniques you use to figure that out. I'm guessing the Script Editor, but can you tell us what your approach was? I'd like to learn.
This works beautifully! Many thanks! Like @Airy, I would also like to know how you figure that out. What tools are you using to get the paths to the items?
Using UI Browser and Accessibility Inspector, I've managed to sniff out this bit of AppleScript which worked once or twice and then didn't:
select text field 3 of sheet 1 of window "Hendrix - XXXXX Invoice.numbers-Invoice.pdf – 1 page"
My goal, if it's even possible is to figure out how to refer to this element (the Save dialog search field) without the unique reference of the window name. (The hideous file name is a result of the PDF export from Numbers and gets sorted out in the save process with some find and replace actions.)
This way gets the desired text into the search field, but the search get stuck and won't complete even with manual assistance:
set value of text field 3 of sheet 1 of window "Hendrix - XXXXX Invoice.numbers-Invoice.pdf – 1 page" to "<string>"
This doesn't appear to work either:
tell application "System Events"
tell its application process "Preview"
tell its window "Hendrix - XXXXX Invoice.numbers-Invoice.pdf – 1 page"
tell its sheet 1
tell its text field 3
tell its UI element 1
action "AXPress"
end tell
end tell
end tell
end tell
end tell
end tell
Nice! Had to add a 0.5s pause after the Return keystroke to get it working in the macro. Playing around with the Press Button action was very instructive. Thanks!
I'll be using your UI Browser Lite macro on a daily basis. Thanks for that, too.
You might try changing it to window 1 instead of window "weird convoluted file name".
Example:
set value of text field 3 of sheet 1 of window 1 to "<string>"
As to the initial questions about how I setup AppleScripts, the posts @Tom provided in his reply are great references. I use UI Browser a lot. It has been discontinued, unfortunately, and there is no longer a way for new users to get a license, but those who already had one can continue to use it.