• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Finding the focused UI element
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding the focused UI element


  • Subject: Re: Finding the focused UI element
  • From: Philip Aker <email@hidden>
  • Date: Thu, 17 Jul 2008 00:49:33 -0700

On 08-07-16, at 19:26, Arjen Baarsma wrote:

I've only just started looking into applescript, but I've already been able to make a few tiny scripts that are making my life easier as we speak.
One script that I've been wanting to make for quite some time, but which I don't seem to be able to get to work well is one that brings up a context menu for the control that currently has keyboard focus. The idea is that I'll set up a keyboard shortcut to run this script (using quicksilver), so I'll be able to access context menus without touching the mouse (like one could do on a Windows machine using the context menu key, or like what happens when you press ctrl-space when a control/link is selected in firefox in OS X).

Once I have found the UI element that has keyboard focus, bringing up the context menu is no problem (using "AXShowMenu", although it doesn't work for all controls, but I'll worry about that later), so the main problem right now is finding the focused UI element. The most straighforward way to do this I could think of was

tell application "System Events"
set theApplication to first application process whose frontmost is true
set theControl to value of attribute "AXFocusedUIElement" of theApplication
end tell

but this does not work unfortunately. I know from a UI Browser that the attribute AXFocusedUIElement should indeed take the value of the focused UI element, so it's a mystery to me why it doesn't work.

I've also written a function that parses all UI elements in a window (and their children and children's children etc) until it finds the one that has focus, but when there are many of them (which happens quite often) it becomes VERY slow (can take minutes or even longer), so that's not going to work either.

I suspect there should be an easy solution, but I'm completely out of ideas. Searching for it on the net hasn't yielded any results (not for just applescript at least), so if anyone knows how to do this, I would be very grateful.

– Arjen

p.s. I'm running OS X version 10.4.11 (and AppleScript 1.10.7), should that be relevant.

It is.

I think you will have to search out third party utilities effect a solution because although a UI Browser may show you the focused element, it's not always the case that System Events scripting has a mapping implemented for it or if it does, implements it correctly.

Secondly, your goal appears to be the general case (you've not specified the contextual menu item you wish to invoke). That is, you just want to pop the menu and continue on with keyboard navigation. I wonder if a more direct approach might work? For instance, I recently found out that I can assign a keyboard shortcut to the Finder's contextual menu item "Show Package Contents" (I'm using Mac OS X 10.5.4).

For the UI element search speed, you may be able to apply the Serge- Garvey techniques: <http://www.google.com/search?client=safari&rls=en-us&q=Nigel+Garvey+Applescript+speed&ie=UTF-8&oe=UTF-8 > (List Processing speed). There are a few people on this list you can help you speed up a script but you'd have to post it for them to be able to make any comments.



Philip Aker
echo email@hidden@nl | tr a-z@. p-za-o.@

Democracy: Two wolves and a sheep voting on lunch.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: Finding the focused UI element
      • From: Arjen Baarsma <email@hidden>
References: 
 >Finding the focused UI element (From: Arjen Baarsma <email@hidden>)

  • Prev by Date: Finding the focused UI element
  • Next by Date: Re: Finding the focused UI element
  • Previous by thread: Finding the focused UI element
  • Next by thread: Re: Finding the focused UI element
  • Index(es):
    • Date
    • Thread