Hello
If some of my words seem to be rude, take care to the fact that English is not my main language.
I did my best to don’t hurt but maybe my best is not sufficient.
Being new to Numbers or AppleScript is definitely not a sin.
But it’s not an excuse to try to guess the way those tools are behaving.
As every applications, they have their own language with their own rules.
For AppleScript, most of the rules are defined in AppleScript Language Guide available from :
https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf
For Numbers, they are defined in documents reachable thru the application’s Help menu.
For the duo Numbers + AppleScript, the rules are defined by the Numbers AppleScript dictionary which is available thru :
AppleScript Editor > File > Open a Dictionary > then navigate to Numbers;
I’m not sure that I really understand what you want to achieve.
(1) What is sure is that you made an awful mix of vanilla AppleScripting and GUiScripting.
In vanilla AppleScripting we don’t speak to the application process.
(2) reading the user guide is often useful.
I assume that when you wrote : set ay to “A” + rowNumber
you asked Applescript to attach the numerical row number just after the letter A to build a string like “A6”.
Alas, AppleScript can’t use the operator + to do that, it uses the operator & [or the function AND() but today it’s an other story]
Use : set ay to “A” & rowNumber
(3) I assumes that when you wrote : set columnA to the value of ay
you asked AppleScript to return the value of the variable ay.
Alas, AppleScript can’t do that this way.
You must ask it, politely :
set columnA to ay
or
copy ay to columnA
The two instructions aren’t doing exactly the same thing but this is an other other story.
(4) Honestly I I don’t understand what you want to achieve with repeat with incrementValue in {ay, be}
because you don’t use the variable incrementValue after defining it.
(5) I assumes that when you wrote : if value of columnA is equal to “” then
you asked AppleScript to check if the cell whose name is stored in the variable columnA contains the empty string.
Alas AppleScript doesn’t understand that.
You must ask it, always very politely,
if value of cell columnA is equal to “” then
If like me you are lazy, you may ask :
if value of cell columnA = “” then
which means exactly the same thing.
(6) I assumes that when you wrote : replaceText(setTrim(strikethroughText(value of columnA)), setTrim(strikethroughText(value of columnB))
you asked AppleScript to understand that you wanted it to execute the valid instruction :
replaceText(setTrim(strikethroughText(value of cell columnA)), setTrim(strikethroughText(value of cell columnB)))
Doing that, you would have render AppleScript happy during compile time.
Alas, at execution it would fail because you are passing two parameters to your handler replaceText which is built to receive three of them.
(7) I assumes that when you wrote : set rowNumber to rowNumber + 1
you wanted that AppleScript trigger the next row (remember, you started with row number = 1.
Alas, the instructions using rowNumber are outside of the repeat loop so you instruction does nothing useful.
(8) I assume that the handler replaceText is supposed to replace the styled text defined by the variable find by the styled text defined by the variable replace in the styled text defined by the variable someText.
Alas, as far as I know, it doesn’t treat styled text but raw text.
(9) For the handler strikethrough I am really puzzled.
(a) you ask it to work upon the variable thisWord which is not defined
(b) the variable columnB is meaningful in the main body of your script but it isn’t in the handler.
It’s a matter of scope and explaining what it means would be too long.
(c) If you use value of as you did in the case (3) the instruction must be :
if (character 1 of thisWord is in columnB) then
which would compile but is probably not what you want.
If, as I guess, you want it to check if the character exists in the cell whose name is defined by columnB,
it would be useful to ask AppleScript to speak - in the handler - to the document with something like :
tell application “Numbers” to tell front document to tell active sheet to tell table 1
After these long explanations, I made several changes in your script.
I didn’t wanted to build a script achieving your goal, just build a script able to compile.
The result is :
tell application "Numbers"
activate
tell front document to tell active sheet to tell table 1
set rowNumber to 1
set ay to "A" & rowNumber
set be to "B" & rowNumber
set columnA to ay
set columnB to be
repeat with incrementValue in {ay, be}
if value of cell columnA is equal to "" then
exit repeat
else
replaceText(setTrim(strikethroughText(value of cell columnA)), setTrim(strikethroughText(value of cell columnB)))
set rowNumber to rowNumber + 1
end if
end repeat
end tell
end tell
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
on trim(someText)
repeat until someText does not start with " "
set someText to text 2 thru -1 of someText
end repeat
repeat until someText does not end with " "
set someText to text 1 thru -2 of someText
end repeat
return someText
end trim
on strikethroughText(someSubstring)
tell application "Numbers" to tell front document to tell active sheet to tell table 1
if (character 1 of thisWord is in value of cell columnB) then
try
thisWord as text
-- set character style of word w to character style "Strikethrough"
end try
end if
end tell
end strikethroughText
As you may see, I was forced to disable one instruction :
– set character style of word w to character style “Strikethrough”
because, in the late version of Numbers (3.2.2), character style is not defined in the application’s dictionary.
Maybe you are trying to drive an older version (maybe 2.3) but, as I wrote often, I’m not a sooth sayer and can’t guess this kind of thing.
Have fun with Numbers and AppleScript.
Yvan KOENIG (VALLAURIS, France) vendredi 29 août 2014 18:24:14