PopUp Menu GUI scripting

I have a interface where I have to select an item from a popup menu, test it and then select the next time from the pop up menu, test it, repeat.

The number and names items in the popup menu are not the same each time I run the app.

Is there a way I can get the names of all the items in a popup menu?

Here is an old script doing that.

--(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:
http://wafflesoftware.net/thisservice/

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 
http://wafflesoftware.net/thisservice/

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
					tell me to (system attribute "sysv")
					if result 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

--===== 

I just edited the instruction grabbing the «sysv» attribute which was issuing a non fatal error (triggering an OSAX from the internal of a tell application block).

Yvan KOENIG (VALLAURIS, France) lundi 31 mars 2014 10:39:00