Search code examples
applescript

AppleScript choosing from list object type of selection


It's been a long time since I touched AppleScript and so this might be me just not doing things right. I have a list that I create by collecting the second to the last text item in a path. The path would look something like this:

HD:Users:xxxxxxxx:Library:Developer:Xcode:Archives:2017-06-14:focus 6-14-17, 8.03 AM.xcarchive:"

and so the list is populated with strings that look something like this:

focus 6-14-17, 8.03 AM.xcarchive

I then present to the user and ask them to select an archive. I have another list that contains all the archive paths. What I am looking to do is take the selected archive, find out it's index in the archive name list and then pull the path from the archive path list. Pretty straight forward, at least I thought. The relevant part of the code looks like this:

set myFile to choose from list archiveFileNames with prompt "Select An Archive"
    -->return class of myFile
    
    repeat with i from 1 to count of every item of archiveFileNames
        
        if (myFile is equal to (item i of archiveFileNames as string)) then
            log myFile & "::" & item i of archiveFileNames & "::" & i
        end if
    end repeat

I never got a match, even though if I just repeat through the archive name list and use a log statement I can get results that I am looking for:

log myFile & "::" & item i of archiveFileNames & "::" & i

(See last result for the match)

(*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-1-17, 1.40 PM.xcarchive, ::, 1*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-1-17, 10.48 AM.xcarchive, ::, 2*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-1-17, 11.04 AM.xcarchive, ::, 3*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-1-17, 11.24 AM.xcarchive, ::, 4*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-1-17, 9.37 AM.xcarchive, ::, 5*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-3-17, 2.01 PM.xcarchive, ::, 6*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-3-17, 2.02 PM.xcarchive, ::, 7*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-5-17, 2.51 PM.xcarchive, ::, 8*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-12-17, 8.49 AM.xcarchive, ::, 9*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-12-17, 8.53 AM.xcarchive, ::, 10*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 5-16-17, 11.43 AM.xcarchive, ::, 11*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-5-17, 1.42 PM.xcarchive, ::, 12*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-6-17, 10.37 AM.xcarchive, ::, 13*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-6-17, 4.09 PM.xcarchive, ::, 14*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-13-17, 4.01 PM.xcarchive, ::, 15*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-14-17, 11.36 AM.xcarchive, ::, 16*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-14-17, 8.00 AM.xcarchive, ::, 17*)
    (*focus 6-14-17, 8.03 AM.xcarchive, ::, focus 6-14-17, 8.03 AM.xcarchive, ::, 18*)

So I thought this must be a class/type issue where there is no equality because I am comparing apples and oranges. When I run it and have -->return class of myFile uncommented, I get that the class of my selection from the list is a list.

Obviously I am doing something wrong. I was expecting the type to be string, maybe text, not list.

Edit

OK, so if I change the conditional to this:

if (myFile is equal to (item i of archiveFileNames as **list**)) then

I get the expected results. But I am still confused as to why the type of an object selected from a list of string is a list.


Solution

  • The issue occurs because choose from list returns always a list of objects (or boolean false if the user presses Cancel).

    You have to flatten the list.

    set myFile to choose from list archiveFileNames with prompt "Select An Archive"
    
    if myFile is false then return
    set myFile to item 1 of myFile
    repeat with i from 1 to count of every item of archiveFileNames
        set currentItem to item i of archiveFileNames
        if myFile = currentItem then
            log myFile & "::" & currentItem & "::" & i
        end if
    end repeat