Sunday, June 13, 2021

#26 2009-02-09 02:44:16 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: How to script the selection of an option from a popup menu?

pwannh wrote:

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.


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:

Applescript:



display dialog (name of current application)

You will get the name of the script ;-)

Yvan KOENIG (from FRANCE lundi 9 février 2009 21:44:01)

Offline

 

#27 2009-02-09 02:54:20 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: How to script the selection of an option from a popup menu?

I tried with the

click pop up button 1
click menu item myLanguage of menu 1 of pop up button 1


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

keystroke myLanguage & return
click pop up button 1 of group 1


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 ;-)

Yvan KOENIG (from FRANCE lundi 9 février 2009 21:54:05)

Offline

 

#28 2009-02-09 10:41:15 pm

chris2
Member
Registered: 2008-09-10
Posts: 331
Website

Re: How to script the selection of an option from a popup menu?

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 ;-)


Sorry, "Yvan". I think i have messed up with your name quite a few times even in Apple Discussions>>Numbers forum.  I will take care next time.:cool:


Tireless endeavour is the effort required to restrict the modifications of the mind
www.thehungersite.com

Offline

 

#29 2009-02-10 02:31:57 am

pwannh
Member
Registered: 2009-02-03
Posts: 9

Re: How to script the selection of an option from a popup menu?

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.

Offline

 

#30 2009-02-10 07:07:33 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: How to script the selection of an option from a popup menu?

Hello

(a) I'm like you, I just got what is available on Apple pages dedicated to AppleScript to use GUI scripting.

(b) I never found a way to get the name of the calling application from a script.

I was quite decided to quit this kind of duty when I discovered thisService
available from:
http://wafflesoftware.net/thisservice/services/

It's a tool allowing us to encapsulate AppleScripts in Services.

In this environment, you will see that this huge piece of code:

Applescript:


tell application "System Events"
   set _appname to name of first process whose frontmost is true
end tell

grabs the name of the true 'current app'.

More, we may link shortcuts to these services.
So it's a way to solve your entine problem.

I just discovered the version 2.0.2 so I don't know which are the enhancements upon version 2.0.1

Yvan KOENIG (from FRANCE mardi 10 février 2009 14:07:16)


Filed under: System

Offline

 

#31 2009-02-10 01:41:42 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: How to script the selection of an option from a popup menu?

Here is a new version ready to be included in a service.

Applescript:


(*
Thanks to "thisService", you may encapsulate this script in a Service.
Download "thisService" for free, from
[url]http://wafflesoftware.net/thisservice/[/url]

When creating the service click the button "Produce output"

At this time I don't know why there is a long pause (with wheel of death)
at the beginning but it does it's duty.

If you wish you may play with the kind of service.
If you may select the name of the language in a TextEdit document, you may edit the instructions
on process()
   set input to "Svenska"
   
   as
   on process(input)
   --set input to "Svenska"
   and create a service of kind "Filter (both)"
   
   It will use the selected sting as the language to apply.
   
   Yvan KOENIG (Vallauris, FRANCE)
   10 février 2009
*)


on run
   set rezult to my doYourDuty("TextEdit", "Svenska")
   if rezult is not "" then error rezult
end run

--=====

(* Here is the handler used by thisService to enter the script *)

on process()
   set input to "Svenska"
   tell application "System Events" to set app_name to name of process 1 whose frontmost = true
   set rezult to my doYourDuty(app_name, input)
   if rezult is not "" then
       tell application "System Events"
           activate
           display dialog rezult
       end tell
   end if
   return ""
end process

--=====

on doYourDuty(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 (system attribute "sysv") mod 4096 div 16 < 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
           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 (system attribute "sysv") mod 4096 div 16 < 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
           end if -- count of windows
       end tell -- process
   end if -- curApp
   
   if flag = 0 then return "The Spell Checker window was open, you closed it !"
   if flag = 2 then return myLanguage & " was already set"
   if flag = 3 then return "The opened window can't be accessed !"
   return ""
end doYourDuty

--=====

Yvan KOENIG (from FRANCE mardi 10 février 2009 20:41:29)


Filed under: System

Offline

 

#32 2009-02-10 03:27:04 pm

pwannh
Member
Registered: 2009-02-03
Posts: 9

Re: How to script the selection of an option from a popup menu?

Oh wow, that's a lot of work you have been doing Yvan - thanks a lot! smile
I will need some time to digest and learn about thisService. Will report back when I have understood and tried it.
Peter

Offline

 

#33 2009-02-11 10:53:58 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4622

Re: How to script the selection of an option from a popup menu?

Oops, I missed that when no window is open, we aren't allowed to send the shortcut :-(

Applescript:


--(SCRIPT triggerPopUp]{code}
(*
Enregistrer le script en tant qu'Application ou Progiciel : triggerPopUp.app
déplacer l'application créée dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:
Il vous faudra peut-être créer le dossier Applications.

menu Scripts > triggerPopUp

Le script essaie de définir le langage utilisé par le vérificateur orthographique
selon la property input.

--=====

L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".

-- NOUVEAU NOUVEAU NOUVEAU

Grace au programme gratuit ThisService, il est possible d'encapsuler ce script dans un service.
Télécharger ThisService depuis:
[url]http://wafflesoftware.net/thisservice/[/url]

Lors de la création du service, cliquer le bouton "Produce output".
Il est possible d'associer un raccourci clavier au service.

+++++++++

Save the script as an Application Bundle: triggerPopUp.app

Move the newly created application into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:
Maybe you would have to create the folder Applications by yourself.

menu Scripts > Pages > triggerPopUp

The script try to set the language used by the spell checker
given the setting of the property input.

--=====

The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.

-- NEW NEW NEW NEW

Thanks to the free program ThisService, we may encapsulate this script in a Service.
Download ThisService from
[url]http://wafflesoftware.net/thisservice/[/url]

When creating the service click the button "Produce output"
We may link the service to a shortcut.

At this time I don't know why there is a long pause (with wheel of death)
at the beginning but it does it's duty.

If you wish you may play with the kind of service.
If you may select the name of the language in a text document, you may edit the instruction
on process() (* or process(input) *)

   as
on process(input) (* or process() *)
   
and create a service of kind "Filter (both)".
   
It will use the selected string as the language to apply.
   
Yvan KOENIG (Vallauris, FRANCE)
10 février 2009
11 février 2009
*)

property input : "Svenska"

--=====

on run
   set rezult to my doYourDuty("TextEdit", input)
   if rezult is not "" then error (rezult & " !")
end run

--=====

(* Here is the entry point used by thisService *)

on process() (* or process(input) *)
   local the_language, rezult
   set the_language to input
   tell application "System Events" to set app_name to name of process 1 whose frontmost = true
   set rezult to my doYourDuty(app_name, the_language)
   if rezult is not "" then
       tell application "System Events"
           activate
           display dialog (rezult & " !")
       end tell
   end if
   return ""
end process

--=====

on doYourDuty(curApp, myLanguage)
   local flag, knt, nw1
   tell application curApp to activate
   delay 0.5
   set flag to 1
   if curApp is not "Script Editor" then
       tell application "System Events" to tell process curApp
           set knt to count of windows
           if knt = 0 then (*
don't apply the shortcut if no window open *)

               set flag to 4
           else
               keystroke ":" using command down
               delay 0.2
               if (count of windows) ≥ knt then
                   set nw1 to name of window 1
                   if (system attribute "sysv") mod 4096 div 16 < 5 then
                       try
                           tell window nw1
                               set flag to 0
                               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
               end if -- count of windows
           end if -- knt
       end tell -- process    
   else (*
special case: "Script Editor" *)

       tell application "System Events" to tell process curApp
           set knt to count of windows
           if knt = 0 then (*
don't apply the shortcut if no window open *)

               set flag to 4
           else
               keystroke ":" using command down
               delay 0.2
               if (count of windows) ≥ knt then
                   set nw1 to name of window 1
                   if (system attribute "sysv") mod 4096 div 16 < 5 then
                       try
                           tell window nw1
                               set flag to 0
                               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
               end if -- count of windows
           end if -- knt
       end tell -- process
   end if -- curApp
   
   if flag = 0 then return ""
   (* here, flag = 1, 2, 3 or 4 *)
   if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
       return item flag of {"Le vérificateur orthographique était ouvert, vous l'avez fermé", myLanguage & " était déjà appliqué", "La fenêtre ouverte est inaccessible", "Pas de fenêtre ouverte"}
   else
       return item flag of {"The Spell Checker window was open, you closed it", myLanguage & " was already set", "The opened window can't be accessed", "No window open"}
   end if
end doYourDuty

--=====

Yvan KOENIG (from FRANCE mercredi 11 février 2009 17:53:42)


Filed under: System

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)