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
27 replies
Sort By: 

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

To set a formula, use:


set value to "=theFormula"


You need the =.


In your most recent short example you haven't specified a table name and those columns aren't in the same table as the formula so Numbers doesn't where to look.


Have you tried the shortcut I posted?


SG

Reply

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

Yeah, I did note that it shouldn't be an issue for the script where you extract the current formula text, modify it, and write it back, since that should include the leading =.

Just for grins, does it make a difference if you include the '='? I'm guessing not, but sometimes troubleshooting like this takes iteration.


The error code itself is odd - -10006 is usually a 'permission denied' error message (or sometimes 'write permission denied'. Is there anything unusual about this table? it's not locked?


Now that I think about it, all the references, quotes, =, etc. are all moot - if the formula isn't valid, Numbers should still enter it and just give a Syntax Error warning. Therefore I think the problem does lie more with the sheet, or the table and not with the function/formula itself.



Reply

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...

Reply

Mar 31, 2025 2:41 PM in response to SGIII

SUCCESS!!

Unfortunately, I can't tell you which of your two suggestions addressed the problem -- I both restarted my computer and deselected "Use header names as labels" (which was indeed selected). Then I ran your shortcut once more, and, boom!, it worked.


Thanks to you, SG, and to Camelot and Badunit, for your persistence. I may have burned several hours trying to figure out automation, but from this point forward, I'll know what to do, and it proved so worth the time spent.


Finally, I'm still not sure whether Chat's verbose AppleScript would have worked if I had Number's settings correctly toggled, but I suspect it would have. I'll share the solution with him and keep the threaded conversation in my GPT account so that he'll remember (at least in my case) what works.


And yet if Chat and I had solved it on our own, I wouldn't have gotten to see real-life geniuses at work, and wouldn't feel so validated by such a frightfully talented community. Just wish the solution had proved to be something elegant and not so embarrassingly entry-level.


Back to research on the metrics of ancient poetry!

Thanks again,

JAS

Reply

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" :)

Reply

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

Reply

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

Reply

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

Reply

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.