The dialog is not the same under the two OS but it’s also not the same for Script Editor and TextEdit.
my setLanguage("TextEdit", "Svenska")
my setLanguage("Script Editor", "Svenska")
--=====
on setLanguage(curApp, myLanguage)
tell application curApp to activate
set flag to 0
if curApp is not "Script Editor" then
tell application "System Events" to tell process curApp
set knt to count of windows
keystroke ":" using command down
delay 0.2
set knt2 to count of windows
if knt2 > knt then
set nw1 to name of window 1
if my quelos() < 5 then
try
tell window nw1
set flag to 1
if (item 1 of (get value of pop up button of group 1)) as text is not myLanguage then
click pop up button 1 of group 1
click menu item myLanguage of menu 1 of pop up button of group 1
else
set flag to 2
end if
end tell
click button 1 of window nw1 (* close the window *)
on error
set flag to 3
end try
else (*
Here macOS X 10.5 or higher *)
try
tell window nw1
set flag to 1
if (get value of pop up button 1) is not myLanguage then
click pop up button 1
click menu item myLanguage of menu 1 of pop up button 1
else
set flag to 2
end if -- state
end tell -- window
click button 1 of window nw1 (* close the window *)
on error
set flag to 3
end try
end if -- quelOS
else if knt2 = knt then
set flag to 4
end if -- count of windows
end tell -- process
else (*
special case: "Script Editor" *)
tell application "System Events" to tell process curApp
set knt to count of windows
keystroke ":" using command down
delay 0.2
set knt2 to count of windows
if knt2 > knt then
set nw1 to name of window 1
if my quelos() < 5 then
try
tell window nw1
set flag to 1
if (item 1 of (get value of pop up button of group 1)) as text is not myLanguage then
keystroke myLanguage & return
click pop up button 1 of group 1
else
set flag to 2
end if -- item 1.
end tell -- window
click button 1 of window nw1 (* close the window *)
on error
set flag to 3
end try
else (*
Here macOS X 10.5 or higher *)
try
tell window nw1
set flag to 1
if get value of pop up button 1 is not myLanguage then
keystroke myLanguage & return
click pop up button 1
else
set flag to 2
end if -- item 1.
end tell -- window
click button 1 of window nw1 (* close the window *)
on error
set flag to 3
end try
end if -- quelOS
else if knt2 = knt then
set flag to 4
end if -- count of windows
end tell -- process
end if -- curApp
if flag = 0 then error "The Spell Checker window was open, you closed it !"
if flag = 2 then error myLanguage & " was already set"
if flag = 3 then error "The opened window can't be accessed !"
if flag = 4 then error "Spell Checker window is unavailable !"
end setLanguage
--=====
on quelos()
return (system attribute "sysv") mod 4096 div 16
end quelos
--=====
Do what you want with the four displayed messages
As you see, using GUI scripting is not really simple.
I repeat my thanks to both chris2 and YK for your great efforts. However, I am still having problems.
Answering chris2: the script in post #3 works well when executed within Script Editor (“Run” button), but does not work in these cases:
if I change “name of current application” to ““TextEdit””.
if i run it from the Script menu in the top menubar with TextEdit as current application.
YK: your latest script also works fine when executed within Script Editor, but when I run it from the Script menu in the top menubar, I get these results:
If I change the script to call setLanguage only for TextEdit, all OK.
If I change the script to call setLanguage only for Script Editor, I have the following after running the script:
the popup list of languages remains open, but nothing else of the Spelling window. In this list, the “old” language is still checked. In the Script Editor window, the first word after the previous cursor position which fails the spell check is replaced by “Svenska”.
I have also tried running the script from Butler, as recommended by chris2. I don’t have much success with that. The only debugging method I know is to put a “beep” in various places. A beep at the top of the script is executed. With YK’s script, a beep at the top of the body of setLanguage is not executed. Any tips?
Again: someone, please, point me to a good manual for GUI scripting!
I forgot to mention: with YK’s script (post #21), if I change it to call setLanguage with p1 = “name of current aplication”, then run the script from the Script menu in the top menubar with TextEdit as current application, the following happens: the Spelling window remains open, the language list is open with the “old” language checked.
Maybe because, as YK said: The dialog is not the same under the two OS but it’s also not the same for Script Editor and TextEdit.
Forget about my script, work on YK’s last script. He has a better insight.
I dont know. Someone else will suggest you that. But before reading any documenation, please have a look at a wonderful write-up by Kai. http://macscripter.net/viewtopic.php?id=16145
A good manual is not going to solve your problems like you expect them to.
I have been looking at several articles on GUI scripting over the last few days.
I believe trial and error is must in GUI scripting unless Yuan Koenig can explain me which documentation he used to figure out that he must use :
keystroke myLanguage & return
click pop up button 1 of group 1
and not
click pop up button 1
click menu item myLanguage of menu 1 of pop up button 1
As far as I know, if you run the script from the Script menu with p1 = name of current application, p1 will contain the name of the script so the described behavior is perfectly normal.
try to save as a bundle application the simple script:
and, tracing the script behavior with some log instructions I was able to see that this couple of instructions failed.
I also discovered that when a language was set when the application was Script Editor, it was not set when I opened the dialog by hnd from Text Edit.
So I understood that the two dialogs wheren’t using the same code.
As you posted the
couple of instructions which was odd during my tests with different applications, I assumed that you tested it before posting it so I thought: maybe he used it with “Script Editor”.
Last thing, may you take care that since 1943/12/31, my first name is Yvan not Yuan. I like this old friend. So please don’t hurt it
Reply to Yvan post #26:
I now understand that “name of current application” is not the way to refer to the name of the application which has focus, and whose name is displayed at the top left in the top menubar. That was a surprise and a disappointment. (Oh, how I wish I had some reference manual to learn these things, instead of having to try, and try, and retry, and ask others for help. This seems to be the new software engineering culture - noone has the time to write a manual; if you’re lucky, you’ll find an FAQ, or a forum. There was a time when one said (in reference to documenting software products): “the job isn’t finished until the paperwork is done”. The illustration was a small boy sitting on the toilet wiping his buttocks.)
My ultimate goal is to have a script, activated by a keystroke (some combination of keys) from “within” any application capable of text input. By “within” I mean that the application has focus and I am typing text into one of its windows. The purpose of the script is to change the language used by the spell checker. (I want to alternate between three languages; I may use three different keystrokes, or a simple selection dialog within the script, but this is outside the scope of this discussion).
I assumed that “name of current application” would be the proper way to identify the current application. If not, then how do I do it? And how would you propose to implement the full solution?
Very much indebted to Yvan and chris2, and anyone else who might want to help.
Oh wow, that’s a lot of work you have been doing Yvan - thanks a lot!
I will need some time to digest and learn about thisService. Will report back when I have understood and tried it.
Peter