The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Applescript: How do I select menu item with Quotes (http://hintsforums.macworld.com/showthread.php?t=74854)

Red_Menace 07-08-2007 11:57 AM

Quote:

offset of "Sync “Jordan's iPhone”" in "Sync “Jordan’s iPhone”"
The reason your offset doesn't match is that you are using different single quotes. What I did was copy characters from the result of listing the menu items (from Script Editor) and testing/comparing those. I used another application (Mail) that uses the quotes in a menu item, and was able to click on it using the ASCII codes.

tw 07-08-2007 12:48 PM

Apple sure could have made this easier... lol

mark hunte 07-08-2007 03:17 PM

The thing is you should not need to enter the name of the menu item in the script, but use what you have to get the script to do it for you.

Code:

activate application "iTunes"

tell application "System Events"
        tell process "iTunes"
                set parentObject to menu 1 of menu bar item "File" of menu bar 1
                set SyncList to name of (every menu item of parentObject whose name contains "Sync")
               
                repeat with i from 1 to number of items in SyncList
                        set SyncItem to item i of SyncList as Unicode text
                        if SyncItem contains "iPhone" then
                                click menu item SyncItem of parentObject
                        end if
                end repeat
               
        end tell
end tell

I do not have a iPhone, but it worked when i used "iPod" in the line:
if SyncItem contains "iPhone" then

Red_Menace 07-08-2007 04:19 PM

Good idea (see, this is why I don't program for a living) - my menu click handler now just looks if the item is contained in one of the menu items. In case anyone is interested, here is the handler:

Code:

on run -- example
        ClickMenu({"Script Editor", "Edit", "Find", "Find"}) -- matches "Find…"
end run


on ClickMenu(MenuList)
        (*
        clicks a menu item in an application (the application will be activated)
            the last menu item doesn't need to be exact, just close enough for a match
                parameters -                MenuList[list] -
                                                item 1 [string]: the application name
                                                item 2 [string]: the menu bar item name
                                                    item(s) 3+ [string]: the menu item(s)
                returns [boolean]:        true if successful, false if no match or error
        *)
        try
                set ItemCount to the count of the items in MenuList
                if ItemCount is less than 3 then error "The menu item list contains too few items"
                set ApplName to the first item of MenuList
                set MenuName to the second item of MenuList
                set MenuItem to the last item of MenuList
                tell application ApplName to activate
                tell application "System Events"
                        set MyMenu to (application process ApplName)'s (menu bar 1)'s (menu bar item MenuName)'s menu MenuName
                        if ItemCount is greater than 3 then -- process any sub menu items
                                repeat with MyItem from 3 to (ItemCount - 1)
                                        set MenuName to item MyItem of MenuList
                                        set MyMenu to MyMenu's (menu item MenuName)'s menu MenuName
                                end repeat
                        end if
                        set MenuItemList to the name of every menu item of MyMenu -- get a list of all the menu items
                        repeat with MyItem in MenuItemList
                                if MyItem contains MenuItem then -- look for a match
                                        click MyMenu's menu item MyItem
                                        return true -- match found
                                end if
                        end repeat
                end tell
                return false -- no match
        on error ErrorMessage number ErrorNumber
                if (ErrorNumber is -128) or (ErrorNumber is -1711) then -- user cancelled
                else
                        activate me
                        display alert "ClickMenu Handler Error " & (ErrorNumber as string) message ErrorMessage as warning buttons {"OK"} default button 1
                end if
                return false -- error
        end try
end ClickMenu


JordanX 11-17-2007 11:01 AM

Red_Menace and Mark Hunte,

Just wanted to follow up with an update. I dropped the project and worked on some other things before Red posted the solution above. Went back to the project today and BINGO!

the code works flawlessly now. I'm so excited I don't know what to do.

Thank everyone for contributing to this post, I'm sure it helps tons of people.

Thanks, Jordan


All times are GMT -5. The time now is 05:51 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.