2

This question is related to:

where I'm trying to figure out a way to do what I want using AppleScript.

I've been searching the Web to find a way to set the active sheet in Numbers before I attempt to make a modification there. The preliminary steps are essentially:

tell application "Numbers"
    activate
    try
        set the chosenDocumentFile to "/path/to/myfile.numbers"
        open the chosenDocumentFile
    on error errorMessage number errorNumber
        if errorNumber is not -128 then
            display alert errorNumber message errorMessage
        end if
    end try
    tell chosenDocumentFile
        try
            -- this is where I'm stuck
        end try
    end tell
end tell

Most of the stuff I've found online talks about creating or deleting sheets, but I haven't found anything that provides a clear example of how to select a sheet.

In my case I currently have 5 sheets in my spreadsheet. I know I want to select the second sheet to act on (sheet 2 - right?)

I've tried:

open the sheet 2

open sheet 2

select sheet 2

activate sheet 2

None of them seem to work, and all of them prevent the next line of code within that enclosing try statement from being executed.

I'm hoping there's some really simple answer for this (I still have lots to do before I get the code to do everything I want it to, but I'm stuck here right now).

1

As far as I know, Numbers does not have an explicit AppleScript command to select a given sheet.

There is the active sheet property of the document, e.g.,

tell application "Numbers" to get active sheet of front document

which will return the active sheet; however, while one might think that e.g.,

tell application "Numbers" to set active sheet of front document to sheet 2

assuming there is a sheet 2, would set e.g., sheet 2 to the active sheet, it does not set it as the active sheet.

Here is the method I use the makes a given sheet the active sheet in the front document:

Note: This is a hack method and it does cause a saved document to show as edited, even though it's not altering the value of the target cell. It works by setting the value of cell "A1" to it's current value, so while it's perceived as an edit, no data has been altered from its current value. However, it does work for me in Numbers 5.1 in macOS High Sierra, and is why I use it.

my selectSheet(2)

on selectSheet(n)
    try
        tell application "Numbers"
            activate
            set value of first cell of first table of sheet n of front document to ¬
                (value of first cell of first table of sheet n of front document)
        end tell
        --  # Press escape key to remove focus from cell A1.
        tell application "System Events" to key code 53
    on error eStr number eNum
        display dialog eStr & " number " & eNum buttons {"OK"} ¬
            default button 1 with icon caution
        return
    end try
end selectSheet

Note: The example AppleScript code is just that and, sans any included error handling, it does not contain any other 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.

  • That doesn't seem to work for me in Automator - I get the following error:: "Automator is not allowed to send keystrokes. Number 1002" – ghoti Jul 11 at 0:47
  • Correction: It works as far as getting the sheet to become visible / active, but it doesn't remove the focus on the cell - if I comment out that portion of the code it works. I'm having issues with setting a value in the specific cell I want to, but I might have to either update this post or make a subsequent one to deal with that... – ghoti Jul 11 at 0:53
  • @ghoti, You never mentioned in the OP that you are running AppleScript code from Automator! It works for me, without issue, when run in an Automator Workflow; however, I have Automator added to System Preferences > Security & Privacy > Privacy Accessibility in macOS High Sierra. Are you running it from an Automator Workflow, Application or Service (Quick Action in macOS Mojave) and in what OS? As to other AppleScript code, you really should ask that in a different question and include both the code and a screenshot of the Automator workflow. – user3439894 Jul 11 at 4:37
  • My bad - I usually include environment details when posting questions: MacOS X Mojave (10.14.5), Numbers 6.1, Automator 2.9. I'm thinking of making this code into a Service specifically for use in this particular spreadsheet. The System Preferences change seems to work. Thanks! – ghoti Jul 11 at 15:08
  • After various Q&A's and design morphing, this ended up as: AppleScript Numbers: Process data from Quicken – ghoti Jul 15 at 20:47

You must log in to answer this question.

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