Apple Event: May 7th at 7 am PT

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

AppleScript: Add Commas to Numbers Without String Conversion

Can AppleScript format number values in an Apple Numbers sheet without conversion to a string?


I am using AppleScript to import a financial report into Apple Numbers, with a lot of reformatting. I can set the data format of my numeric data to "Number" (as I would in the Cell tab of the Format Inspector), but I can't:


  • add a 'thousands separator', the commas used in the US to delimit thousands in a number; or
  • set the number of decimal places to a fixed number (2 is conventional).


So, I get a number like 1234.5, instead of 1,234.50, which is what I need.


I know I can set these formats manually after the script runs, but it's going to be easier if I can script the process. And I know I can convert the values to strings and reformat that way, but I need numeric values, not strings.


So, here's my question: Does AppleScript offer any way to perform this formatting directly, or will I have to script the UI to do it? Any code snippets would be really helpful. Thanks!

iMac, 27" Late 2013

Posted on Feb 8, 2018 8:58 AM

Reply
Question marked as Best reply

Posted on Feb 9, 2018 10:28 AM

Hi Laguna Hiker,


The following script should convert the content of cell B2 from 1234.5 to 1,234.50 without using GUI scripting:


tell application "Numbers"

tell table 1 of sheet 1 of document 1

set theValue to value of cell "B2"

set format of cell "B2" to automatic

set value of cell "B2" to "0,000.00" -- the desired format

set format of cell "B2" to number

set value of cell "B2" to theValue

end tell

end tell

7 replies
Question marked as Best reply

Feb 9, 2018 10:28 AM in response to Laguna Hiker

Hi Laguna Hiker,


The following script should convert the content of cell B2 from 1234.5 to 1,234.50 without using GUI scripting:


tell application "Numbers"

tell table 1 of sheet 1 of document 1

set theValue to value of cell "B2"

set format of cell "B2" to automatic

set value of cell "B2" to "0,000.00" -- the desired format

set format of cell "B2" to number

set value of cell "B2" to theValue

end tell

end tell

Feb 8, 2018 11:09 AM in response to Laguna Hiker

It appears that I have to use UI scripting to get the number formats I need. Here's the code I used to set commas, format negative numbers, and set the number of decimals:



-- Add number formatting


activateapplication "Numbers"

tell application "System Events"

tell process "Numbers"


-- Tick the 'Thousands Separator' checkbox

tell checkbox " Thousands Separator" of scroll area 4 of window 1

if value is not 1 then click

end tell


-- Set the number of decimals to 2

tell text field 1 of scroll area 4 of window 1

keystroke "2"

end tell


-- Format negative numbers with parentheses

tell pop up button 2 of scroll area 4 of window 1

click

delay 0.5

pick menu item "(100)" of menu 1

end tell


end tell

end tell

I found an app, UI Browser (http://pfiddlesoft.com/uibrowser/) that gets the object reference to the Numbers UI control that I need, which saves figuring out the Numbers UI object hierarchy. I pass it along as a potentially useful tool for UI scripting--I have no affiliation with the developer and bought my copy.

Feb 9, 2018 6:58 AM in response to Laguna Hiker

One more note: The UI scripting code shown above assumes that the 'Cell' tab of the Numbers Format Inspector is active. Here's the code to perform that task (note that the tab is actually a radio button control):



-- Activate the 'Cell' tab of the Format Inspector

tell radio button "Cell" of radio group 1 of window 1

if value is not 1 then click

end tell

Feb 9, 2018 1:10 PM in response to Pierre L.

Thanks, Pierre!


Here is a slight embellishment to include formatting of negative numbers:


tell application "Numbers"

tell table 1 of sheet 1 of document 1


-- Get the cell value

set theValue to value of cell "B2"


-- Set the data format

set format of cell "B2" to automatic

if theValue is greater than 0 then

set value of cell "B2" to "0,000.00" -- the desired format

else

set value of cell "B2" to "(0,000.00)" -- the desired format

end if

set format of cell "B2" to number


-- Set the data value

set value of cell "B2" to theValue


end tell

end tell

Feb 9, 2018 1:14 PM in response to Laguna Hiker

Pierre L's response is the best one, and I have marked it as solving my problem. The GUI scripting approach works, but GUI scripting should only be used as a last resort. The forum won't let me delete my GUI scripting answer--hopefully, it will prove helpful to those learning GUI scripting with Excel.


But again--use Pierre's approach. It's the best solution to the problem.

Feb 9, 2018 3:00 PM in response to Laguna Hiker

I'm glad I could help you. Many thanks for your feedback.


By the way, the IF block doesn't seem necessary to the embellishment. Try this:


tellapplication "Numbers"

tell table 1 of sheet 1 of document 1

set theValuetovalueofcell "B2"

set formatofcell "B2" to automatic

set valueofcell "B2" to "(0,000.00)" -- the desired format

set formatofcell "B2" to number

set valueofcell "B2" to theValue

end tell

end tellB

AppleScript: Add Commas to Numbers Without String Conversion

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