2017-03-01 35 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 
相關問題