Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to select columns in numbers using applescript

Hello,


I am looking for an applescript to do the following operations in numbers:


- select a given column

- add a column after

- select formatting choices of data and number without going through the clicking of menus


thanks

MacBook Pro (13-inch, Late 2016, 4 TBT3), macOS High Sierra (10.13.6)

Posted on Aug 24, 2018 7:52 PM

Reply
Question marked as Best reply

Posted on Aug 25, 2018 3:41 PM

Here's a short script that illustrates all the operations you describe:


To see how the operations work experiment on a Table 1 in Sheet 1 that has a least 8 columns and 7 rows with some values in column 2.


set {sh, ta, co} to {"Sheet 1", "Table 1", "B"}

tell application "Numbers"

tell document 1

tell sheet sh's table ta


# select a column

set the selection range to its column co



# copy values from selected range


activate

tell application "System Events" to keystroke "c" using {command down}



# paste values into a new range

set the selection range to column "C"'s first cell


activate

tell application "System Events" to keystroke "v" using {command down}



# format a range

set column "A"'s format to checkbox-- or number, date and time, currency, etc



# add a column


add column aftercolumn 2



# delete rows

delete (rows 6 thru 7)



# delete columns

delete (columns 7 thru 8)

end tell

end tell

end tell



SG

10 replies
Question marked as Best reply

Aug 25, 2018 3:41 PM in response to Ricabude

Here's a short script that illustrates all the operations you describe:


To see how the operations work experiment on a Table 1 in Sheet 1 that has a least 8 columns and 7 rows with some values in column 2.


set {sh, ta, co} to {"Sheet 1", "Table 1", "B"}

tell application "Numbers"

tell document 1

tell sheet sh's table ta


# select a column

set the selection range to its column co



# copy values from selected range


activate

tell application "System Events" to keystroke "c" using {command down}



# paste values into a new range

set the selection range to column "C"'s first cell


activate

tell application "System Events" to keystroke "v" using {command down}



# format a range

set column "A"'s format to checkbox-- or number, date and time, currency, etc



# add a column


add column aftercolumn 2



# delete rows

delete (rows 6 thru 7)



# delete columns

delete (columns 7 thru 8)

end tell

end tell

end tell



SG

Aug 25, 2018 2:43 PM in response to Ricabude

There is a website that is linked from an apple site for scripting numbers (iWork in general), the answer is on there under column header on the left

https://iworkautomation.com/numbers/column-add.html

and for formatting the cells

https://iworkautomation.com/numbers/cell-styling.html


and finally a duckduckgo search for applescipt numbers (using duckduckgo ensures you see the same results I did)

https://duckduckgo.com/?q=AppleScript+numbers&t=hj&ia=web


Jason

Aug 25, 2018 3:20 PM in response to Ricabude

same website has how to remove rows and columns:

https://iworkautomation.com/numbers/row-remove.html


something like this will work:

set thisRangeName to "D:F"
removecolumnthisRangeName

setthisRangeNameto "D:F"=

I write automation in Excel vba for a living and usually we don't copy/paste, but write the value(s) to a variable and then set the new cells to that value. this gives us the ability to modify or change the values before writing them back out.


Sometimes we use the below type thing, setting the value of one cell to the value of another directly

set value of cell 2 of row 2 to value of cell 1 of row 1


Jason

Aug 25, 2018 4:27 PM in response to SGIII

Wonderful SG, thank you so much!!! 🙂


Only one point remain:


- when using setcolumn "A"'s formatto number, you cannot set the number of decimals, or the thousand separator. Is there a way to do that?


- when using setcolumn "A"'s formatto date and time, the time is there, and I'd like to leave only the date, in the format I'd choose, like - dd/mm/yy.


thanks again for your great help

Aug 25, 2018 6:13 PM in response to Ricabude

I haven't found a way to have AppleScript control the decimals or separator when setting data format to 'number' or to tell Numbers to hide the time part of a date-time string.


One could perhaps gui-script the Data Format section of Format > Cell in the panel at the right, but it's probably easier just to make adjustments there manually.


SG

How to select columns in numbers using applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.