The following example AppleScript code uses UI Scripting, however as tested it did not require Numbers to be frontmost. In other words it will work in the background, albeit I did see the Zoom menu pop up briefly but the window stayed in the background.
Example AppleScript code:
-- # Assumes Numbers spreadsheet is already opened.
my zoomToPercent("400%")
on zoomToPercent(p)
tell application "System Events"
tell group 2 of ¬
toolbar 1 of ¬
window 1 of ¬
application process "Numbers"
set percentButton to ¬
the first menu button ¬
whose name contains "%"
click percentButton
click menu item p of menu 1
end tell
end tell
end zoomToPercent
Notes:
Set it up as a handler so you can pass it a new value after the processing at the higher percent.
You can change window 1
to e.g. window "Untitled"
or whatever the name of the window is.
The handler also could be modified to take a second parameter, being the name of the window.
Note: The Zoom menu on the Toolbar and the value passed must be one that shows on the Zoom menu on the Toolbar.
UI Scripting can be kludgy and is prone to failure. It can easily break as the UI changes in different version of the product, or change the user can make, timing issues, etc.
The example AppleScript code, shown above, was tested in Script Editor with Numbers version 10.2 (7028.0.88) under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
- 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Modified handler to take the percentage and the name of window:
-- # Assumes Numbers spreadsheet is already opened.
my zoomToPercent("400%", "Untitled")
on zoomToPercent(p, n)
tell application "System Events"
tell group 2 of ¬
toolbar 1 of ¬
window n of ¬
application process "Numbers"
set percentButton to ¬
the first menu button ¬
whose name contains "%"
click percentButton
click menu item p of menu 1
end tell
end tell
end zoomToPercent
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.