1 2 Previous Next 18 Replies Latest reply: May 11, 2013 10:55 AM by iamsudo
iamsudo Level 1 Level 1 (0 points)

I cannot get any reference for the context menu and its child elements.

 

Accessibility Inspector reads

 

AXApplication

    AXWindow:AXStandardWindow

        AXMenu

 

but ASE always returns an invalid index. Also tried UI Browser to no avail.

 

Any ideas?


MacBook Air, OS X Mountain Lion (10.8.3)
  • 1. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    iamsudo wrote:

     

    I cannot get any reference for the context menu and its child elements.

     

    Nor do I. However, with at least one track selected in the iTunes window, it is not too difficult, using GUI Scripting in conjunction with cliclick, to open the contextual menu and select a menu item. For example:

     

    tell application "iTunes" to activate

     

    tell application "System Events"

        tell process "iTunes"

            tell window "iTunes"

                tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1

                    tell (row 1 whose selected is true)

                        set {x, y} to position

                    end tell

                end tell

            end tell

        end tell

    end tell

     

    do shell script "/usr/local/bin/cliclick kd:ctrl c:" & (x + 5) & "," & (y + 5) & " ku:ctrl"

     

    tell application "System Events" to keystroke "Get Album Artwork" & return


     

    Tested under OS X 10.8.3 with iTunes 11.0.2 and cliclick 2.2. (When using GUI Scripting, you must enable access for assistive devices in the Accessibility System Preference pane.)

  • 2. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    Thanks, Pierre L.

     

    What I'm trying to achieve is add a track to up next via its HFS path.

     

    This script works with fixed coordinates and runs in about .3-.5 sec.

     

    set TheTrack to "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

    tell application "iTunes"

              set SelTrack to (add TheTrack)

      reveal SelTrack

      activate

      -- fixed coordinates for iTunes fullscreen 1440.900

              do shell script "MouseTools -x 380 -y 180 -controlKey -leftClick" -- context menu

              tell application "System Events" to keystroke "Add to Up Next" & return

    end tell


    This other script also works but runs in 3-4 sec.

     

    set TheTrack to "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

    tell application "iTunes"

              set SelTrack to (add TheTrack)

      reveal SelTrack

      activate

              tell application "System Events"

                        tell process "iTunes" to tell text field 1 of (row 1 whose selected is true) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to set {x, y} to position

                        do shell script "MouseTools -x " & (x + 5) & " -y " & (y + 5) & " -controlKey -leftClick" -- context menu

                        keystroke "Add to Up Next" & return

              end tell

    end tell

     

    But, if I set the track reference it runs in about .3-.5 sec.

     

    set SelTrack to file track id 15626 of user playlist id 15622 of source id 75 of application "iTunes"

     

     

    Getting the file track id is somehow slowing getting coordinates to a crawl and vice versa. Any ideas?

  • 3. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    Unfortunately, there seems to be an issue with the AppleScript “reveal” command, at least in iTunes 11.0.2. I'm unable to have it work properly.

  • 4. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    The following script runs much better although still relies on MouseTools —

    perform action "AXShowMenu" would still slow it down.

     

    set TheTrack to "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a" -- test track

    tell application "iTunes"

              set SelTrack to (add TheTrack)

              reveal playlist "Purchased"

              activate

              tell application "System Events" to tell process "iTunes" to tell text field 1 of (first row whose value is not null) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to set {x, y} to position

              reveal SelTrack

              do shell script "MouseTools -x " & (x + 5) & " -y " & (y + 5) & " -controlKey -leftClick" -- context menu

              tell application "System Events" to keystroke "Add to Up Next" & return

    end tell

     

    Would you mind testing it to have a second opinion?

  • 5. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    Here's another variation, which doesn't need to “reveal” the track or the playlist:

     

     

    set theFile to POSIX file "/Users/pierre/Music/iTunes/iTunes Media/Music/Compilations/Gold - Greatest Hits/11 Chiquitita.m4a" -- just an example (ABBA Gold - Chiquitita)

     

    tell application "iTunes"

        add theFile

        set theIndex to index of result

        activate

    end tell

     

    tell application "System Events"

        tell process "iTunes"

            tell window "iTunes"

                tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1

                    set {x, y} to position of row theIndex

                end tell

            end tell

        end tell

    end tell

     

    do shell script "/usr/local/bin/cliclick kd:ctrl c:" & (x + 5) & "," & (y + 5) & " ku:ctrl"

     

    tell application "System Events" to keystroke "Add to Up Next" & return


  • 6. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    Here's another variation, which doesn't need to “reveal” the track or the playlist:

     

    Well, that was not a good idea at all. It works only when the track is visible in the iTunes window. Sorry…

  • 7. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    Try with another song, maybe…

     

    I tried running

     

    osascript -e "tell application \"System Events\" to tell process \"iTunes\" to tell (first row whose selected is true) of outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1 to perform action \"AXShowMenu\"" > /dev/null 2>&1 &

     

    to see if I could rather use AXShowMenu, but it's nonetheless still very slow.

     

    For now I'll stick with my version of the script. Btw, did you try it?

  • 8. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    For now I'll stick with my version of the script. Btw, did you try it?

     

    I tried, but was unable to run it, no more than any of your other versions. As I said in my second post (in this thread), there seems to be an issue with the “reveal” command. Actually, the issue is most likely due to my setup of iTunes. The “reveal SelTrack” line of code reveals the track for just a brief fraction of a second, so that the next line of code cannot be executed. I've finally found out why:

     

    set TheTrack to "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"

    tell application "iTunes"

        add TheTrack --> file track id 2599 of library playlist id 1801 of source id 74 of application "iTunes"

    end tell


    Actually, library playlist id 1801 of source id 74 is the same as library playlist "Library" of source "Library", whose visible property is false.

     

     

    Of course, I could use the following workaround:

     

    set TheTrack to "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"

    tell application "iTunes"

        add TheTrack

        set theDatabaseId to database ID of result

        set TheTrack to file track 1 of user playlist "Music" of source "Library" whose database ID is theDatabaseId

        reveal TheTrack

    end tell

     

    But that's not going to make things faster.

  • 9. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    I use iTunes 11.0.2 in OS X 10.8.3 — visible of library playlist "Library" of source "Library" is set to false.

    Despite this setting reveal SelTrack effectively reveals the track via its file track id or HFS path allowing the next lines of code to execute.

    Do you first reveal playlist "Purchased"?

     

    My last version runs in about .3 sec. Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think.

  • 10. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    Does this script work for you?

     

    tell application "iTunes"

            reveal playlist "Purchased"

            reveal first track of library playlist 1 of source 1

    end tell

  • 11. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    Does this script work for you?

     

    No, it doesn't. No error, but the script again reveals the track for just a brief fraction of a second. The reason is that library playlist 1 of source 1 is the same as library playlist "Library" of source "Library", and has nothing to do with the “Purchased” playlist.

     

    Could it be that your files are not located in the standard folder, that is ~/Music/iTunes/iTunes Media/Music?

     

    Let's compare:

    Your example file: "HD:Users:me:Music:iTunes:iTunes Music:Your Music:test.m4a"

    My example file: "Macintosh HD:Users:pierre:Music:iTunes:iTunes Media:Music:Compilations:Gold - Greatest Hits:11 Chiquitita.m4a"

     

    By the way, I've not the slightest idea of what you are talking about when you say “Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think”.

  • 12. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    Pierre L. wrote:

     

    The reason is that library playlist 1 of source 1 is the same as library playlist "Library" of source "Library", and has nothing to do with the “Purchased” playlist.

     

    I get to reveal the Library playlist if I first reveal the Purchased playlist *or* if I run reveal first track of library playlist 1 of source 1 twice in a row.

    Could it be that your files are not located in the standard folder, that is ~/Music/iTunes/iTunes Media/Music?

     

    I still use the old ~/Music/iTunes/iTunes Music folder organization — I doubt that that has something to do with your problem.

    By the way, I've not the slightest idea of what you are talking about when you say “Bear in mind that I use TotalSpaces with no transitions to achieve this speed — with Mission Control alone it'd probably need introducing delays, I think”.

     

    TotalSpaces is a spaces manager app that among other features has immediate space switching. With Mission Control one would introduce delays for my script to work.

  • 13. Re: applescript - cannot reference iTunes context menu
    Pierre L. Level 4 Level 4 (3,860 points)

    Thanks for your explanations. Now, since you already have a working version of your script and since I personally don't need a script that “add a track to up next via its HFS path”, everything is nice.

  • 14. Re: applescript - cannot reference iTunes context menu
    iamsudo Level 1 Level 1 (0 points)

    Just one last question, is there any way to access the "Add To…" context menu (the one with the > in a circle) with GUI scripting?

1 2 Previous Next