2017-03-01 63 views
0

好,所以我觉得这很简单,但我忽略了很长的一段时间,经过2个小时的尝试,我在寻求帮助的同时,仔细考虑了更多。在苹果脚本中,我如何比较两个不同长度的列表?

在applescript中,我试图比较两个排序的列表,但长度不一样。我想要做的是获取文件夹中所有文件的列表,将每个文件筛选到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 

谢谢。

我也改变了我在这个问题的方法几次,所以也许有比比较它们更好的方法吗?

回答

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

dammit @jweaks你击败了我不到一分钟! :-) – CRGreen

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 

[编辑]下面是采取孤儿的列表并且在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 
相关问题