Numbers v3.3 and AppleScript

Hello everybody

To help an user, I wrote a script which replace several strings in cells of a table and apply a color to the replaced strings.
To do that I was forced to use a temporary text file (TextEdit one).
As some cells are embedding linefeed characters, I replaced them by the characterID 8629 (“↵”) so that the script may paste the datas with colored strings in the table.
This works well.

I tried to use the Find - Replace dialog to replace back the (“↵”) by linefeed.
I discovered that the Find Replace dialog doesn’t accept the linefeed ( no more character ID 8232 or character ID 8233) in the replace field so the final replacement must be done by hand for each occurrence.
I’m bored because the feature is available in the good old Numbers v2.3.

As I had already wrote the skeleton of a handler using the Find - Replace dialog.

At first thought it seemed to be a simple task and I got :

my Rechercher_Remplacer("lapin", "crétin")

#=====

on Rechercher_Remplacer(avant, |après|)
	local nom_du_dialog, X_bouton, Y_bouton
	tell application "Numbers" to activate
	
	tell application "System Events" to tell application process "Numbers"
		keystroke "f" using {command down}
		set nom_du_dialog to title of window 1
		tell window nom_du_dialog
			set value of first text field to avant
			set value of second text field to |après|
			delay 0.2
			get value of first text field
			get value of second text field
			title of button -3 # To check that I corredctly trigger the correct button
			set {X_bouton, Y_bouton} to position of button -3
			click at button -3 # Don't execute the "Replace All" task
			
			--click at {X_bouton + 5, Y_bouton + 5} # Issue the error :  Il est impossible de rendre {916, 806} en type list." number -1700 from {916, 806} to list
		end tell
		
		--keystroke "w" using {command down} # Close the dialog
		
	end tell -- System Events.
	
	# Try to use the Unix command Clicclik (available at : [url=http://www.bluem.net/jump/cliclick/]www.bluem.net/jump/cliclick/[/url] )to click the button
	do shell script "/usr/bin/cliclick c:" & (X_bouton + 5) & "," & (Y_bouton + 5) # Don't execute the "Replace All" task
	tell application "Numbers" to display dialog "click "Continue" then click the Numbers's button "Replace All"" buttons {"Continue"} default button 1
	# Try to use the ASObjC Runner « click button » command to click the button
	--tell application "ASObjC Runner" to click button once at {X_bouton + 5, Y_bouton + 5} # Don't execute the "Replace All" task
	return
end Rechercher_Remplacer

#=====

(*
# Events Report. At first look there is no oddity

tell application "Numbers"
	activate
end tell
tell application "System Events"
	keystroke "f" using {command down}
	get title of window 1 of application process "Numbers"
		--> "Rechercher et remplacer"
	set value of text field 1 of window "Rechercher et remplacer" of application process "Numbers" to "lapin"
	set value of text field 2 of window "Rechercher et remplacer" of application process "Numbers" to "crétin"
	get value of text field 1 of window "Rechercher et remplacer" of application process "Numbers"
		--> "lapin"
	get value of text field 2 of window "Rechercher et remplacer" of application process "Numbers"
		--> "crétin"
	get title of button -3 of window "Rechercher et remplacer" of application process "Numbers"
		--> "Tout remplacer"
	get position of button -3 of window "Rechercher et remplacer" of application process "Numbers"
		--> {1329, 1035}
	click window "Rechercher et remplacer" of application process "Numbers" at button -3 of window "Rechercher et remplacer" of application process "Numbers"
		--> missing value
end tell
*)

Alas, click at button -3 doesn’t launch the Find Replace, it just highlight the first occurrence of the string to replace
Trying to trigger the button by its coordinates issue an error number -1700 as I already reported.
I tried to trigger the button with cliclick but got the same behaviour.
Same result with the feature click button available in ASObjC Runner.

I’m really puzzled. How may I launch the Find - Replace process ?
As you may see, the only workaround which I found is to issue a dialog urging the user to click the “Replace All” button by hand. Awful, isn’t it ?

Yvan KOENIG (VALLAURIS, France) vendredi 5 septembre 2014 17:18:40

If I understand what you’re trying to say, you’re asking how to press the “replace all” button in the find & replace pop up menu.

click button "Replace All" of window 1

This did the trick for me for the replace all button.
If you want to use the “Replace & Find” button, it would be similar to the above code except you would use “Replace & Find”.

Thanks.
I didn’t want to trigger a popup menu.
I want to click the [ Replace All ] button in the Find-Replace dialog which we get pressing command + f.
During my tests I tried also click button -3 which behaved exactly like click at button -3.
Your message gave me the idea to code :
set ReplaceAll to title of button -3 # To get the button’s name
click button ReplaceAll
and bingo, it worked.

So, the code required to apply Replace All with a given pair of strings before & after is :

my Rechercher_Remplacer("lapin", "la" & linefeed & "pin")

#=====

on Rechercher_Remplacer(avant, |après|)
	tell application "Numbers" to activate
	
	tell application "System Events" to tell application process "Numbers"
		keystroke "f" using {command down}
		title of window 1
		--> "Rechercher et remplacer"
		tell window result
			set value of first text field to avant
			set value of second text field to |après|
			title of button -3 # To get the button's name
			--> "Tout remplacer"
			click button result
		end tell
		keystroke "w" using {command down} # Close the dialog
	end tell -- System Events.
	return
end Rechercher_Remplacer

#=====

I really don’t understand why three days ago, when I inserted a linefeed in the string after I got in fact a space character.
Today, it does the job so I will be able to finish the script posted in your own thread.

Yvan KOENIG (VALLAURIS, France) samedi 6 septembre 2014 10:56:40

I’m more puzzled than ever.

It seems that trying to understand what puzzled be was a bad idea.

Today I tried to trigger the [Replace All ] button using three different syntaxes.

title of button 4 # To get the button's name
			--> "Tout remplacer"
			click button result
			--> button "Tout remplacer" of window "Rechercher et remplacer" of application process "Numbers"
click button 4
			--> button "Tout remplacer" of window "Rechercher et remplacer" of application process "Numbers"
click button -3
			--> button "Tout remplacer" of window "Rechercher et remplacer" of application process "Numbers"

The three ones behaved flawlessly although, at least the two late ones were failing to achieve their goal the days before.
I am running the same iMac, the same Mavericks 10.9.4 in French and the same Numbers v3.2.2.
I listened carefully and didn’t hear any kind of gremlins playing the fools in the machine.

Even the compact instruction :

click button (title of button 4)

does the job.

Maybe a track able to help to find an explanation.
As I’m curious, I tried without the parenthesis:

click button title of button 4

and it just selected the matching occurrences.
So I reinserted the parenthesis and this time, it failed again.
I quitted Numbers and AppleScript Editor then restarted both and bingo, this time,

click button (title of button 4)

did its duty.
At least, now I’m aware that the stripped instruction ” which compiles flawlessly ” install something wrong in AppleScript environment (caching ?) which prevent the correct instruction to behave well during attempts to come.

Must use a postIt stating : " before scrapping your head too much when something seems to be puzzling, quit the apps and AppleScript then restart them. "

Yvan KOENIG (VALLAURIS, France) dimanche 7 septembre 2014 16:22:27