0

The script for batch exporting playlists from Apple Music app is throwing up the execution error:

"The variable all_ps is not defined. (-2753)"

This is the portion of the code in question:

(-- GET ALL PLAYLISTS FROM APPLE MUSIC)

        try
            set all_specialps to (get name of every user playlist whose special kind is not none)
            set all_userps to (get name of every user playlist whose smart is false and special kind is none)
            set all_smartps to (get name of every user playlist whose smart is true and special kind is none)
            
            set delim to "--------------------------------------------------"
            set delim_specialpl to "---------------- Special Playlists: ----------------"
            set delim_userpl to "------------------ User Playlists: -----------------"
            set delim_smartpl to "---------------- Smart Playlists: -----------------"
            set all_ps to "{}"
            if ((length of all_specialps) > 0) then
                set the end of all_ps to delim
                set the end of all_ps to delim_specialpl
                repeat with ps in all_specialps
                    set the end of all_ps to ps
                end repeat
            end if
            if ((length of all_userps) > 0) then
                set the end of all_ps to delim
                set the end of all_ps to delim_userpl
                repeat with ps in all_userps
                    set the end of all_ps to ps
                end repeat
            end if
            if ((length of all_smartps) > 0) then
                set the end of all_ps to delim
                set the end of all_ps to delim_smartpl
                repeat with ps in all_smartps
                    set the end of all_ps to ps
                end repeat
            end if
        end try

I cannot see what is not defined about all_ps from this, total noob, any help v gratefully received

Changed "set all_ps to "---------------All Playlists:--------------" to no effect, same issue thrown up.

  • The `all-ps` variable is being treated as a list, but is declared as a string - is that a typo? Note that you need to include the `on error` part of a `try` statement to handle any error, otherwise the statement will exit without any indication. – red_menace Feb 06 '23 at 04:47
  • Thanks so much for coming back @red_menace - I didn't write this and it is only part of the entire script - originally adapted from an iTunes script to work with Apple Music and I cannot reach the original coder on GitHub. Not sure how to include "on error" part of a "try" statement - know any resources that cover this basic kind of stuff I can learn from? – KirklandStackOF Feb 07 '23 at 06:21

2 Answers2

0

In a try statement, any error will jump to a separate set of statements, skipping whatever statements follow the error. There will be no indication unless the on error portion of the try statement is included to handle the error (even if it is just to log that there was an error).

In this case, the error is from trying to get the name of a list of playlists, since lists don't have a name property. This skips everything else in the try statement, including where the all_ps variable is declared. Once that is fixed, the next error(s) would be from trying to set the end of a string to something.

Strings are immutable, so you can’t add stuff to them like that - a new string needs to be built from the desired parts using the concatenation operator (&). Your posted script can still be used with all_ps set to a list (adding to the end of a list is OK), which can be converted later into a string, for example:

tell application "Music" to try
   set delim to "--------------------------------------------------"
   set all_ps to {}
   
   set delim_specialpl to "---------------- Special Playlists: ----------------"
   set all_specialps to get every user playlist whose special kind is not none
   if all_specialps is not {} then
      set the end of all_ps to delim
      set the end of all_ps to delim_specialpl
      repeat with ps in all_specialps
         set the end of all_ps to name of ps
      end repeat
   end if
   
   set delim_userpl to "------------------ User Playlists: -----------------"
   set all_userps to get every user playlist whose smart is false and special kind is none
   if all_userps is not {} then
      set the end of all_ps to delim
      set the end of all_ps to delim_userpl
      repeat with ps in all_userps
         set the end of all_ps to name of ps
      end repeat
   end if
   
   set delim_smartpl to "---------------- Smart Playlists: -----------------"
   set all_smartps to get every user playlist whose smart is true and special kind is none
   if all_smartps is not {} then
      set the end of all_ps to delim
      set the end of all_ps to delim_smartpl
      repeat with ps in all_smartps
         set the end of all_ps to name of ps
      end repeat
   end if
on error errorMessage -- oops, handle the error
   activate me
   display alert "Script Error" message errorMessage -- or log, quit, etc
end try

if all_ps is not {} then
   set tempTID to AppleScript's text item delimiters -- stash any existing delimiters
   set AppleScript's text item delimiters to return -- set a delimiting character
   set all_ps to all_ps as text -- join list items using the current text item delimiter
   set AppleScript's text item delimiters to tempTID -- restore the original delimiters
end if

return all_ps -- or whatever
red_menace
  • 3,162
  • 2
  • 10
  • 18
  • Thanks so much for coming back @red_menace - have replaced the slug in my OP with your improved code and the Script now executes without error BUT does not open a Script window with options for playlist exporting per the script's intended function. I don't know that I fully understand what the changes you kindly provided have done so I'm not sure how to pick further to get this working. Admittedly there's a substantial amount of more code beyond the original portion I shared. – KirklandStackOF Feb 12 '23 at 09:15
  • @KirklandStackOF - note that my example is self contained and returns the string of playlists, so if it is in the middle of other stuff, you would need to remove that statement. – red_menace Feb 12 '23 at 16:27
  • FWIW finally got this script working - thanks for everyone's suggestions here. Best I can tell errors were caused by poor code syntax & lack of a functional error clause that I added in - thanks to ChatGPT's beady eyes. Original code included "set the end of", functional code has changed all those to "set end of". Cheers all! – KirklandStackOF May 25 '23 at 05:12
0

Do not confuse lists and strings. Having received the necessary data from the application, it is better to finish the tell-block. Try-block is not needed. Always try not to use repeat loops.

tell application "Music" to tell user playlists
    set all_specialps to name whose special kind is not none
    set all_userps to name whose smart is false and special kind is none
    set all_smartps to name whose smart is true and special kind is none
end tell

set delim to "----------------------------------------------------"
set delim_specialpl to "---------------- Special Playlists: ----------------"
set delim_userpl to "------------------ User Playlists: -----------------"
set delim_smartpl to "---------------- Smart Playlists: -----------------"

set all_ps to {}
if all_specialps is not {} then set all_ps to all_ps & {delim, delim_specialpl} & all_specialps
if all_userps is not {} then set all_ps to all_ps & {delim, delim_userpl} & all_userps
if all_smartps is not {} then set all_ps to all_ps & {delim, delim_smartpl} & all_smartps

set all_ps to my convertList:all_ps toTEXTbyDelimiter:linefeed


on convertList:theList toTEXTbyDelimiter:theDelimiter
    set ATID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to theDelimiter
    set theText to theList as text
    set AppleScript's text item delimiters to ATID
    return theText & theDelimiter
end convertList:toTEXTbyDelimiter:
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8