1

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.

6
  • Thanks for the answers but I should have clarified that I need this to happen in the background. I think all these options require the activation of Numbers which doesn't work for my situation. Commented Aug 22, 2021 at 16:28
  • See superuser.com/questions/487030/… for background scripting.
    – Tetsujin
    Commented Aug 22, 2021 at 16:39
  • Thanks @Tetsujin, I will try it that way. Commented Aug 22, 2021 at 17:16
  • OK I will try it. Can you post your answer again please? Sometimes we can't know all the necessary parts of the question until we know what the possible solutions will be. Commented Aug 22, 2021 at 17:39
  • BTW @user3439894 I'm surprised it works so well. I had mistakingly thought that anytime you used system events that the application had to be activated first but it does it correctly without even showing the app or needing to be in the foreground. This will be very helpful in accessing other features in Numbers that don't have native AppleScript support. Where can I learn more about the syntax for System Events? Commented Aug 22, 2021 at 18:03

1 Answer 1

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.

8
  • I just got this error. It was working for a while but then this: Can’t get window 1 of «class pcap» "Numbers" of application "System Events". Invalid index. (-1719) Commented Aug 22, 2021 at 18:35
  • @Steve Torrence, What version of macOS are you running and version of Numbers? Commented Aug 22, 2021 at 18:39
  • Big Sur 11.5 (latest) Numbers 11.1 (7031.0.102) It seems to happen if I'm using spaces (which I always do) and I'm currently on another space. Maybe there is "on error" I can add to it so it doesn't interrupt the execution of the rest of the script? Because it's not necessary it always works but when it does, it speeds up that part of the script 100%. Commented Aug 22, 2021 at 18:44
  • I put around it: Try … End Try Maybe that will work. Commented Aug 22, 2021 at 18:55
  • Yes, macOS Big Sur has caused many a script that worked well in previous versions of macOS. One workaround when manipulating thing in System Preferences > using UI Scripting the to add a launch command as the line of code in the tell application "System Events" block, I do not think that applicable here. It's most likely because it was in a different Space. As far as error handling goes, that is why I put what I do at the end of my AppleScript answers. Commented Aug 22, 2021 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .