Auto-Populate Value Inspector With Variables in Current Macro

This is a little suite of macros to interact with the Value Inspector.

It's a potential answer to how to display the values of all variables used in the current macro but I thought it best to start a new thread.

I've placed them in my "Keyboard Maestro" macro group. All assume v10 of KM (mainly because the main macro is a subroutine).

The macros are shown collapsed as they rely on image recognition for the Add/Remove buttons in the Inspector - and it might otherwise find those buttons in the macro itself. Take care when running these macros if you have any Add/Remove buttons visible in the currently-visible macro, or those buttons may be pressed in it instead ... with unforeseen consequences!

UPDATE: Please use the safer, alternative form of macro 2 below which doesn't suffer from this. Macro 3 could also be re-written to use AppleScript UI scripting, but I haven't got there yet!

1. Open (and set focus to) the Value Inspector
This just opens the Value Inspector (or closes and re-opens it) so that it is certain to have focus. This sometimes fails for me at the moment.

Open (and set focus to) the Value Inspector.kmmacros (7.1 KB)

2. Remove all entries in the Value Inspector
This is a separate macro, as it may be helpful to call it separately.

UPDATE: Please use the safer, alternative form below

Remove all entries in the Value Inspector.kmmacros (23 KB)

3. Show set of variables in Value Inspector
This is the main macro (well, subroutine) which can be passed a comma-separated list of variables to display (the same format that the AccessedVariables token uses). It can optionally remove all other entries in the Inspector first, using the second macro above.

Show set of variables in Value Inspector.kmmacros (22 KB)

4. Test displaying variables in Value Inspector
This is a test macro to demonstrate functionality of the main one.

Test displaying variables in Value Inspector.kmmacros (2.6 KB)

1 Like

After a cursory look at your macro set I really like it! I can foresee myself using it quite a bit for debugging. Thanks again for taking the time to come up with this.

I hope you don't mind, but I wanted to share an alternative method of clearing any existing items in the Value Inspector using GUI AppleScript. It basically gets the description of button 1 on the first row, and if it's "Delete Button" then it runs the next set of actions which are to click that button, and repeat the initial script to see if there are still any "Delete Button" buttons. Once the initial script returns false (indicating button 1 is add, and not delete), then the macro finishes.

This way you don't need the Value Inspector to be in focus, which makes it possible to simplify the Open (and set focus to) the Value Inspector macro which I have also included here.

I haven't had time to look at the other two macros but hopefully I will this evening after work and (shudder) my dental appointment :sweat_smile:

-Chris

Remove all entries in the Value Inspector.kmmacros (5.2 KB)

Open (and set focus to) the Value Inspector.kmmacros (5.0 KB)

1 Like

Thanks, Chris - that looks like a much more reliable solution :+1:

I thought about using GUI scripting, but couldn't use UI Browser to look at the Value Inspector window, as it disappears when KM loses focus! How did you work out the window/button elements?

Hope it goes well at the dentist!

Best wishes,
Steve

Right click in the Value Inspector and click the option in the screen shot :wink:

Thanks! It's a routine cleaning so it should be fine :sweat_smile:

-Chris

Ha! Thanks - I didn't know that was there! :rofl:

I've compressed your AppleScript into a single block...

Remove all entries in the Value Inspector.kmmacros (2.5 KB)

1 Like

I just barely learned it this year after over 3 years of using Keyboard Maestro :laughing:

Even better! I don't know why I didn’t write it this way... since I've written other scripts like that. My coffee apparently hadn't kicked in haha. It works a lot faster than my method too, very nice!

1 Like

I feel I should convert the Show set of variables in Value Inspector macro (which should probably be called Add variables to Value Inspector) to UI AppleScript now - but I'm not sure how to select "Variable" from the pop-up button after clicking the last even-numbered button (or the first button if there's only one). Perhaps someone else can scratch that itch :wink:

Thanks for this. It will save me a lot of time!

2 Likes

I've been working on this off and on today and I'm kind of stumped. Have you made any progress?

EDIT: This is what I came up with BUT... because of what I believe are accessibility issues (as documented here) there is a significant delay (about 6 seconds) between clicking the add button and clicking variable...adding ignoring application responses does not make a difference unfortunately.

----------------------------------------------------------
# Author:				Chris Thomerson
#
# Current Version:		1.0
# Version History:		1.0 (Initial script)
#
# Created:				Thursday, December 2, 2021
# Modified:				Thursday, December 2, 2021
# macOS:				11.6.1 (Big Sur)
#
# Notes:			
# I claim no responsibility nor guarantee compatibility
# As with any kind of custom scripts, these must be tested thoroughly on each person's device
# May be used and distributed freely
----------------------------------------------------------

tell application "System Events"
	tell application process "Keyboard Maestro"
		--		ignoring application responses
		click (last button whose role is "AXButton" and description is "Add Button") of scroll area 1 of window "Value Inspector"
		click menu item "Variable" of menu 1 of scroll area 1 of window "Value Inspector"
		--		end ignoring
	end tell
end tell
1 Like

Hey Chris,

This is Apple's long time buggy b.s. They deserve much invective for never fixing it, so please complain to Feedback - macOS - Apple

The best solution currently is to have Keyboard Maestro click the button and let System Events select the menu item.

I've encapsulated all the Keyboard Maestro actions in the AppleScript.

I have not written the code to handle the macro variable names; I leave that to the user.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/02 19:08
# dMod: 2021/12/02 19:25
# Appl: Keyboard Maestro, Keyboard Maestro Engine, System Events
# Task: Populate the Variable Value Inspector Window
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Keyboard_Maestro_Engine, @System_Events, @Populate, @Variable, @Value, @Inspector, @Window
--------------------------------------------------------

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell scroll area 1 of window "Value Inspector"
         tell (last button whose role is "AXButton" and description is "Add Button")
            set {x, y} to its position
         end tell
      end tell
   end tell
end tell

# Position Offsets
set x to x + 15
set y to y + 15

# For zeroing in on the location.
set kmMoveAction to text 2 thru -1 of "
<dict>
   <key>Action</key>
   <string>Move</string>
   <key>ActionUID</key>
   <integer>97334</integer>
   <key>Button</key>
   <integer>0</integer>
   <key>ClickCount</key>
   <integer>0</integer>
   <key>DisplayMatches</key>
   <false/>
   <key>DragHorizontalPosition</key>
   <string>0</string>
   <key>DragVerticalPosition</key>
   <string>0</string>
   <key>Fuzz</key>
   <integer>15</integer>
   <key>HorizontalPositionExpression</key>
   <string>" & x & "</string>
   <key>MacroActionType</key>
   <string>MouseMoveAndClick</string>
   <key>Modifiers</key>
   <integer>0</integer>
   <key>MouseDrag</key>
   <string>None</string>
   <key>Relative</key>
   <string>Absolute</string>
   <key>RelativeCorner</key>
   <string>TopLeft</string>
   <key>RestoreMouseLocation</key>
   <false/>
   <key>VerticalPositionExpression</key>
   <string>" & y & "</string>
</dict>
"

set kmClickAction to text 2 thru -1 of "
<dict>
   <key>Action</key>
   <string>Click</string>
   <key>ActionUID</key>
   <integer>97347</integer>
   <key>Button</key>
   <integer>0</integer>
   <key>ClickCount</key>
   <integer>1</integer>
   <key>DisplayMatches</key>
   <false/>
   <key>DragHorizontalPosition</key>
   <string>0</string>
   <key>DragVerticalPosition</key>
   <string>0</string>
   <key>Fuzz</key>
   <integer>15</integer>
   <key>HorizontalPositionExpression</key>
   <string>" & x & "</string>
   <key>MacroActionType</key>
   <string>MouseMoveAndClick</string>
   <key>Modifiers</key>
   <integer>0</integer>
   <key>MouseDrag</key>
   <string>None</string>
   <key>Relative</key>
   <string>Absolute</string>
   <key>RelativeCorner</key>
   <string>TopLeft</string>
   <key>RestoreMouseLocation</key>
   <true/>
   <key>VerticalPositionExpression</key>
   <string>" & y & "</string>
</dict>
"

set kmTypeReturn to text 2 thru -1 of "
<dict>
   <key>ActionUID</key>
   <integer>97361</integer>
   <key>KeyCode</key>
   <integer>36</integer>
   <key>MacroActionType</key>
   <string>SimulateKeystroke</string>
   <key>Modifiers</key>
   <integer>0</integer>
   <key>ReleaseAll</key>
   <false/>
   <key>TargetApplication</key>
   <dict/>
   <key>TargetingType</key>
   <string>Front</string>
</dict>
"

# Use Keyboard Maestro to Click the + Button.
tell application "Keyboard Maestro Engine"
   do script kmClickAction
   do script kmTypeReturn
   # do script kmMoveAction -- for zeroing in on the location.
end tell

tell application "System Events"
   tell application process "Keyboard Maestro"
      tell window "Value Inspector"
         tell scroll area 1
            
            tell menu 1
               # tell menu item "Variable" -- Select New Variable.
               # 	perform action "AXPress"
               # end tell
            end tell
            
            tell last text field
               set its value to "VARIABLE_NAME"
            end tell
            
         end tell
      end tell
   end tell
end tell

--------------------------------------------------------
1 Like

Hey Chris,

Thanks for chiming in and confirming it's an issue with Apple!

Some of your AppleScript is beyond my comprehension this late in the day, but it did help me think of other ways of accomplishing this. I'll take a look at it again tomorrow when I'm hopefully a little more lucid haha.

@coordinated here's something that works for me quite reliably, based on @ccstone's post above. Basically it gets the coordinates of the button and then uses Keyboard Maestro to click it. The rest is pretty much how you left it.

One thing I did notice when running it in a macro that quite a few variables (15 to be exact), is if the Value Inspector is not large enough to display 15 lines, it will only populate as many variables as the window has room for. After that, it ends up just replacing the last line with each new variable. But it's progress at least!

EDIT: Made a few tweaks: returning mouse to original location and cleaning up variables at the end of the macro. Let me know what you think.

EDIT 2: If you change the tell last button line to tell first button, it resolves the issue I mentioned earlier. The variables might not be in the preferred order, but the VI size is at least irrelevant.

46)[AS-KM] Variable Audit- Show set of variables in Value Inspector (for testing).kmmacros (15 KB)

2 Likes