在applescript中,如何比较长度不同的两个列表?

时间:2017-03-01 01:26:49

标签: arrays list applescript

好的,所以我觉得这很简单,但是我忽略了很多事情,经过2个小时的尝试后,我正在寻求帮助,而我正在考虑更多。

在AppleScript中我试图比较2个有序的列表,但长度不一样。我要做的是获取文件夹中所有文件的列表,将每个文件筛选到MP4或MTS中,并列出每个文件,然后比较两个确切位置以找出哪些MTS文件不有一个MP4对应物,然后将MTS文件名添加到新列表,以便稍后在查找器中选择。我正在使用totalFiles变量的占位符,直到它可以在现实世界中使用。在占位符中,结果应该只是“6534-Seasons.MTS”,因为没有MP4可以使用它。相反,它崩溃是因为它无法在MP4列表中搜索那么远,因为它没有MTS那么多。在这种情况下,没有MTS就永远不会有MP4。

我已经创建了两个单独的列表,并在发现我的重复循环中断之前稍微比较它们,因为MP4的数量大部分时间都会低于MTS文件的数量。这就是我所拥有的

--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

感谢。

我也曾多次修改过这个问题的方法,所以也许比比较它们有更好的方法?

2 个答案:

答案 0 :(得分:1)

这是怎么回事?希望我明白你在寻找什么。 我只是按照我的方式做到了,而不是尝试调整你的代码。希望没关系。 我利用Finder的能力使用“其子句”首先使用正确的扩展名来填充文件名(如果这是一个巨大的列表,可能需要一些时间),然后循环。我更喜欢有一个文件名列表而不是完整的Finder引用(或AS的别名),如果需要,我可以重建文件路径字符串。

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] 这是获取孤立列表并在Finder中进行选择的一种非常有效的方法(因为列表只是文件名;我可以构建一个别名列表并使用它) :

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

答案 1 :(得分:0)

这将完成你想要的。我清理了一些杂乱的部分,删除了一些不必要的部分,但我制作的主要内容是将MP4列表强制转换成字符串,这样你就可以使用简单的“包含”。如果你需要更多我想念的东西,请告诉我。

- 查找没有.MP4的文件,找到他们的MTS对应文件。

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