Can Apple Script find/replace for conditions in formulas within cells?

I asked ChatGPT (who is writing the apple script for me) how to even phrase this question and here's what Chat suggested:


I'm using AppleScript to replace part of a COUNTIFS formula in a Numbers table. The original formula refers to columns in another table by name (e.g., Data Table::$Author).

When I manually enter or paste this formula in the Numbers UI, the app highlights each column reference as a color-coded token — confirming that it has resolved them to actual columns.

However, when I assign the exact same formula string using AppleScript (via set formula or set value), Numbers either:

  • throws error -10003, or
  • accepts the formula as text but fails to interpret the column references correctly.

This suggests that AppleScript-injected formulas are not being parsed through the same semantic resolution engine as user-entered formulas.

Is there a way to ensure that AppleScript-submitted formulas are parsed with full table/column binding, like user-entered ones?

iMac 27″ 5K, macOS 13.7

Posted on Mar 31, 2025 1:10 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 31, 2025 11:45 AM

anotherjas wrote:

All I want is an updated formula that looks for T rather than Pl in the Author column. Chat worried about deeper troubles.


Yes all the special characters can confuse machines (and humans).


If all you want to do is replace =PI with =T in a bunch of formulas then you can use this 'Find Replace in Formulas' Shortcut I made a while ago.


https://www.icloud.com/shortcuts/e52edb6b10084a93a7f030f03e2eff30


Click the link in Safari to view the AppleScript and, if you like, install the Shortcut. It can be run from the Shortcuts app or pinned to the Menu Bar and run from there.


To use:


  1. Select the cells with formulas that you want to change.
  2. Run the shortcut (it can be pinned to menu bar)
  3. Answer the first prompt with: =PI
  4. Click Next
  5. Answer the second prompt with: =T.
  6. Click OK.


This changes the values in the target cells, so make a backup before running to make sure it is giving you the results that you want.


SG



27 replies

Mar 31, 2025 11:21 AM in response to SGIII

Thank you, SG, for your interest.


Here's a screenshot of part of the data table:


and here's the right hand side of the table (with other columns of interest):


Sorry it's so small--it's a big data table. But it's interesting that Chat had to work out what do with my (naively) using characters like "$" and "@" in my data. All I want is an updated formula that looks for T rather than Pl in the Author column. Chat worried about deeper troubles.


JAS

Mar 31, 2025 11:50 AM in response to Camelot

Thank you both, SG and Camelot, for your interest. Here's Chat's attempt to fix just a single formula in one cell:


tell application "Numbers"

activate

tell document "Correlation scratch sheet.numbers"

tell sheet "All Septenarii Terence"

tell table "Table 3"

tell cell 3 of row 2

try

set formula to "COUNTIFS(Total 5@, \"=5\", clause 0, \"=B0-D12*\", Author, \"=T\")"

log "✅ Formula successfully assigned"

on error errMsg number errNum

log "❌ Error setting formula: " & errMsg & " (Error " & errNum & ")"

end try

end tell

end tell

end tell

end tell

end tell



and here is the error message that was logged in the script editor:


tell application "Numbers"


activate


set formula of cell 3 of row 2 of table "Table 3" of sheet "All Septenarii Terence" of document "Correlation scratch sheet.numbers" to "COUNTIFS(Total 5@, \"=5\", clause 0, \"=B0-D12*\", Author, \"=T\")"


--> error number -10003 from formula of cell to any


(*❌ Error setting formula: Numbers got an error: Can’t set formula of cell to "COUNTIFS(Total 5@, \"=5\", clause 0, \"=B0-D12*\", Author, \"=T\")". (Error -10006)*)


end tell

Mar 31, 2025 12:10 PM in response to SGIII

So I tried to run your script and am stuck at the same syntax error message for the one cell it attempted to replace:

The good syntax looks like this:

COUNTIFS(Data Table::$Total 5@,"=5",Data Table::$clause 0,"=B0-D12*",Data Table::$Author,"=Pl")

and appears like this:


the substitution that gets the syntax error message looks like this:


COUNTIFS(Data Table::$Total 5@,"=5",Data Table::$clause 0,"=B0-D12*",Data Table::$Author,"=T")

Mar 31, 2025 12:39 PM in response to anotherjas

So, running SG's shortcut, here's the cell before I run the shortcut:



And here's the cell after I run the shortcut:



and the cell contains this:



Isn't there some magical command to get Numbers to re-evaluate the table?


and, thank you, everybody, for sticking with me on this. It's an essential problem I'd love to master, but at some point, when you've all become exasperated, I'll just manually enter T in 400 cells.


JAS

Mar 31, 2025 12:49 PM in response to anotherjas

Interesting - Numbers does recognize the cell contents as a formula - both from the error message you get, plus the fact that it shows the formula editor... if the contents was just recognized as text then it would appear in-cell.


Very strange...


It looks valid. The script seems to work as expected if I run on my machine (with dummy data). Yet somehow it throws an error on your sheet.... curious...

Mar 31, 2025 3:42 PM in response to anotherjas

My guess would be the Header Names setting since that changes how Numbers interprets cell references. However, since the original script extracted the existing formula/cell references, they should have been in the form that Numbers prefers, so setting it back shouldn't have been a problem.

Also, I have that setting enabled, and the script worked for me...


So, I'm stuck between "happy you found a solution" and "wanting to know more" :)

Mar 31, 2025 5:25 PM in response to anotherjas

anotherjas wrote:

SUCCESS!!

Back to research on the metrics of ancient poetry!


Glad to hear the scripting works, though why it didn't work before and works now may have to remain a mystery.


I was wondering about your project. Curious when I learned it had to do with ancient poetry, I decided to ask AI what the Pl and T likely stand for, telling it I thought the poetry might be Greek. It told me that in classical studies abbreviations for authors are often standardized from the Latinized names. Its guess: Pl stands for Plato, who wrote poetry in addition to his philosophizing, and T stands for Tyrtaeus. How did it do?


SG

Mar 31, 2025 11:10 PM in response to SGIII

I'm studying Roman play scripts of two comic playwrights, Plautus (Pl) and Terence (T). And my work analyzes the interplay of measures of rhythm and segments of syntax. My syntax annotation shows clauses starting from a ^ symbol and ending with a $ symbol. I also use the @ symbol to mark accent beats. And when I first made these choices in my text mark-up, I didn't think through the trouble I might be causing myself by using regular expression characters in ordinary text strings. But little did I know when I was making these choices that one day I'd have AI to backslash all my characters so that $ is just a $ in formula syntax.


This is not to say that Tyrtaeus (Tyrt.) isn't fully worthy of scholarly attention for his Spartan marching beat. Just not my attention.


Thanks again.


JAS

Apr 1, 2025 4:32 AM in response to anotherjas

Ah, the machine tells me Roman comic poets. I threw it off by telling it I guessed they were Greek.


The need to "escape" special characters (typically ") with \ is an AppleScript thing, not related to regular expressions, which AppleScript does not handle natively as does, say, Javascript for Automation (JXA).


Getting the right escaping can be tedious.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can Apple Script find/replace for conditions in formulas within cells?

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