Re: Accesing menu bar items.
Re: Accesing menu bar items.
- Subject: Re: Accesing menu bar items.
- From: kai <email@hidden>
- Date: Wed, 14 Mar 2007 01:07:03 +0000
On 22 Feb 2007, at 00:13, Irwin Poche wrote:
On Feb 20, 2007, at 8:46 PM, kai wrote:
On 20 Feb 2007, at 03:39, Diego Barros wrote:
I was wondering if it was possible to select OS X menu bar items?
Just to be clear, I am referring to the icons on the top-right in
OS X. For example, the Bluetooth icon, volume control icon, etc.
I have an application that has a few menu items which I would
like to select via AppleScript.
The first trick is to identify the required menu extra by its
description, Diego - so that, whatever its current position in the
menu bar, it should still be possible to target accurately.
We can get a list of descriptions with something like: <snip>
Apparently not all icons have a name - iSync and Keychain Access,
for instance. They return "missing value".
None of my third party icons, Drag Thing, iKey, or
MenuCalendarClock appear at all.
Both valid points, Irwin. And since there's evidently more than one
way in which items might be placed to the right of the menu bar, it
may be worth briefly revisiting this subject.
Most of Apple's MenuExtra plugin files are located in /System/Library/
CoreServices/Menu Extras/, so we can get a fairly comprehensive list
using something like:
------------
list folder (path to "csrv" as Unicode text) & "Menu Extras" without
invisibles
--> {"AirPort.menu", "Battery.menu", "Bluetooth.menu",
"Classic.menu", "Clock.menu", "Displays.menu", "Eject.menu",
"Fax.menu", "HomeSync.menu", "iChat.menu", "Ink.menu", "IrDA.menu",
"PCCard.menu", "PPP.menu", "PPPoE.menu", "RemoteDesktop.menu",
"Script Menu.menu", "Sync.menu", "TextInput.menu", "User.menu",
"Verizon.menu", "Volume.menu", "VPN.menu"}
------------
As far as I can see, the only exception is the Keychain.menu file,
which resides within the Keychain Access application bundle: /
Applications/Utilities/Keychain Access.app/Contents/Resources/
Keychain.menu.
The application that (among other things) controls which Apple Menu
Extras appear on right hand side of the Menu bar is SystemUIServer (/
System/Library/CoreServices/SystemUIServer.app/). A list of the
user's currently installed Menu Extras is kept in the property list
file: ~/Library/Preferences/com.apple.systemuiserver.plist.
I believe the API used by Apple to develop Menu Extras is the private
NSMenuExtra class, which allows them to operate within the
SystemUIServer address space. The order in which they appear in the
menu can be changed by command-dragging their icons. They can also be
removed by command-dragging them from the menu.
So how do these Menu Extras differ from third-party menu status items?
From what I've gathered so far, many of the latter are developed as
individual (faceless, background-only) applications - probably using
a separate API intended for third-party developers: the NSStatusItem
class. These apparently differ from Apple's Menu Extras in a number
of ways; they don't operate within the SystemUIServer address space -
and can't usually be repositioned or removed by command-dragging.
(Removal is normally accomplished by selecting the "Quit" option from
the menu.)
In the context of this discussion, they also can't be accessed via
SystemUIServer's GUI. In theory, GUI access would be through the
individual application process; typically <AXApplication:
"application name"/AXButton>. In practice, at least from initial
tests, GUI Scripting seems unable to hook into any such UI element.
If the same is true of the OP's target application, then I fear he
may be out of luck.
Incidentally, it seems some developers have also managed to reverse-
engineer the Menu Extra API, to produce third-party Menu Extras that
infiltrate SystemUIServer's address space (a move probably not
welcomed by Apple). One example is Unsanity's Cee Pee You:
<http://www.unsanity.com/ceepeeyou>
In spite of the difficulties in accessing third-party menu status
items, it should be rather easier to address the other issue raised
by Irwin; that of 'nameless' Menu Extras. Instead of identifying them
by description, we could use their filename to determine their
position in the menu bar - and then target the required menu items as
before. For example:
------------
to click_menu_extra at menu_list
tell application "System Events"
set extra_list to value of property list item "menuExtras" of
property list file ¬
(preferences folder's path & "com.apple.systemuiserver.plist")
tell menu_list's beginning
if it is not in (extra_list as string) then return my (beep)
repeat with current_item from 1 to count extra_list
if extra_list's item current_item ends with it then exit repeat
end repeat
end tell
tell process "SystemUIServer"'s menu bar 1
click menu bar item current_item
repeat with item_name in rest of menu_list
click (first menu item of result's menu 1 whose name is item_name)
end repeat
end tell
end tell
end click_menu_extra
click_menu_extra at {"Keychain.menu", "Open Security Preferences…"}
------------
Here's a couple of modified versions of other calling statements, to
help demonstrate the accessing of different levels of nested menu
items. As before, these assume that library scripts are shown in the
Script Menu:
----------------
click_menu_extra at {"Script Menu.menu", "Finder Scripts", "About
Finder Scripts..."}
----------------
click_menu_extra at {"Script Menu.menu", "URLs", "AppleScript Related
Sites", "AppleScript Website"}
----------------
---
kai
_______________________________________________
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