Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
Old 04-14-2012, 08:16 PM   #1
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Applescript - how to click on a menu icon

I'm a newbe to Mac and applescript. I've found helper files on how to script the Finder menu items, e.g. activate Finder, Click View, Sort By and then Size.

I've managed to make it work but can't figure out how to do the same thing for a downloaded app that placed an icon on the right side of the menu bar. The app is called AppigoSync. If I click the menu bar icon, it opens a menu list with four options including "Sync Now".

I think my problem is that whereas the Finder example deals with text, e.g. "View", "Sort By" and "Size", etc. I don't know how to specify the reference to the AppigoSync icon.

The following is part of the sample I've been working from:
tell menu bar item "View"
tell menu "View"
tell menu item "Sort By"
tell menu "Sort By"
click menu item "Size"

Any ideas on how to have the script click the icon and then "Sync Now" would be greatly appreciated.

Thanks for any help.
SBoat is offline   Reply With Quote
Old 04-15-2012, 11:03 AM   #2
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 515
The menus on the right side are handled by a different process, so you need to do something like the following (change names as needed):

Code:
tell application "System Events" to tell process "SystemUIServer"
	tell (first menu bar item of menu bar 1 whose value of attribute "AXDescription" is "AppigoSync")
		click -- perform action "AXPress"
		delay 0.5
		click menu item "Sync Now" of front menu
	end tell
end tell
__________________
MacBook Pro / OS X Mavericks (10.9.3) / Xcode 5.1.1 / RubyMotion FTW
Red_Menace is offline   Reply With Quote
Old 04-17-2012, 06:05 PM   #3
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Applescript - how to click on a 3rd party application menu bar icon

I'm not sure what happened to my last post in reply but - thanks Red Menace. Your script works well for the 'apple pre-installed applications' on the right side, e.g. Sync, Time Machine, Bluetooth, etc.

I also found another example that indicates the menu bar icons as item 1, 2, etc. from left to right of the apple applications. I also found a script that lists the menu bar icon applications and it has all the apple applications.

My problem is that I can't reference my 3rd party application (AppigoSync). It is on the right side but to the left of the apple applications. I have about 5 others in addition to AppigoSync.

The sample you gave me uses the SystemUIServer process. Is there something else that accesses the 3rd party applications or some other way to clidk their menus?

Thanks again for any help.
SBoat is offline   Reply With Quote
Old 04-18-2012, 12:42 PM   #4
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 515
Those are more-or less regular applications running in the background that are using the status menu. Unfortunately, not much is exposed to System Events so there isn't much information you can get from AppleScript unless there is something in their scripting dictionaries. You might try using something like MouseTools to click at specific screen locations.
__________________
MacBook Pro / OS X Mavericks (10.9.3) / Xcode 5.1.1 / RubyMotion FTW
Red_Menace is offline   Reply With Quote
Old 04-19-2012, 10:57 AM   #5
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Code:
tell application "System Events"
	tell application process "AppigoSync"
		entire contents
	end tell
end tell
The results returned will be information about all the user interface elements that can be manipulated.
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 04-19-2012, 06:07 PM   #6
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Thanks. Sojourner - I ran the script and got pages of text but I'm not sure what it all means or what to do with it.

I'll try to keep figuring it out. Thanks.
SBoat is offline   Reply With Quote
Old 04-20-2012, 11:40 AM   #7
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Ah, sorry. It is a little overwhelming when first using.

I would suggest opening the System Events dictionary in AppleScript Editor. (Access via File > Open Dictionary.) Once you have that open, check the Processes Suite for info on UI [user interface] element commands.

I don't have AppigoSync, so I'm going to give examples using AppleScript Editor. I'm using 10.6.8, and if your OS is different, there may be differences between my examples and what will work on your Mac.

I used 'entire contents' to get a list of the UI elements available for AppleScript Editor. (I like to copy the result and paste it into a TextEdit document window. From there, I do a find/replace, replacing each comma with comma and return. This helps me delineate each item.) One of the items returned was:

Code:
menu item "Open Dictionary…" of menu "File" of menu bar item "File" of menu bar 1 of application process "AppleScript Editor" of application "System Events",
Which I turn into:

Code:
tell application "System Events"
	tell application process "AppleScript Editor"
		tell menu bar 1
			tell menu bar item "File"
				tell menu "File"
					click menu item "Open Dictionary…"
				end tell
			end tell
		end tell
	end tell
end tell
Or, a shorter version:

Code:
tell application "System Events"
	tell application process "AppleScript Editor"
		click menu item "Open Dictionary…" of menu "File" of menu bar item "File" of menu bar 1
	end tell
end tell
Let's add some other commands:

Code:
tell application "System Events"
	tell application process "AppleScript Editor"
		click menu item "Open Dictionary…" of menu "File" of menu bar item "File" of menu bar 1
		delay 2
		keystroke "system"
	end tell
end tell
I put the delay command in there to allow the computer time to open and display the Open Dictionary dialog box, to be ready for the keystrokes selecting the System Events dictionary.

From here, I could use:

Code:
		keystroke return
to choose the selected dictionary and dismiss the dialog box, but let's actually click the Choose button directly.

Wait. How do we do that? We need to know what to call it. It's probably called button "Choose", but we don't know for sure, and it may be part of some other UI element hierarchy (remember menu item "Open Dictionary…" of menu "File" of menu bar item "File"?).

Back to 'entire contents'. Yes, calling it in different places means getting different results, depending on where you are. Which makes sense when you think about it. Entire contents isn't going to tell you about the UI elements of a dialog box that's not displayed. Right now, our dialog box is displayed, so let's find out where our 'Choose' button is.

Code:
tell application "System Events"
	tell application process "AppleScript Editor"
		click menu item "Open Dictionary…" of menu "File" of menu bar item "File" of menu bar 1
		delay 2
		entire contents
	end tell
end tell
And in the results:

Code:
button "Choose" of window "Open Dictionary" of application process "AppleScript Editor" of application "System Events",
which I'll incorporate into my previous script:

Code:
tell application "System Events"
	tell application process "AppleScript Editor"
		click menu item "Open Dictionary…" of menu "File" of menu bar item "File" of menu bar 1
		delay 2
		keystroke "system"
		delay 2
		click button "Choose" of window "Open Dictionary"
	end tell
end tell
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 04-21-2012, 02:57 PM   #8
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Applescript - how to click on a menu icon

Thanks for the help on this. I've tried to follow your explanation and found the following:

menu item "Open" of menu "Services" of menu item "Services" of menu "Appigo Sync" of menu bar item "Appigo Sync" of menu bar 1 of application process "AppigoSync" of application "System Events",

From there, I tried the following script:

tell application "System Events"
tell application process "AppigoSync"
tell menu bar 1
tell menu "Appigo Sync"
tell menu bar item "Appigo Sync"
tell menu bar item "Services"
tell menu "Services"
click menu item "Open"
end tell
end tell
end tell
end tell
end tell
end tell
end tell

I get the following error:
error "System Events got an error: Can’t get menu bar item \"Appigo Sync\" of menu \"Appigo Sync\" of menu bar 1 of application process \"AppigoSync\"." number -1728 from menu bar item "Appigo Sync" of menu "Appigo Sync" of menu bar 1 of application process "AppigoSync"

I keep trying to work through it but i you can let me know where I've gone wrong, I'd really appreciate it.

Thanks again for the help.
SBoat is offline   Reply With Quote
Old 04-22-2012, 11:16 AM   #9
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
This is good for your first attempt, and believe me, with so many ui elements to 'tell', it's easy to get confused. Especially when there's menu "Appigo Sync" and menu bar item "Appigo Sync" (of app process "AppigoSync"!).

Generally, I'll use the shorter
Code:
tell application "System Events"
	tell application process "AppigoSync"
		click menu item "Open" of menu "Services" of menu item "Services" of menu "Appigo Sync" of menu bar item "Appigo Sync" of menu bar 1
	end tell
end tell
unless AppleScript is being fussy. When it's being fussy, I'll go with the longer version
Code:
tell application "System Events"
	tell application process "AppigoSync"
		tell menu bar 1
			tell menu bar item "Appigo Sync"
				tell menu "Appigo Sync"
					tell menu item "Services"
						tell menu "Services" 
							click menu item "Open"
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell
In your code, you refer to menu bar item "Services" instead of menu item, and you have menu bar item "Appigo Sync" after menu "Appigo Sync".

Don't know about you, but those are the kinds of things which can have me wanting to send a MacBook Air airborne after spending an hour or so trying to figure out what's off.
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 04-22-2012, 12:35 PM   #10
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Applescript - how to click on a menu icon

Sojourner - wow, I guess I should be more attentive. I thought I was being careful. In any case, your corrected script works perfectly. Thanks very much for the help and the explanations.

Unfortunately, after all of this, I still son't think I'll be able to do what I want. In the UI, I click the icon for AppigoSync and from its list, I click on "Sync Now"

From the earlier script you gave me that gives the entire contents, there is nothing for "Sync Now". While the new script you gave me runs without error, I tried to add - click menu item "Sync Now" and it fails; it can't find it.

I assume since Sync Now isn't in the entire contents results, it isn't available to use. Does this sound right?

In any event, I've learned a few things and appreciate all the help.

Thanks.
SBoat is offline   Reply With Quote
Old 04-22-2012, 03:09 PM   #11
benwiggy
League Commissioner
 
Join Date: Aug 2006
Location: London
Posts: 5,785
This may not be entirely helpful, but it's worth pointing out that in an ideal world, an app would have AppleScript commands so that you just tell it to do something, rather than "playing back" physical interface actions.

In other words, if an app has scripting commands to do something, you should use those, rather than automate the moving of a mouse and clicking on stuff.

Obviously, where this isn't possible, the ability to select menu items directly is great.
benwiggy is online now   Reply With Quote
Old 04-23-2012, 08:32 PM   #12
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Confused, SBoat. When you say "In the UI, I click the icon for AppigoSync...", do you mean that you click the "AppigoSync" in the menu bar, see a list of menu items, including "Sync Now", which you can click on, but is not listed when you ask for 'entire contents'?

If that's not the case, please explain. If that is the case, would you cllick on AppigoSync, take a screenshot (press cmd-shift-4, then outline the menu using the crosshairs cursor), and post it?
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 04-23-2012, 09:15 PM   #13
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Applescript - how to click on a menu icon

OK .. sorry if I've been confusing. I click on the AppigoSync icon and then Sync Now. Thanks
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2012-04-23 at 9.08.27 PM.png
Views:	184
Size:	39.9 KB
ID:	4510  
SBoat is offline   Reply With Quote
Old 04-24-2012, 02:16 AM   #14
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Ah, you're clicking on the AppigoSync icon in the menu bar. That's different than what we've been doing ... Explaining how the icons on the right side of the menu bar are menu bar apps (applications), and how the menu items on the left side of the menu bar are ... Wait. Okay, so, on those icons on the right are menu bar applications. And the items we've been scripting on the left side are menu items of applications.

As far as I know, the only way to script those menu bar apps are to script mouse clicks, clicking at predetermined coordinates. That's only going to work if the menu bar app shows up in the same place every time, and if your screen resolution stays the same. And I can't help you right at the moment with scripting that, due to workload.

I'll suggest checking out the 'click' command in System Events dictionary. You might also want to find an app that helps you get mouse coordinates on your screen.
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 04-24-2012, 07:03 AM   #15
SBoat
Prospect
 
Join Date: Apr 2012
Posts: 7
Sojourner - Thanks again for all the help. Regardless of my immediate project, I've learned some things and appreciate it.

Red Menace had suggested a tool called "Mouse Tools" that I think helps with controlling the curser and clicking on the UI. I'll see if I can make that work.

Thanks again.
SBoat is offline   Reply With Quote
Old 07-07-2012, 12:21 PM   #16
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Unsure where you are in your process, but for you and others who might be interested, the command-line utility cliclick can be used to get mouse coordinates, as well as click where needed.

For AppleScript purposes, some code snippets follow.

To save mouse coordinates to a variable
Code:
set textMouseCoordinates to do shell script "/usr/local/bin/cliclick -q"
To click at a coordinate
Code:
do shell script "/usr/local/bin/cliclick 26 12"
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Old 05-21-2013, 11:07 AM   #17
cdmx34
Prospect
 
Join Date: May 2013
Posts: 3
I'm a newbe to Mac and applescript. I would appreciate your help to write an applescript in order to automate a command to highlight a word in mac mail .... Normally after writing a word in mail I double clicking on it then right click the trackpad , then select font and then styles .

Thanks in advance .
cdmx34 is offline   Reply With Quote
Old 05-25-2013, 04:45 AM   #18
sojourner
Major Leaguer
 
Join Date: Apr 2010
Posts: 371
Uh, cdmx34, I think you should create a new thread with your request. One reason is that other people who share the same problem that you do can more easily search the forums and find your question. When multiple questions are posted in the same thread, it can be confusing and make searches more challenging/difficult.

In creating a new thread with your question, I encourage to re-word it. I'm reading your question and not getting a clear idea of what it is you want to accomplish.
__________________
see a problem; solve a problem.
sojourner is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:26 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.