10 Replies Latest reply: Jul 20, 2009 2:43 AM by dw32
dw32 Level 1 Level 1 (0 points)
Is it possible to do this? I have a pile of unsmart PLs that I want to convert to smart ones. I've got a scpt going that basically does the job, but although I say

make user playlist with properties {smart: true, name:PLname}

they always come out unsmart.

I note that the iTunes dictionary says smart is r/o, but surely for a new PL I'm just making, I can say smart:true ?

Mac OS X (10.4.9)
  • 1. Re: Make smart playlists in iTunes?
    Pierre L. Level 4 Level 4 (3,860 points)
    Maybe it's a new bug. Anyway, the script below uses [GUI Scripting|http://www.macosxautomation.com/applescript/uiscripting/index.html] to make a new smart playlist in iTunes (the Accessibility Frameworks must be enabled by clicking the checkbox labeled "Enable access for assistive devices" in the Universal Access System Preference pane):

    --BEGINNING OF SCRIPT
    tell application "iTunes" to activate
    tell application "System Events" to tell process "iTunes"
    keystroke "n" using {option down, command down} -- New Smart Playlist…
    keystroke return & return -- click the "OK" button, then the "Yes" button
    delay 0.5
    keystroke "New smart playlist" & return -- to rename the playlist
    end tell
    --END OF SCRIPT

    To build a rule from within the script, just replace the line “keystroke return & return" with the following tell block (changing the text strings to fit your needs):

    --BEGINNING OF REPLACEMENT LINES
    tell scroll area 1 of group 1 of window 1
    click pop up button 2
    click menu item "Composer" of menu 1 of pop up button 2
    delay 0.5
    click pop up button 1
    click menu item "is" of menu 1 of pop up button 1
    keystroke "Beethoven" & return
    end tell
    --END OF REPLACEMENT LINES

    Good luck.
  • 2. Re: Make smart playlists in iTunes?
    dw32 Level 1 Level 1 (0 points)
    WOW! That's utterly brilliant Pierr. Problem solved! You even use a classical composer as an example.

    I'd wondered about using the GUI interface, but I've been a bit leery of that. Your code looks as though it's pretty darned close to what I need.

    Thanxalot!
  • 3. Re: Make smart playlists in iTunes?
    dw32 Level 1 Level 1 (0 points)
    PS I apologise for losing the e off the end of your name.

    Vive les Canadiens!
  • 4. Re: Make smart playlists in iTunes?
    Pierre L. Level 4 Level 4 (3,860 points)
    It was my pleasure.

    I must add that the *tell process "iTunes"* statement is necessary only with the replacement lines, and not when using just keystroke statements.

    Message was edited by: Pierre L.
  • 5. Re: Make smart playlists in iTunes?
    dw32 Level 1 Level 1 (0 points)
    Doesn't work, Pierre. I've usually found GUI scpt encounters bruising, even apparently non-deterministic.

    Same thing here. Early on I did get the PL names (appended to the end of the scpt 'cos I hadn't run it from the menu bar). Later I did get a new un-named SPL made (about once or so). Now I can't get that far again.

    GUI scripting seems to cost a great deal of time making very slow progress. Do I need to assign a hot-key to run it?
  • 6. Re: Make smart playlists in iTunes?
    Pierre L. Level 4 Level 4 (3,860 points)
    Did you try the script with longer delays, for example 1 sec. instead of 0.5 sec.?
  • 7. Re: Make smart playlists in iTunes?
    Pierre L. Level 4 Level 4 (3,860 points)
    All I can say is that the script seems to work OK on my computer, except when the iTunes window isn't already open. So I first added a few lines to the beginning of the script to handle that situation. Most importantly for you, I also added five 1 sec. delays, in order that you can check if the script works properly, though very slowly, with all of them present. If so, you could then try to remove them one by one, or choose shorter delays for some of them, until you find the best solution. I hope it can help.

    tell application "iTunes" to activate
    tell application "System Events" to tell process "iTunes"
    set menuItem to menu item "iTunes" of menu 1 of menu bar item "Window" of menu bar 1
    if value of attribute "AXMenuItemMarkChar" of menuItem is "" then
    keystroke "1" using {option down, command down} -- open the iTunes window
    delay 1 -- for testing
    end if
    keystroke "n" using {option down, command down} -- New Smart Playlist…
    tell scroll area 1 of group 1 of window 1
    delay 1 -- for testing
    click pop up button 2
    delay 1 -- for testing
    click menu item "Composer" of menu 1 of pop up button 2
    delay 1
    click pop up button 1
    delay 1 -- for testing
    click menu item "is" of menu 1 of pop up button 1
    delay 1 -- for testing
    keystroke "Beethoven" & return
    end tell
    delay 1
    keystroke "New smart playlist" & return -- to rename the playlist
    end tell

    My experience is that GUI scripting works well once adapted to the computer your are using.
  • 8. Re: Make smart playlists in iTunes?
    dw32 Level 1 Level 1 (0 points)
    Hello Pierre, I'm glad to be able to tell you it's working beautifully. I wanted "Composer Contains", so this is what is now working:

    (code which gets "comp" from existing unsmart PLs)
    .....
    tell application "System Events" to tell process "iTunes"
    keystroke "n" using {option down, command down} -- invoke new smart PL dialog
    tell scroll area 1 of group 1 of window 1
    delay 0.5
    click pop up button 2
    delay 0.5
    click menu item "Composer" of menu 1 of pop up button 2
    delay 0.5
    keystroke comp --insert my composer
    delay 1
    keystroke return --finish Composer which finishes SPL pop up
    delay 1
    keystroke return --finish SPL name in sidebar
    end tell

    The GUI is a bit hard to predict, for example since the cursor was already in the 3rd field, I could just use keystroke to enter the string, even though I'd used your "click pop up button 2".

    Obviously I'll have to take out the liberal sprinkling of Delays, but at the moment I'm just sighing happily. Thank you VERY much sir!
  • 9. Re: Make smart playlists in iTunes?
    Pierre L. Level 4 Level 4 (3,860 points)
    Wonderful! Thanks for the feedback.
  • 10. Re: Make smart playlists in iTunes?
    dw32 Level 1 Level 1 (0 points)
    In case anyone else wants to do what I did, here's the final form of my scpt. But it has a couple of bug workarounds, so it may not work beyond iTunes 8.1.1 (6). Good luck!

    (* Make smart PLs from unsmart Composer-based *)

    (* Doesn't test if unsmart PL name actually IS a composer contained in Music PL. Would take too long. *)


    (* - - - - - - - >>> !!!! TURN OFF AirPort !!!!! <<<- - - - - - - else you can get killed by e.g. Software Update *)


    tell application "iTunes"
    delay 3 -- Delay 3secs to get a chance to click the iT window after launching the scpt.
    set allPLs to (get every user playlist)
    repeat with thisPL in allPLs
    if name of thisPL is not "Music" then
    (* extract 1st word (surname) of unsmart PL name... *)
    if smart of thisPL is false then
    set comp to name of thisPL
    set comp to words of comp -- ludicrous - takes 4 operations to get word out of Unicode text
    set comp to (comp as string)
    ignoring hyphens
    set AppleScript's text item delimiters to " " --necessary for words
    set comp to word 1 of comp -- got 1st word at last
    end ignoring

    tell application "System Events" to tell process "iTunes"
    delay 0.5
    keystroke "n" using {option down, command down} ---- <---- <---- create New smart PL

    (* change Artist to Composer *)
    delay 0.5
    tell scroll area 1 of group 1 of window 1
    delay 0.5
    click pop up button 2
    delay 0.5
    click menu item "Composer" of menu 1 of pop up button 2 -- Change Artist to Composer
    delay 0.5
    keystroke comp & quote -- the quote stops iT auto-complete filling in the whole name etc. See ASCII below
    (*Cursor is still in TF1 of SA1 of G1 of W1, so keystroke comp works almost regardless of where we are*)
    end tell
    (* Change Random to Album *)
    delay 0.5
    click pop up button 1 of window 1
    delay 0.5
    click menu item "Album" of menu 1 of pop up button 1 of window 1
    (* as soon as you change above menu item, Checkbox 3 (to left of 25) gets set. Bug?
    Solution: Click Checkbox 3 of w1 later (below)
    It seems NOTHING can change the 25 to e.g. 999 (another bug?) *)

    (* Now we can complete the Composer *)
    delay 0.5
    keystroke (ASCII character 8) -- rubout the quot in the PL name with a Backspace character

    (* workaround the Limit to 25 checkbox bug *)
    delay 0.5
    click checkbox 3 of window 1 -- uncheck the "limit to 25"
    (* The above WILL DO THE REVERSE OF WHAT I WANT if they ever fix the bug above *)

    (* finish comp and close the New SPL window *)
    delay 0.5
    keystroke return

    (* Now we need a Return to finish the new SPL Name in the sidebar *)
    delay 1
    keystroke return

    end tell
    end if
    end if
    end repeat
    end tell

    beep 3 -- Say we're all done
    (*
    tell application "Script Editor" -- TEST
    activate
    end tell*)
    -------

    As I said, Good luck!

    David Whitford