Is there an AppleScript command to zoom a sheet in Numbers?

I think the answer to this question is "no", as I've not found any mention of it in the Numbers dictionary, but I thought I would ask here in case it's an undocumented feature.

I have a fairly large Table that I'm using AppleScript on. I'm running a script like this on it and have noticed it's very slow when the zoom level is low. When I set it to 400% it runs 100x faster.

repeat with i from 2 to the count of cells of column "E"
    set the value of cell i of column "E" to (the value of cell i of column "C")
end repeat

So is there a way to automated this in AppleScript so that before I enter that repeat loop I can set zoom to 400%?

I would like to add that I can't use System Events in this case as I need it to happen in the background.


Solution 1:

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.