I am having trouble creating a new table on a specific sheet using Applescript unless the sheet I want insert the new table in is either new or current selected.
The general form of the code I am using is:
tell application "Numbers"
tell document 1
tell sheet "This is the sheet I want to use"
make new table with properties {name:"A new table"}
end tell
end tell
end tell
Has anyone had any more success at achieving this? This looks to me like a bit of a problem for some advanced spreadsheet scripts in Numbers.
OK. My first answer was accepted and could not be deleted, but was inadequate, so I have decided to edit it and add some code which really does solve the problem!
This code comes via Yvan Koenig, who credits Nigel Garvey. Both are active on the applescript-users list, which is excellent, by the way, not least because of the activity of these two gentlemen, and so many other great AppleScripters.
It relies on GUI scripting (which is always a horrible thing to have to fall back on, but it’s what we’ve got).
The call to
my activateGUIscripting()may cause a prompt for an admin password to appear. If you absolutely know that you have GUI scripting enabled (System Prefs->Universal Access->Enable Access for Assistive Devices) – then you can omit this line.The next two lines are just example calls, so you need a sheet called “Sheet 1” for these example calls to work.
This will allow you to create a table in any sheet in any document, regardless of what is selected or frontmost.