2017-03-01 8 views
0

私はこれがシンプルだと感じますが、私は長いことを見落としています。それを自分で試して2時間後に、私はそれ以上のことを検討しながら助けを求めています。AppleScriptで同じ長さではない2つのリストを比較するにはどうすればよいですか?

私は2つのリストを比較しようとしていますが、同じ長さではありません。私がしようとしているのは、フォルダ内の総合計ファイルのリストを取得し、各ファイルをMP4またはMTSのいずれかに移動して、それぞれのリストを取得し、2つの正確な位置を比較して、 MP4の対応がある場合は、新しいリストにMTSファイル名を追加して、後でファインダーで選択します。現実世界で使用できるようになるまで、totalFiles変数のプレースホルダを使用しています。プレースホルダでは、MP4がないため、結果は「6534-Seasons.MTS」になります。代わりに、MTSほど多くないのでMP4リストのそれを検索できないため、クラッシュします。このような状況では、MTSなしのMP4は存在しません。

私はMP4の数がほとんどMTSファイルの数よりも少なくなるため、2つの別々のリストを作成し、それらを少し比較してから私のリピートループが壊れていることを発見しました。ここにあるものは

--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あなたは1分未満で私を倒す! :-) – CRGreen

1

どういうことですか?私はあなたが探しているものを理解することを願っています。 私はあなたのコードを調整しようとするのではなく、私がやるようにしました。希望はOKです。 私は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 
関連する問題