Move Cursor to a Specific Tabbed Field

You might think this is not specific to Keyboard Maestro but navigation techniques like the one I’m asking about could be central to some automations and in fact I want to use it with Keyboard Maestro.

In a file dialog that I’m manipulating I want to tab to a specific tabbable field. It’s not just the first tabbed field, but sometimes it is.

Is there a method - Applescript or key press (or something else) - to guarantee the cursor is in the first tabbed field of a dialog? Or the 5th, for example? Or the last?

The web has proven no help in this regard.

Hey Martin,

You'll have to be more specific about what dialog in what application.

They can be very different.

Open and Save dialogs are usually the same between apps, but they're different from each other – and can vary according to the app.

Here's an example that works in the Save dialog of Google Chrome, where I'm using the search field as the target.

-Chris

tell application "System Events"
   tell (first process whose frontmost is true)
      tell (first window whose subrole is "AXStandardWindow")
         if exists of sheet 1 then
            tell sheet 1
               tell group 2
                  tell text field 1
                     set focused to true
                  end tell
               end tell
            end tell
         else
            beep 2
         end if
      end tell
   end tell
end tell

Hey Chris. The use of AppleScript is probably closer to my actual need than I originally described...

... Here’s what inspired the question...

... Just recently Excel seems to have changed, without warning :frowning: , to saving graphs as pictures in SVG format, rather than PNG.

My need was to get to the control in the Save File Dialog it pops up that specifies PNG, SVG, etc. And to force that, with the letter “P” to PNG.

(md2pptx doesn’t yet (and maybe never will but I’m working on it) support SVG so I need PNG.)

Today I use “Click On Found Image” to get to that drop down list.

Sometimes (and don’t you just hate it when it’s “sometimes” :slight_smile: ) the cursor doesn’t go back to the file name entry field after that.

Hence my desire to force the cursor back there.

I reckon - with AppleScript derived from yours - I could automate both the “PNG” selection and getting the cursor back to the file name entry field.

(And, as a Performance guy, it would probably do it much quicker.)

(And and :slight_smile: , it could probably automate generating a file name and typing it into that entry field and copying it to the clipboard or directly into BBEdit as a Markdown image reference.)

So thanks @ccstone for this!

Hey Martin,

What's the name of the menu item for PNG?

What I would do is try using the System Keyboard Prefs to make an App Shortcut for it.

See “Save PDF to Desktop” and “Save PDF to Receipts Folder” in the following image?

image

These represent items in the PDF pop-up menu of the system print dialog.

By giving them odd-ball keyboard shortcuts I am able to use them in Keyboard Maestro macros.

If you can do this it will be very fast and bombproof.

Scripting the fields should not be that difficult.

I only have Microsoft Office 2016, but I'm happy to consult if you need help.

-Chris

This is Excel and it isn't a menu item. If it were life would be a lot easier. This is a dropdown list in an Excel file dialog. (I'm not even sure if that list is available in a standard file save dialog.)

Thanks for your offer. This might give me something to play with this weekend.

Yes, I got that from the top.

The PDF pop-up menu I am referring to is in the system print dialog.

The Keyboard system preferences can find menu items in strange places - not just the menu bar. While it may not work for you in this Excel dialogue, it's worth a try.

All you have to do is enter the exact text of the menu item, and give it a keyboard shortcut.

Then go back to your application, find the menu, and see if the OS has added a keyboard shortcut to it.

-Chris

2 Likes

@MartinPacker, it is usually best to provide a specific, real-world use case rather than trying to generalize it.

Hmmm, I am not seeing this, running Microsoft Excel 16.42.20101102 on macOS 10.14.6 (Mojave). This is Excel in Office 365. Here is what I see when I right-click on a graph and select "Save a picture":

Is this what you are seeing? If not, please post a screenshot of the picture save dialog you are seeing.

With this save dialog, I can do four Shift-TAB to select the "Save as Type", and then type select the type I want.

image

Thank you. I won’t post the Dialog because all I need to say is that where you have PNG I suddenly have SVG.

I’ll play with Shift+Tab because I hope it will be more robust than my Click On Found Image (which, to be fair, has been reliable so far). Tabbing worries me because I’ve found the starting point in the tab cycle to be unreliable in general. Also it’s a cycle so “doing potentially too many tabs” doesn’t work as a method to get to the last (or first) tabbable control.

Hey Martin,

Don't do it – tabbing around is a blind process, and you can't count on it.

If you set things up right it will work most of the time, but for a lengthy automated process it's just not bombproof enough.

All you have to do is accidentally turn off “Change the way tab moves focus” F7, and the functionality goes away with no notification to the user.

You're using the “Save as Picture“ command in Excel? And that uses a regular Save dialog – at least it does on my 2016 version.

This should get you close:

tell application "System Events"
   tell application process "Microsoft Excel"
      tell window "Save"
         tell group 1
            tell pop up button 1
               if value ≠ "PNG" then
                  
                  perform action "AXPress"
                  
                  tell menu 1
                     tell menu item "PNG"
                        perform action "AXPress"
                     end tell
                  end tell
                  
               end if
            end tell
         end tell
      end tell
   end tell
end tell

-Chris

Yes, using KM to move the focus to different fields does require that the dialog starting point always be consistent. Fortunately, I have found that the macOS Open and Save dialogs to be very consistent, always starting with the focus on the file name at the top of the dialog.

Also, these dialogs rarely change, but could do so with a major change to the macOS.

Sorry Chris, but I have to disagree for this specific use case, for the reasons I just stated above.

OTOH, I would agree that using AppleScript UI scripting would be a more robust solution.
Of course, this means the user must:

  1. Know how to write AppleScript UI scripts, which can often be very challenging
  2. Use a third-party tool, like UI Browser, to get the proper object reference to the target field. And UI Browser is not cheap at $50.
  3. Also be aware that the same changes in a major macOS that cause a TAB-based approach to fail can also cause the UI script to fail.

Chris, of course, has all the tools and skills to fairly quickly write such an UI script.

While I could have written a UI script, I found it much quicker to develop a simple KM solution. It took me only about 30 sec to determine the TAB path for the Save dialog, and then less than a minute to write the KM Actions to use it.

I think for most KM users writing the KM actions would be the fastest.
Of course, YMMV.

My own approach would normally be to quickly write the simple KM solution, and then when I have time, or if the TAB-based solution fails, develop the UI script.

A lot of this is personal preference, and I'm not trying to suggest that the TAB-based solution is best for all users, not by any means. It is just another tool in our KM tool kit that each user may consider.

This is the great thing about KM: Provides lots of solutions suitable to users with a variety of skills.

Thank you! Something to play with, even if I don’t put it into Production. It at least teaches me more of UI Scripting - with or without UI Browser’s help.

So the whole point of the original question was whether a key combo could reliably get the cursor to the first tabbable field. And then tab forwards from there. Likewise the last. The answer to this is “not as such”, from the way this thread has developed.

(The relevance to Keyboard Maestro is that a reliable mechanism to do just this could form a part of lots of automations. Now if KM could be taught (Version 10?) to do reliable things with dialogs like this that would be worth paying for (an upgrade to).)

I tried using the latest release of UI Browser for some investigation and it came up short. It doesn’t do pop up dialogs at all well. (Either that or I’m using it wrong.) Two problems:

  1. It couldn’t produce AppleScript to acquire the dialog when on screen.
  2. It couldn’t produce AppleScript to interact with the dialog’s controls.

Maybe the problems UI Browser has in this area are related to AppleScript either being incomplete or poorly understood. Which is where code snippets like @ccstone’s are vital to use and adapt.

So, I really would like to come up the learning curve some more and do more UI scripting as it relates to dialogs. (And I will.)

But for now the “blunderbuss my way with keystrokes” method is what I have “in Production”, dismal though it seems to me.

Any reason why you have two “System Events” tells, nested inside each other?

No good reason. Thanks for noticing and letting me know.

-Chris

1 Like

Hey Martin,

I don't understand what you mean by this. Please clarify.

Please be more specific about what control and what interaction.

-Chris

Hey Martin,

As I mentioned in an earlier post this is what I would generally do on my machine if possible.

It works with Excel from Office 2016:

image


This works very well with Keyboard Maestro.

  • Open the dialog.
  • Wait for button Save to be enabled.
  • Have Keyboard Maestro emulate keyboard shortcut *
  • Have Keyboard Maestro press the Save button.

This method is pretty well bombproof.

-Chris

1 Like

Ah – I forgot to mention the goodies surrounding the save dialog.

Those are added by Default Folder – a utility I've found indispensable since long before macOS X came to be.

-Chris

1 Like

@MartinPacker, I thought I covered that. You don't need to do anything. When you open the "Save" dialog the same first tabbable field is selected:

After the Save dialog is opened, a single TAB moves to the next field. a single SHIFT-TAB moves to the "last" tabbable field. It takes only four SHIFT-TABs to move to the "Save as Type" field.

Pretty much every dialog I have used always opens to the "First" tabbable field, and consistently does that until/if there is a change in the dialog. So we really don't need KM to do anything special.

It depends on the type of popup. If it is a static set of items, then UI Browser works quite well. However, if it is a dynamic set of items, that are not determined until the user clicks on the popup, then UI Browser has a hard time working well with the popup.

Take a look at Chris @ccstone's script. I'm pretty sure he used UI Browser to get the object references, although in a different format than he presented.

IAC, if you have not seen this video by @DanThomas, you may find it very helpful:

TUTORIAL VIDEO: Using UI Browser and Keyboard Maeastro

Two sayings have helped guide me for decades:

  1. Better is the enemy of good enough
  2. KISS -- Keep It Simple Stupid

Oh, and one more:
3. If it ain't broke, don't fix it. :wink:

1 Like

Thank you. I’m just nervous of the risk of breakage. In fact I thought I saw times the cursor wasn’t on the first field. Anyhow for now the method of tabbing around seems to work.

I tried another method: Copying to the clipboard as PNG and pasting into Preview and saving. The first part is fine. The second part is unbelievably slow / unreliable. Well, it was an interesting experiment - discovering Preview isn’t wonderfully scriptable.

I'm not above using the tab method – especially for non-crucial work – or temporary macros – but I've seen the above too many times over the years.

This is one of the reasons I got into UI-Scripting.

Oh, heck – in that case drop the second part and write the file directly from the clipboard with Keyboard Maestro.

Write to a File action.

That should be very fast and bombproof.

-Chris

1 Like