user1159454
user1159454

Reputation: 3317

In applescript how can I compare two lists that aren't the same length?

Ok so I feel this is simple but I'm overlooking a long of things and after 2 hours of trying it on my own I'm asking for help while I mull it over more.

In applescript I'm trying to compare 2 lists that are ordered, but not the same length. What I'm trying to do is take a list of all total files in a folder, sift each file into either MP4 or MTS, and have a list of each, then compare the two exact positions to find out which MTS files don't have a MP4 counterpart, then add the MTS filename to a new list to later be selected in the finder. I'm using a placeholder for the totalFiles variable until it's ready to use on real world. In the placeholder, the result should be only "6534-Seasons.MTS" because there is no MP4 to go with it. Instead, it crashes because it can't search that far in the MP4 list because there's not as many as the MTS. There will never be an MP4 without an MTS in this situation.

I've gotten as far as creating two separate lists, and comparing them a little bit before finding that my repeat loop breaks because the number of MP4s will most of the time be lower than the number of MTS files. Here's what I have

--Look for files that don't have .MP4, find their MTS counterpart.


set totalFiles to {"41 - The words.MTS", "41 - The words.MP4", "445 - Life on the rails.MTS", "445 - Life on the rails.mp4", "6354-Seasons.MTS"} -- List of all files in folder


set clickList to {}

--log totalFiles
--log vidlist


on siftfiles(totalFiles)
    --Sift through everything and find mp4s, then add to a list. Do the same for MTS but add separately.
    set MTSlist to {}
    set MP4list to {}
    repeat with vidname in totalFiles
        if (vidname contains ".MP4") or (vidname contains ".mp4") then
            set end of MP4list to vidname as string
        end if
        if vidname contains ".MTS" then
            set end of MTSlist to vidname as string
        end if
    end repeat
    set returnlist to {MP4list, MTSlist}
    return returnlist
end siftfiles


set MP4slist to item 1 of siftfiles(totalFiles)
set MTSlist to item 2 of siftfiles(totalFiles)

--siftfiles(totalFiles)
--MP4slist







--Compare the two lists
set clickList to {}
set i to 1
repeat with thename in MTSlist
    set MP4name to characters 1 thru -5 of item i of MP4slist as string
    set MTSname to characters 1 thru -5 of item i of MTSlist as string
    if MP4name is not MTSname then
        set end of clickList to (thename as string)
    end if
    set i to i + 1
end repeat

clickList

Thanks.

I've also revamped my approach at this problem a few times, so perhaps there's a better way than comparing them?

Upvotes: 0

Views: 570

Answers (2)

CRGreen
CRGreen

Reputation: 3444

How's this? Hope I understand what you're looking for. I just sort of did it the way I would do it, rather than try to tweak your code. Hope that's ok. I utilize the Finder's ability to use "whose clauses" to suck up the file names with the right extensions first (if this is a huge list, it may take a moment), then loop through. I prefer having a list of file names rather than full Finder references (or AS aliases for that matter), then I can rebuild file path strings if I need to.

set ff to choose folder
tell application "Finder"
    set mpfours to name of files of ff whose name ends with ".mp4"
    set mtses to name of files of ff whose name ends with ".mov"
end tell

--sorry, had to remove temp lines that were just for me

set orphanMTSes to {}

repeat with thisOne in mtses
    set choppedMTS to (text 1 thru -4 of thisOne) --includes dot
    if ((choppedMTS & "mp4") is not in mpfours) then set orphanMTSes to (orphanMTSes & thisOne)
end repeat
orphanMTSes

[edit] Here's a pretty efficient way of taking that list of orphans and doing a selection in the Finder (since the list is just file names; I can build an alias list and use that):

set selectedList to {}

repeat with f in orphanedMTSes
    set selectedList to (selectedList & (alias ((ff as text) & f)))
end repeat

tell application "Finder"
    select selectedList
end tell

Upvotes: 1

jweaks
jweaks

Reputation: 3792

This will accomplish what you want. I cleaned up some messy parts, deleted some unnecessary parts, but the main swith I made was to coerce the MP4 list into a string so you can use a simply "contains". Let me know if you need anything more out of this that I missed.

--Look for files that don't have .MP4, find their MTS counterpart.

set totalFiles to {"41 - The words.MTS", "41 - The words.MP4", "445 - Life on the rails.MTS", "445 - Life on the rails.mp4", "6354-Seasons.MTS"} -- List of all files in folder

on siftfiles(totalFiles)
    --Sift through everything and find mp4s, then add to a list. Do the same for MTS but add separately.
    set MTSlist to {}
    set MP4list to {}
    repeat with vidname in totalFiles
        if (vidname contains ".mp4") then
            set end of MP4list to vidname as string
        else if vidname contains ".mts" then
            set end of MTSlist to vidname as string
        end if
    end repeat
    set returnlist to {MP4list, MTSlist}
    return returnlist
end siftfiles

set {MP4list, MTSlist} to siftfiles(totalFiles)

-- turn the MP4 list into a string
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
set MP4string to "|" & (MP4list as string) & "|"
set AppleScript's text item delimiters to otid

--Compare the two lists
set clickList to {}
repeat with thename in MTSlist
    set trimmedname to (text 1 thru -5 of thename)
    if ("|" & trimmedname & ".") is not in MP4string then
        set end of clickList to (trimmedname as string)
    end if
end repeat

return clickList

Upvotes: 0

Related Questions