イメージのみが含まれるフォルダを指定のフォルダに移動

イメージのみが含まれるフォルダを指定のフォルダに移動するスクリプトです。
-- http://piyocast.com/as/archives/3375

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSURLTypeIdentifierKey : a reference to current application's NSURLTypeIdentifierKey
property |NSURL| : a reference to current application's |NSURL|
property NSPredicate : a reference to current application's NSPredicate
property NSArray : a reference to current application's NSArray


set targFolder to "/Users/me/Desktop/A" as POSIX file as alias -- 処理をするフォルダ
set destFolder to "/Users/me/Desktop/E" -- 移動先のフォルダ

tell application "Finder"
set theseFolders to every folder of targFolder -- 処理をするフォルダ内の全てのフォルダ
if theseFolders is {} then return

set aList to {}
repeat with i in theseFolders
set aFol to (contents of i) as alias
set theseFiles to (every file of aFol) as alias list
set filRes to filterAliasListByUTI(theseFiles, "public.image") of me -- フォルダに含まれるイメージを取得
if (length of filRes) > 0 then
-- すべてのアイテムとイメージの数を比較->数が同じなら含まれるものがイメージのみと判断
if (length of theseFiles) = (length of filRes) then
set the end of aList to (quoted form of (POSIX path of aFol)) & " "
end if
end if
end repeat
end tell

if aList is {} then return

do shell script "mv -n " & (aList as string) & " " & destFolder



--Alias listから指定UTIに含まれるものをPOSIX pathのリストで返す
on filterAliasListByUTI(aList, targUTI)
set newList to {}
repeat with i in aList
set j to POSIX path of i
set tmpUTI to my retUTIfromPath(j)
set utiRes to my filterUTIList({tmpUTI}, targUTI)
if utiRes is not equal to {} then
set the end of newList to j
end if
end repeat
return newList
end filterAliasListByUTI


--指定のPOSIX pathのファイルのUTIを求める
on retUTIfromPath(aPOSIXPath)
set aURL to |NSURL|'s fileURLWithPath:aPOSIXPath
set {theResult, theValue} to aURL's getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value)

if theResult = true then
return theValue as string
else
return theResult
end if
end retUTIfromPath


--UTIリストが指定UTIに含まれているかどうか演算を行う
on filterUTIList(aUTIList, aUTIstr)
set anArray to NSArray's arrayWithArray:aUTIList
set aPred to NSPredicate's predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr)
set bRes to (anArray's filteredArrayUsingPredicate:aPred) as list
return bRes
end filterUTIList

更新履歴

inserted by FC2 system