How Can Make KM to Select an Item in a Combo List Box?

Hi Murat,

Here's your macro back, with a couple of slight adjustments to the first few actions only (everything I adjusted is indicated with red notes).

Anytime you need to click on a found image, it's good to add a pause until said image is found action before the click action instead of an arbitrary number. This is because sometimes it can take longer than others for the image to appear (for a variety of reasons) and your click action may fail. So here are the adjustments I made:

  1. I adjusted the first few actions to add a pause until the image is found.
  2. I adjusted the fuzziness just a hair.
  3. I also selected best in the click action instead of unique.
  4. I also disabled your variable that sets screen count because the switch action can read that token directly without the need of a variable.

Replicate those things with the rest of the image actions and see if it improves any.

PS. This is likely doable with just AppleScript. It looks like you are at least somewhat familiar with it since I see a script at the beginning of the macro. If you're interested, I'll share an AppleScript I wrote to toggle Siri on/off so you can see a little more about how to use it to interface with the systems preferences panes.

Switch orientation Dell.kmmacros (321 KB)

Macro screenshot (click to expand/collapse)

Hi. In addition to the good tips cdhomer sent, I think that some of your click on image commands can be replaced by using menu commands using the View menu in System Preferences.

Hi RIchard,
I am probably missing something here because I can only see the Screens options in the View menu (respectively Moniteurs and Présentation in French), and not the sub items and panels of that I need to change the screen orientation.

Thank you Chris. Unfortunately, I have already tried all the part of my macro that you have modified was already working without any problem. As I have explained, the hiccups start at the test on 270° or not as the selected and visible option in the combo.
I would have liked to understand what is happening here because I could meet this problem in other contexts but I think I will stop wasting your time and mine, and continue the aborted macro by clicking myself on the Standard item. The other case is already handled by the macro it seems, because it does not seem to see the 270° item when it is the initially active one, and never enters this part of the IFELSE block... But, strangely enough, it can find it in the ELSE branch without any problem....

After some more experiments, I see that the Fuziness slider has a hair-triggering effect, I either get false positives or false negatives depending if I put it slightly lower or higher.
Probably when 270° is the active item, the image does not contain enough information to be located by KM in a useful way. I think I can stop now.
I will check if I can set the external screen orientation directly with AppleScript, I do not know much about the dictionary of the Settings app.

You're welcome Murat, and my apologies because I should have explained myself better. I meant that I made a few adjustments to the first few actions only as an example, that you could follow in adjusting the rest of the found image related actions. I didn't have time yesterday afternoon to go through and adjust all of them on my own. So even though those were working fine, I'm curious if making those same adjustments to the 270º related actions would help the issue.

Either way, I will look later on when I have more time for an AppleScript solution to this too since that is generally more reliable than found images.

Hi, I guess you're on a different version maybe. Here's an example that I pulled from MacWorld UK. My View menu looks like this. I'm on Monterey 12.2.

@myildi so I've been working on some AppleScript for you... but I am at an impasse.

I do not have an external monitor I can hook up to my MacBook to complete the script... but with what I have so far you can likely figure out the rest.

Basically what you need to do at this point is add an additional script after the click action to click the orientation dropdown menu and then click the appropriate entry. Since all I have is an iPad to hook up, and it doesn’t show the same settings as an actual monitor, that’s the part that I cannot write. If you've never heard of UI Browser, I suggest you take a look at it because it is a must-have for GUI scripting. It has a free demo.

Please note too that while this works on my system, it may need tweaks to work on yours, especially since yours is in a different language. For instance, the application names, and perhaps the com.apple.preference.displays parts might need to be changed to their French counterpart; but I simply don't know.

Here's the AppleScript in case you want to look at it before/without downloading the macro.

AppleScript (click to expand/collapse)
----------------------------------------------------------
# Author:			Chris Thomerson (@cdthomer)
#
# Current Version:	1.0 (Initial script)
# Version History:	1.0 (Initial script)
#
# Created:			Saturday, February 5, 2022
# Modified:			Saturday, February 5, 2022
# macOS:			12.1 (Monterey)
# Tags:				System Preferences, Displays, @myildi
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
----------------------------------------------------------

tell application "System Preferences"
	
	if it is running then
		set sysPrefRun to true --sets variable to indicate System Preferences was already running
		if current pane exists then --verifies that System Preferences is open to a preferences pane
			set currPaneExists to yes
			set currPane to the id of the current pane --sets current preferences window to variable			
		else
			set currPaneExists to no
		end if
	else
		set sysPrefRun to false --sets variable to indicate System Preferences was not running
		tell it to run
	end if
	
	# Opens Displays preferences pane
	reveal pane id "com.apple.preference.displays"
	activate
	
	tell application "System Events"
		tell application process "System Preferences"
			
			# Pauses until the Displays pane loads
			repeat until name of front window is "Displays"
				delay 0.1
			end repeat
			
			# Pauses until Display Settings button appaers
			repeat until button 1 of window 1 exists
				delay 0.1
			end repeat
			
			# Clicks on Display Settings button
			click button 1 of window 1
			
			# Pauses until the external monitor appears in the list
			repeat until row 2 of outline 1 of scroll area 1 of sheet 1 of window 1 exists
				delay 0.1
			end repeat
			
			# Does some math to set variables for KM to click at
			tell row 2 of outline 1 of scroll area 1 of sheet 1 of window 1
				set {xPos, yPos} to position
				set {xSize, ySize} to size
				set xClick to (xPos + (xSize / 2))
				set yClick to (yPos + (ySize / 2))
			end tell
			
		end tell
	end tell
end tell

### Requires Keyboard Maestro 8.0.3+ ###
### Sets local variables for KM to click at ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	setvariable "local__xClick" instance kmInst to xClick
	setvariable "local__yClick" instance kmInst to yClick
end tell

Change monitor orientation on Monterey (for myildi).kmmacros (5.6 KB)

Macro screenshot (click to expand/collapse)

I have this also Richard but using this menu would only replace one step and it is already working quite quickly in the macro already. I was hoping that following your suggestion would allow me to dig deeper in the screens submenus. Unfortunately, when I open the sub panel, the View menu does not get filled with the sub options.

Thank you very much Chris for having taken time to write this, I will go through it and I think it could indeed help me to select elements that does not seem to correspond to a menu/com/button on the sub panel interface.
I have indeed come across UI Browser in my searches and downloaded it for checking the references in the subpanel. Even it seems to get lost on some items and cannot provide usable references and generate AS instructions. Preference panels seems quite strange beasts indeed.
I have also came across a very nice script that creates a text file with references to all objects in the selected app's window : http://macscripter.net/viewtopic.php?pid=37674#p37674

and yes, this script also gets lost in some areas of the external monitor pane and cannot provide usable references....

Regarding UI Browser, I have a video on the use of it with Keyboard Maestro:

I'll admit I haven't watched the video in some time, but it should help understand the process.

I've got some other ideas on the subject of menus, but I'll post it in a separate topic.

1 Like

Thanks a lot Dan! That is very useful indeed. I have missed the Hold CMD key trick.
I can now get the reference to the Rotation box and I can read its value, but when I try to change its value, its does not seem to work. In fact, UI Browser does not give me the command set... because it is grayed out. When I do it by hand, nothing happens. I need to be smarter probably, like opening the combo box and then send keys to navigate in it. KM could probably make it possible for me. I will need to make more trials and errors :slight_smile:

Moreover, my script is blocked before because even with UI Browser I cannot find a way to click the button for entering the individual screen settings in the first screen

click button "Réglages d’affichage…" of window "Moniteurs" of application "System Preferences"

gives me an error message

Erreur dans System Preferences : «class butT» "Réglages d’affichage…" of window "Moniteurs" ne comprend pas le message « click »

even if UI Browser tells me that this button has "Appuyer" = Click I imagine as Action.

I'm not sure what you've tried, and what doesn't work. But you can have UI Browser generate the AppleScript to return the position of the button, then use KM to click it. Wait a tenth of a second or so, then use the cursor keys or whatever to select the item you want.

I can't remember if I mentioned it or not, but you can either have KM type the first letter of what you want, or if that won't work, have KM do a "Home" key, then use arrow keys.

Speaking of button positions, and again I can't remember whether I mentioned this before or not, but I'm willing to bet that menu button is always in the same position of the window, so you could just hard-code the position at which to click.

Hey there, sometimes an app doesn’t accept clicks from AppleScript events even if they're written that way. That's why in my example macro, I recorded the position to KM local variables and used KM to click it like Dan mentioned.

So I've been looking through my JXA code for handling popup menus, and the truth is, once you know the position of the control that pops up the menu, you have everything you need to know.

And like I said before, if it's something like a System Preferences window, you can count on the control being in the same place relative to the window.

I'll post an example later this evening, because I'll be out for the rest of the day.

Thanks a lot for all your suggestions! Following your ideas, I have discovered how UI Browser could tell me where to click. I do not have access to the second monitor for two days but I will try this when I get back home. I have tried it on what I can see with only one monitor and I do not see why it would not work with the full setup. Let's see...

I continue to work on this macro because I learn a lot thanks to your help!
I am pretty novice in AS, and completely ignorant in GUI scripting with it.

Thanks a lot!

Best regards,

Murat

2 Likes

Hi Murat, glad to hear you're making progress. Don't worry about being novice in AppleScript and not knowing about GUI scripting. I had essentially zero experience with AppleScript last fall but thanks to the help of folks here and just tinkering around with UIB I've learned a lot over the last few months.

Have a great day!

-Chris

1 Like

Hello again,

Thanks to all your help, I am getting better at using the UI Browser, and I have been able to write a script that does what I want except for one small issue that I have difficulty tracing.

tell application "System Preferences"
   quit
   delay 1
   launch
   activate
   reveal pane id "com.apple.preference.displays"
   activate
   delay 2
   tell application "System Events"
      tell process "Préférences Système"
         -- insert GUI Scripting statements here:
         
         click button "Réglages d’affichage…" of window "Moniteurs"
         -- click button 1 of window 1
         delay 2
         select row 2 of outline 1 of scroll area 1 of sheet 1 of window "Moniteurs"
         delay 1
         if (get value of pop up button "Rotation :" of sheet 1 of window "Moniteurs") is equal to "Standard" then
            click pop up button "Rotation :" of sheet 1 of window "Moniteurs"
            key code 125
            key code 125
            key code 125
            key code 36
            delay 1
            click button "Terminé" of sheet 1 of window "Moniteurs"
            delay 0.5
            click button "Confirmer" of sheet 1 of window "Moniteurs"
         else
            click pop up button "Rotation :" of sheet 1 of window "Moniteurs"
            key code 126
            key code 126
            key code 126
            key code 36
            delay 1
            click button "Terminé" of sheet 1 of window "Moniteurs"
            
         end if
         
      end tell
   end tell
end tell

Except for the line:

click button "Confirmer" of sheet 1 of window "Moniteurs"

Where AS clicks indeed this button, and confirms the rotation of 270° (this is not necessary when switching to the Standard orientation), but nevertheless gives an error message telling that it cannot obtain sheet 1 of window "Moniteurs" of process "Préférences Système".

This is not really important, since the script is working, but being so close to perfection :slight_smile: I would like to avoid it.

Do you have any idea why I get this message?

PS. And you are right, UI Browser is a very useful tool. I will jump the gun and buy it too I think.

1 Like

Without being able to test it (since I don't have Monterey on my iMac where I have an external monitor) it's hard to say. Perhaps the button "Confirmer" is not part of sheet 1 of window "Moniteurs" of process "Préférences Système" but the fact that it is able to be clicked in that same portion of the script indicates that it is. Hard to say. But if it's working properly that's good at least :sweat_smile: