Hi pwann,
Yuan Koenig 's last two scripts dont work for me either. Perhaps, because of the fact that he uses a non-english language in his system.
To run an applescript with a keystroke, I recommend Butler. Its slightly complicated to use but its more stable than Quicksilver. As I had asked earlier in this thread, how to assign a shortcut to a script which has to be run alone from Scripts menu alone, the only answer that I have as of now to that question is to write another script using UI method and assign a shortcut to it rather than assigning a shortcut to the original script.
It appears that the window in not built the same way in os X 1.4 ans in OS X 1.5
In 1.4 the popup is in a group
In 1.5 it isn’t.
Here is a version which take care of this difference.
set curApp to "TextEdit" --to name of current application
tell application curApp to activate
set myLanguage to "Svenska"
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
if (count of windows) > knt then
set nw1 to name of window 1
try
tell window nw1
if my quelOS() < "1500" then
if item 1 of (get value of pop up button of group 1) 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
end if
else
if item 1 of (get value of pop up button) is not myLanguage then
click pop up button 1
click menu item myLanguage of menu 1 of pop up button
end if
end if -- quelOS
end tell
end try
click button 1 of window nw1 (* close the window *)
end if
end tell -- application
end if
--=====
on quelOS()
local hexData, hexString
set the hexData to system attribute "sysv"
set hexString to {}
repeat 4 times
set hexString to ((hexData mod 16) as string) & hexString
set hexData to hexData div 16
end repeat
return hexString as string
end quelOS
--=====
YK 090207-032102: I get the same result as chris2: no error, but language does not change.
chris2: your script 090205-045316 worked fine, but only when run in Script Editor. If I change the first line to “set curApp to “TextEdit””, it no longer works (“Dansk” entered in text area, popup menu remains open, language not changed).
I am getting very suspicious about GUI scripting - does it really work reliably? Is there a good, complete reference manual on using AppleScript to control GUI’s?
¢ menus may change from on program’s version to another one (Pages and Numbers for instance)
¢ the structure of some GUI elements may change given the OS version.
Here there is a problem with the popup which is in a group in 10.4.1 and seems to fon’t in 10.5
For Pages and Numbers, some GUI items must be triggered with
click button theButton of radio group 1 under 10.4
but must be triggered with
click checkbox theButton of radio group 1 under 10.5
And sometimes it’s difficult to write localization independant scripts.
sorry i dont understand which script is “090205-045316”. I think the times in different countries have to be adjusted for to understand it. On the right hand side of a post, there is always a post number like ("#12, #13). You should refer to that in future. I would like to know why you are not using the first script that I wrote (in post #2 and which you modified in post#3 for the close button). That seems to work very reliably.
As much as I know, GUI scripting is to be used as last resort when normal scripting is not possible.
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