8 Replies Latest reply: Apr 15, 2012 7:31 PM by Jacques Rioux
TingTingBen Level 1 Level 1 (0 points)

Hi,

 

As I'm looking for weeks an elegant way to generate a Genius playlist from AppleScript although this option hasn't been built in (...), I finally found a workaround that clik on Genius button. However, this button's number (ID) change whether the displayed playlist is the Genius playlist or not. For this reason I tried to set two options in my script, laying on the ability to know whether the displayed playlist is the Genius one, thanks to "current playlist".

 

tell application "iTunes"

          if (name of current playlist) is "Genius" then

                    tell application "System Events"

  click button 12 of window "iTunes" of application process "iTunes"

                    end tell

          else

                    tell application "System Events"

  click button 10 of window "iTunes" of application process "iTunes"

                    end tell

          end if

end tell

 

However the "current playlist" seems like return to the playlist in which is the currently playing track, instead of the currently *targeted* track (as dictionary pretends)...

After many tests, it looks like it's the only reason why this script doesn't completely work.

 

Do you have any idea about how tell the targeted playlist?

 

Thanks for help!

 

Ben


MacBook Pro 2.4 GHz Intel Core 2 Duo, Mac OS X (10.6.7)
  • 1. Re: AppleScript - serious issue with "current playlist"
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    You can use the description of the button to click the button "Genius"

     

    Another problem : the window name is not "iTunes" if you open a playlist in a new window.

     

    Here is the script :

    --------------------------------------

    tell application "iTunes"

          name of (first window whose its class is browser window or its class is playlist window)

    end tell

    tell application "System Events" to tell window (the result) of process "iTunes"

          click (first button whose value of attribute "AXDescription" is "Genius")

    end tell

  • 2. Re: AppleScript - serious issue with "current playlist"
    TingTingBen Level 1 Level 1 (0 points)

    Thanks Jacques, this works perfectly (and it's clean script!). However I still lack a last improvement: most of time I use iTunes in fullscreen mode (Mac OS X Lion), and in this mode, the windows are counted as process windows or something like that. As a result your script doesn't work in this case. Do you have any idea to fix it in fullscreen mode?

     

    Anyway, the "current playlist" description provided by Apple in the AppleScript dictionary for iTunes is false as it obviously returns to the current playing instead of targeted playlist. So, to my opinion, here is an important warning to communauty.

  • 3. Re: AppleScript - serious issue with "current playlist"
    Jacques Rioux Level 4 Level 4 (1,960 points)

    TingTingBen wrote:

     

    Do you have any idea to fix it in fullscreen mode?

     

    I don't know because I'm on "Leopard".

     

    But test it :

    iTunes in fullscreen mode,  run this script in the "AppleScript Editor".

    -----------------------------------------------

    tell application "iTunes" to activate

    delay 2

    tell application "System Events" to tell (get first process whose frontmost is true)

    {it, properties of window 1, buttons of window 1 whose value of attribute "AXDescription" is "Genius"}

    end tell

    -----------------------------------------------

     

    Post the result

  • 4. Re: AppleScript - serious issue with "current playlist"
    TingTingBen Level 1 Level 1 (0 points)

    Hi again,

     

    Here the result of your script run on Lion:

     

    {application process "iTunes" of application "System Events", {minimum value:missing value, orientation:missing value, position:{0, 0}, class:window, role description:"fenêtre standard", accessibility description:missing value, focused:false, title:"iTunes", size:{1440, 900}, value:missing value, help:missing value, enabled:missing value, maximum value:missing value, role:"AXWindow", entire contents:{}, subrole:"AXStandardWindow", selected:missing value, name:"iTunes", description:"fenêtre standard"}, {button 12 of window "iTunes" of application process "iTunes" of application "System Events"}}

     

    It looks to me that Lion has a strange way to deal with fullscreen windows...

     

    Moreover, it reminded me that I succeeded few days ago in generating Genius playlist while iTunes was in fullscreen mode (but not systemacilly) thanks to the line "telle "iTunes" to activate".

    So, here is an update of your first script:

     

    tell application "iTunes"

      name of (first window whose its class is browser window or its class is playlist window)

    end tell

    tell application "System Events" to tell window (the result) of process "iTunes"

              tell application "iTunes"

      activate

      play -- seems like script doesn't work in some cases if iTunes is not playing

              end tell

              click (first button whose value of attribute "AXDescription" is "Genius")

    end tell

     

    I tried every situation I can remember that didn't work before and now it seems to be completely funtional. So

    thanks again! But I stay in touch in case you're curious about the result you asked me to post.

  • 5. Re: AppleScript - serious issue with "current playlist"
    TingTingBen Level 1 Level 1 (0 points)

    I come again with a final clean script:

     

    tell application "iTunes"

              set TheITunesWindowName to name of (first window whose its class is browser window or its class is playlist window)

      activate

              if selection is {} then reveal current track

    end tell

     

    tell application "System Events" to tell window TheITunesWindowName of process "iTunes"

              click (first button whose value of attribute "AXDescription" is "Genius")

    end tell

  • 6. Re: AppleScript - serious issue with "current playlist"
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    In 99% of applications the click command doesn't work if the application is not the frontmost, but it works in iTunes, except in full screen.

     

    This will depend, how you run the script (a script saving as Application,  a third party utility, a service Automator or other, in some cases the frontmost process will not be iTunes, then the "click" will not work.

     

     

    If you want to click on UI Element in a background application (without use the activate command), you must use -->  perform action "AXPress" of ......

    ----------------------------------------------------

    tell application "iTunes"

          if selection is {} then reveal current track

          set TheITunesWindowName to name of (first window whose its class is browser window or its class is playlist window)

    end tell

     

    tell application "System Events" to tell window TheITunesWindowName of process "iTunes"

          perform action "AXPress" of (first button whose value of attribute "AXDescription" is "Genius")

    end tell

  • 7. Re: AppleScript - serious issue with "current playlist"
    TingTingBen Level 1 Level 1 (0 points)

    I run the script file from a keyboard shortcut (Ctrl+G).

     

    The script I just posted works fine in every situation (iTunes fullscreen or not, already playing or note, a track being selected or not), but your last one doesn't work in fullscreen mode.

  • 8. Re: AppleScript - serious issue with "current playlist"
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

    TingTingBen wrote:

     

    but your last one doesn't work in fullscreen mode.

    OK, there are always exceptions.

     

    The important thing is that your script works and that this solves the problem.