« [stringWithFormat]NSDictionaryをテキストに | トップページ | [Diagnostics Mode]ハードウェア診断モードで起動するmacOS »

[Music]重複音楽ファイルをゴミ箱に入れる

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

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

property refMe : a reference to current application

set objFileManager to refMe's NSFileManager's defaultManager()


##############################################
## ダイアログ関連
##############################################
####デフォルトロケーション
set ocidFilePathURLArray to (objFileManager's URLsForDirectory:(refMe's NSMusicDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidFilePathURL to item 1 of ocidFilePathURLArray
set ocidFilePathURL to ocidFilePathURL's URLByAppendingPathComponent:"Music/Media.localized/Music"

set aliasDefaultLocation to ocidFilePathURL as alias
####プロンプトテキスト
set strPromptText to "フォルダ選んでください" as text

try
    set aliasFolderPath to (choose folder strPromptText with prompt strPromptText default location aliasDefaultLocation without multiple selections allowed, invisibles and showing package contents) as alias
on error
    log "エラーしました"
    return
end try

##############################################
##入力フォルダ パス
##############################################
####パス
set strFolderPath to POSIX path of aliasFolderPath as text
####パスをNSString
set ocidFolderPath to refMe's NSString's stringWithString:strFolderPath
####ドキュメントのパスをNSURLに
set ocidFolderPathURL to refMe's NSURL's fileURLWithPath:ocidFolderPath

##############################################
##準備
##############################################
###enumeratorAtURL用のBoolean用
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
set ocidTrue to (refMe's NSNumber's numberWithBool:true)
###enumeratorAtURLL格納用のレコード
set ocidEmuDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###enumeratorAtURLL格納するリスト
set ocidEmuFileURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###ファイルURLのみを格納するリスト
set ocidFilePathURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
####
set ocidFilePathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
####
set ocidSortedArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0



##############################################
##ディレクトリのコンテツを収集
##############################################
###収集する付随プロパティ
set ocidPropertiesForKeys to {refMe's NSURLIsRegularFileKey}
####ディレクトリのコンテツを収集
set ocidEmuDict to (objFileManager's enumeratorAtURL:ocidFolderPathURL includingPropertiesForKeys:ocidPropertiesForKeys options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) errorHandler:(reference))
###戻り値用のリストに格納
set ocidEmuFileURLArray to ocidEmuDict's allObjects()


##############################################
##『ファイル』だけ取り出したリストにする
##############################################

####enumeratorAtURLのかずだけ繰り返し
repeat with itemEmuFileURL in ocidEmuFileURLArray
    ####URLをforKeyで取り出し
    set listResult to (itemEmuFileURL's getResourceValue:(reference) forKey:(refMe's NSURLIsRegularFileKey) |error|:(reference))
    ###リストからNSURLIsRegularFileKeyのBOOLを取り出し
    set boolIsRegularFileKey to item 2 of listResult
    ####ファイルのみを(ディレクトリやリンボリックリンクは含まない)
    if boolIsRegularFileKey is ocidTrue then
        ####リストにする
        (ocidFilePathURLArray's addObject:itemEmuFileURL)
    end if
end repeat

###解放
set ocidEmuFileURLArray to ""
set ocidEmuDict to ""
(*
ここで『ocidFilePathURLArray』に全ファイルのリストが入る
*)
##############################################
##ソート
##############################################
###NSURLのリストを一旦パスに変更
set ocidFilePathArray to (ocidFilePathURLArray's valueForKeyPath:"path")
###パスでソートしてから
set ocidSortedArray to (ocidFilePathArray's sortedArrayUsingSelector:"compare:")
###逆順にする
ocidSortedArray's reverseObjectEnumerator()'s allObjects()


##############################################
##本処理
##############################################
set numCntArrayItem to (count of ocidSortedArray) as integer
set numCntItem to 0 as integer


####Arrayのアイテム数だけ繰り返し
repeat (numCntArrayItem - 1) times
    ####################################
    ###最初のアイテム
    set ocidArrayItem to ocidSortedArray's objectAtIndex:numCntItem
    set ocidArrayItemURL to refMe's NSURL's fileURLWithPath:ocidArrayItem
    
    ####################################
    ###次の比較アイテム
    set ocidNextArrayItem to ocidSortedArray's objectAtIndex:(numCntItem + 1)
    set ocidNextArrayItemURL to refMe's NSURL's fileURLWithPath:ocidNextArrayItem
    
    ####################################
    ###最初のアイテム の『タイトル』    
    set strTitle to doGetTitle(ocidArrayItemURL)
    ####################################
    ###次の比較アイテムのタイトル
    set strNextTitle to doGetTitle(ocidNextArrayItemURL)
    
    if strTitle is strNextTitle then
        log ocidNextArrayItemURL as text
        log strTitle
        log strNextTitle
        log "タイトルが同じです"
        #####ラベルの色を定義
        set ocidTagNameString to refMe's NSString's stringWithString:"イエロー"
        set ocidTagNameArray to refMe's NSMutableArray's alloc()'s initWithObject:ocidTagNameString
        #####ラベルをセットする
        set listSetDone to ocidArrayItemURL's setResourceValue:ocidTagNameArray forKey:(refMe's NSURLTagNamesKey) |error|:(reference)
        set listSetDone to ocidNextArrayItemURL's setResourceValue:ocidTagNameArray forKey:(refMe's NSURLTagNamesKey) |error|:(reference)
        ####################################
        ###タイトルが同じ場合 ファイルサイズを調べる
        ####################################
        ###最初のアイテム の『タイトル』    
        set strFileSize to doGetFileSize(ocidArrayItemURL)
        ####################################
        ###次の比較アイテムのタイトル
        set strNextFileSize to doGetFileSize(ocidNextArrayItemURL)
        
        ####ファイルサイズの誤差範囲
        set strFileSize to (round of (strFileSize / 1000) rounding down)
        set strNextFileSize to (round of (strNextFileSize / 1000) rounding down)
        log strFileSize
        log strNextFileSize
        ###タイトルが同じ 同じディレクトリにある=同じアルバム(じゃない事もあるけど) でサイズも同じなら重複ファイルと判断
        if strFileSize is strNextFileSize then
            
            ####ゴミ箱に入れる
            set listDone to objFileManager's trashItemAtURL:ocidArrayItemURL resultingItemURL:(missing value) |error|:(reference)
            
        end if
    end if
    
    #####カウントアップ
    set numCntItem to numCntItem + 1 as integer
    
end repeat



###########################################
### ファイルサイズを読み取るサブ NSURL
###########################################

to doGetFileSize(argURL)
    ####処理をNSURLで行うためのパスの整形
    try
        set strClassName to className() of argURL as text
    on error
        set strClassName to class of argURL
    end try
    if strClassName is "__NSCFString" then
        set ocidArgFilePath to argURL's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
    else if strClassName is "NSPathStore2" then
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:argURL
    else if strClassName is "NSURL" then
        set ocidArgFilePathURL to argURL
    else if strClassName is alias then
        set strArgFilePath to POSIX path of argURL
        set ocidArgFilePathStr to refNSString's stringWithString:strArgFilePath
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
    else if strClassName is text then
        set ocidArgFilePathStr to refNSString's stringWithString:argURL
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
    end if
    ###ファイルサイズ取得
    set listResults to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileSizeKey) |error|:(reference)
    set strFileSize to (item 2 of listResults) as text
    ###値を戻す
    return (strFileSize)
end doGetFileSize
###########################################
### タイトルを読み取るサブ
###########################################

to doGetTitle(argURL)
    ####処理をNSURLで行うためのパスの整形
    try
        set strClassName to className() of argURL as text
    on error
        set strClassName to class of argURL as text
    end try
    if strClassName is "alias" then
        set strArgFilePath to POSIX path of argURL as text
        set ocidArgFilePathStr to refMe's NSString's stringWithString:strArgFilePath
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refMe's NSURL's fileURLWithPath:ocidArgFilePath
    else if strClassName is "text" then
        set ocidArgFilePathStr to refMe's NSString's stringWithString:strArgFilePath
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refMe's NSURL's fileURLWithPath:ocidArgFilePath
    else if strClassName is "NSURL" then
        set ocidArgFilePathURL to argURL
    else if strClassName is "NSPathStore2" then
        set ocidArgFilePathURL to refMe's NSURL's fileURLWithPath:argURL
    else if strClassName is "__NSCFString" then
        set ocidArgFilePathStr to refMe's NSString's stringWithString:argURL
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refMe's NSURL's fileURLWithPath:argURL
    end if
    
    ###URLアセットを取得
    set ocidAVURLAsset to refMe's AVURLAsset's URLAssetWithURL:ocidArgFilePathURL options:(missing value)
    ###メタデータを取得
    set ocidCommonMetadata to ocidAVURLAsset's commonMetadata()
    ###タイトルメタデータを取得
    set ocidTitleArray to refMe's AVMetadataItem's metadataItemsFromArray:ocidCommonMetadata withKey:(refMe's AVMetadataCommonKeyTitle) keySpace:(refMe's AVMetadataKeySpaceCommon)
    try
        set ocidMetaTitle to ocidTitleArray's objectAtIndex:0
        set strMetaTitle to ocidMetaTitle's value() as text
    on error
        set strMetaTitle to ""
    end try
    
    return (strMetaTitle)
end doGetTitle

|

« [stringWithFormat]NSDictionaryをテキストに | トップページ | [Diagnostics Mode]ハードウェア診断モードで起動するmacOS »

AppleScript Music」カテゴリの記事