指定したフォルダ内のファイルを抽出し、抽出先のフォルダ内にすべてコピーします。
同名のファイルが複数存在した場合は、処理速度を重視して(簡易的ではありますが)サイズ比較で別ファイルかどうかを判定。
別ファイルだった場合は、連番リネームをして「名称重複」というフォルダ内にコピーします。
script ScptObj
property parent : parent of me
property _contents : {}
end script
on run
with timeout of (60 * 120) seconds
try
activate
set target_folder to (choose folder with prompt "抽出元フォルダを選択してください。") as alias
set collect_folder to (choose folder with prompt "抽出先フォルダを選択してください。(空フォルダを選択してください。)") as alias
tell application "Finder"
set entire_items to items of collect_folder
if 0 < (count of entire_items) then error "抽出先フォルダには空フォルダを選択してください。"
set (_contents of ScptObj) to (files of entire contents of target_folder whose class is document file) as alias list
if (count of (_contents of ScptObj)) is 0 then error "抽出元にはファイルがありません。"
repeat with target_file in (_contents of ScptObj)
set collect_path to ""
set target_name to (name of target_file) as Unicode text
(*同名ファイルの検査*)
if not (exists file target_name in collect_folder) then
set collect_path to (POSIX path of collect_folder) & target_name
else if (size of file target_file) is not (size of (file target_name in collect_folder)) then
if not (exists folder "名称重複" in collect_folder) then
set duplicate_folder to (make folder at collect_folder with properties {name:"名称重複"}) as alias
else
set duplicate_folder to (folder "名称重複" of collect_folder) as alias
end if
(*ファイル名から拡張子を分離*)
set target_ext to (name extension of target_file) as Unicode text
if target_ext is not "" then
set ext_num to (count of target_ext) + 2
set target_txt to text 1 thru -ext_num of target_name
set target_ext to ("." & target_ext) as Unicode text
else
set target_txt to target_name
end if
(*コピー済みの同名ファイル検査*)
set base_name to (target_txt & "_") as Unicode text
set overlap_files to (files of duplicate_folder whose name contains base_name) as alias list
set overlap_num to (count of overlap_files) + 1
repeat with overlap_file in overlap_files
if (size of target_file) is (size of overlap_file) then
set overlap_num to 0
exit repeat
end if
end repeat
if 0 < overlap_num then set collect_path to (POSIX path of duplicate_folder) & base_name & overlap_num & target_ext
end if
if collect_path is not "" then tell me to do shell script "cp -fRP " & quoted form of POSIX path of target_file & " " & quoted form of collect_path
end repeat
delay 0.1
set dup_bool to (exists folder "名称重複" in collect_folder)
end tell
activate
if dup_bool then
display dialog "処理完了しましたが、同名の別ファイルが存在します。抽出先の名称重複フォルダ内を確認してください。" buttons {"OK"} default button 1
else
display dialog "処理完了しました。" buttons {"OK"} default button 1
end if
on error err_txt
if err_txt is "ユーザによってキャンセルされました。" then
return
else
activate
display alert "処理中にエラーが発生しました。" message err_txt as warning
return
end if
end try
end timeout
end run