Daryl Spitzer Daryl Spitzer - 4 months ago 13
AppleScript Question

How do I set the number of decimals in a Numbers cell using AppleScript?

I can set the format of a cell in a Numbers spreadsheet using:

tell application "Numbers"
activate
tell document 1
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
tell selectedTable
tell column "J"
set the format of cell 3 to number
end tell
end tell
end tell
end tell


But how do I set the number of decimal places of the cell?

Answer

Unfortunately the AppleScript dictionary of Numbers does not support to set the decimal places, however you can do it with GUI scripting.

Since GUI scripting strongly depends on the software version, this is for Numbers 3.6.2

tell application "Numbers"
    activate
    tell document 1
        tell active sheet
            set the selectedTable to ¬
                (the first table whose class of selection range is range)
        end tell
        tell selectedTable
            tell column "J"
                set the format of cell 3 to number
            end tell
        end tell
    end tell
end tell

tell application "System Events"
    tell process "Numbers"
        tell radio group 1 of window 1
            if value of radio button "Cell" is 0 then
                click radio button "Cell"
                repeat until value of radio button "Cell" is 1
                    delay 0.2
                end repeat
            end if
        end tell
        tell text field 1 of scroll area 4 of window 1
            set value of attribute "AXFocused" to true
            set value to "2"
            keystroke return
        end tell
    end tell
end tell

In a system language other than English the literal string Cell might be localized.