MacMost Q&A Forum • View All Forum QuestionsAsk a Question

How Do I Make a Numbers Window Active Using AppleScript?

I have a Numbers document that I want to activate, make a selection, copy they activate a second window and paste it in, using AppleScript.
I can bring the document to the front and make the selection, but not copy the selection. This can only be done when the document is active, ie “clicked” on. How do I make it active or in other words, “clicked” on? it should be simple!
Here is the base code that does what I want up to copying the cells.
Any help would be greatly appreciated.

tell application “Numbers”
activate
set myWindow to name of document “Untitled”
set index of window myWindow to 1
tell table 1 of sheet 1 of document myWindow
set thisRange to “A2:D15”
set the selection range to range thisRange
end tell
–Get Modified probabilities and copy to clipboard
tell application “System Events” to tell process “Numbers”
tell menu bar item “Edit” of menu bar 1
click menu item “Copy” of menu 1
click menu item “Delete” of menu 1
end tell
delay 0.1
—–
Ed Weaver

Comments: 3 Responses to “How Do I Make a Numbers Window Active Using AppleScript?”

    6 years ago

    Your script works for me right away. I am running it in the Script Editor.

    What error are you getting?

    Ed Weaver
    6 years ago

    I'm not getting an error, it doesn't complete the script to do what I want.
    Scenario:
    I have 2 docs open. I do some work on doc 1 on with the script, bring doc 2 to the front, make selection, copy, go back to doc 1, paste. For me it does not make the copy or delete the selection unless the doc had been clicked on first.
    I omitted returning to doc 1 part of the script as the copy part didn't work, unless the doc was selected like you had clicked on it.

    6 years ago

    Ed: How about adding this just before the "tell many bar item Edit" line:

    click menu item "Untitled" of menu 1 of menu bar item "Window" of menu bar 1

    You may need to use "Untitled.numbers" if the title bar shows that.

Comments Closed.