Search code examples
xcodeapplescriptapplescript-objc

setStringValue with AppleScript List


I’m using this code to add each selected track in iTunes to a list in AppleScript Obj-C:

    tell application "iTunes"
        set these_titles to {}
        if selection is not {} then
            set mySelection to selection
            repeat with aTrack in mySelection
                if class of aTrack is file track then
                    set end of these_titles to ((name of aTrack) as string)
                end if
            end repeat
        end if
    end tell

I have this line to show the output in a text box in the GUI:

    trackTable's setStringValue_(these_titles)

But it comes out like this:

Output of setStringValue

Is there any way I can get each list item on one line, sans the quotation marks and brackets?


Solution

  • That is the proper output for a list. If you want it coerced to a string in a particular format, you need to do that explicitly. Simply change the delimiter used for lists and then coerce the variable into a string, which will place that delimiter as text between each list item. So, add this code to the end of your code:

    set oldTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to ", "
    set these_titles to these_titles as string
    set AppleScript's text item delimiters to oldTID
    

    Change the delimiter to whatever you want, in place of the comma and space.