« [Shortcuts Events]ショートカット(Shortcuts)の一覧を取得数 | トップページ | [TextEdit]新規ファイル »

[SORT]ファイルパスリストからファイル名の重複を調べる

#/bin/sh

find $HOME/Music  -type f -iname "*.mp3" >> ~/Desktop/FileList.txt

find $HOME/Music  -type f -iname "*.m4a" >> ~/Desktop/FileList.txt


とか で ファイルリスト出した時用


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*

find $HOME/Music -type f -iname "*.mp3" >> ~/Desktop/FileList.txt
find $HOME/Music -type f -iname "*.m4a" >> ~/Desktop/FileList.txt

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSArray : a reference to refMe's NSArray
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSURL : a reference to refMe's NSURL

property refNSRegularExpression : a reference to refMe's NSRegularExpression
property refNSRegularExpressionSearch : a reference to refMe's NSRegularExpressionSearch
property refNSCharacterSet : a reference to refMe's NSCharacterSet



##############################################
## ダイアログ関連
##############################################
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
set listChooseFileUTI to {"public.item", "public.text"}
set strPromptText to "ファイルを選んでください" as text
set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI without invisibles, multiple selections allowed and showing package contents) as alias

####ファイルパス
set strFilePath to POSIX path of aliasFilePath as text
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidFilePathURL to refNSURL's fileURLWithPath:ocidFilePath
###ファイル名
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
###拡張子を取ったベースになるファイル名
set strBaseFileName to (ocidBaseFilePathURL's lastPathComponent()) as text

##############################################
## 保存先 ダイアログ関連
##############################################

set aliasDefaultLocation to (path to desktop folder from user domain) as alias
set strDefaultName to (strBaseFileName & ".output.txt") as text
set strPromptText to "名前を決めてください"

set aliasSaveFilePath to (choose file name default location aliasDefaultLocation default name strDefaultName with prompt strPromptText) as «class furl»
####UNIXパス
set strSaveFilePath to POSIX path of aliasSaveFilePath as text
####ドキュメントのパスをNSString
set ocidSaveFilePath to refNSString's stringWithString:strSaveFilePath
####ドキュメントのパスをNSURL
set ocidSaveFilePathURL to refNSURL's fileURLWithPath:ocidSaveFilePath
###拡張子取得
set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
###ダイアログで拡張子を取っちゃった時対策
if strFileExtensionName is not "lrc" then
set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:"txt"
end if

##############################################
## 読み込んだ内容のテキスト段階での処理
##############################################
####可変テキスト初期化
set ocidReadData to refNSMutableString's alloc()'s initWithCapacity:0
#####読み取ったコンテンツをテキストに格納
set ocidReadData to refNSMutableString's stringWithContentsOfFile:ocidFilePath encoding:(refMe's NSUTF8StringEncoding) |error|:(missing value)
#########パス部分を削除してファイル名のみにする
###レンジを作る
set ocidNsRange to ocidReadData's rangeOfString:ocidReadData
###置き換え
ocidReadData's replaceOccurrencesOfString:"/.*/" withString:("") options:(refNSRegularExpressionSearch) range:ocidNsRange
#########空行があれば削除しておく
###レンジを作る
set ocidNsRange to ocidReadData's rangeOfString:ocidReadData
###置き換え
ocidReadData's replaceOccurrencesOfString:"\n\n" withString:("\n") options:(refNSRegularExpressionSearch) range:ocidNsRange

##############################################
##改行でリスト化する
##############################################
###初期化
set ocidReadDataArray to refNSMutableArray's alloc()'s initWithCapacity:0
####改行を定義
set ocidNewlineCharacterSett to refNSCharacterSet's newlineCharacterSet()
####改行でリスト化
set ocidReadDataArray to ocidReadData's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett
###初期化
set ocidReadDataArrayM to refNSMutableArray's alloc()'s initWithCapacity:0
###可変リストに格納
ocidReadDataArrayM's addObjectsFromArray:ocidReadDataArray
####読み込んだテキストを解放する
set ocidReadData to "" as text
##############################################
##空のアイテムがあれば削除する
##############################################
set numCntArrayItem to (count of ocidReadDataArrayM) as integer
repeat
####空のオブジェクトがあるか?
set boolContain to (ocidReadDataArrayM's containsObject:"") as boolean
####あれば
if boolContain is true then
####空のオプジェクトのインデクスを調べて
set ocidIndex to (ocidReadDataArrayM's indexOfObject:"") as integer
###削除する
ocidReadDataArrayM's removeObjectAtIndex:ocidIndex
else
####空のオブジェクトが無くなればリピートを抜ける
exit repeat
end if
end repeat

##############################################
##正順に並び替える
##############################################
###初期化
set ocidSortArray to refNSMutableArray's alloc()'s initWithCapacity:0
set ocidSelf to refNSString's stringWithString:"self"
set ocidSortArray to ocidReadDataArrayM's sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:"
####可変リストに格納
set ocidSortedArray to refNSMutableArray's alloc()'s initWithCapacity:0
ocidSortedArray's addObjectsFromArray:ocidSortArray


##############################################
##本処理
##############################################
####リストの数を数える
set numCntArrayData to (count of ocidSortedArray) as integer
####index用の1少ない数
set numCntNo to (numCntArrayData - 1) as integer
####処理は逆順に行う
repeat (numCntArrayData - 1) times
####最後のアイテムを取り出す
set strItemA to (ocidSortedArray's objectAtIndex:numCntNo) as text
####次のアイテムを取り出す
set numCntNextNo to numCntNo - 1 as integer
set strItemB to (ocidSortedArray's objectAtIndex:numCntNextNo) as text
####内容が同じなら
if strItemA is not strItemB then
###逆順処理なので先の項目strItemAを削除する
ocidSortedArray's removeObjectAtIndex:numCntNo
end if
set numCntNo to numCntNo as integer
####最後の行は処理しない(比較相手がない=削除しても良いか)
if numCntNo = 1 then
ocidSortedArray's removeObjectAtIndex:0
exit repeat
end if
set numCntNo to numCntNo - 1 as integer
end repeat

##############################################
##テキスト化する
##############################################
###可変テキストを初期化
set ocidOutPutStrings to refNSMutableString's alloc()'s initWithCapacity:0
###オブジェクト毎に改行を入れたテキストにする
repeat with objMutableArray in ocidSortedArray
(ocidOutPutStrings's appendString:(objMutableArray as text))
log objMutableArray as text
(ocidOutPutStrings's appendString:("\n"))
end repeat


##############################################
##保存
##############################################
set listWritetoUrlArray to ocidOutPutStrings's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

#####データ部
set boolDone to item 1 of listWritetoUrlArray as boolean
if boolDone is true then
log "終了しました"
else
log "エラーが発生しました"
end if
####エラー部
set ocidNSErrorData to item 2 of listWritetoUrlArray
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if


##############################################
## エラー発生時のログ用
##############################################


to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

« [Shortcuts Events]ショートカット(Shortcuts)の一覧を取得数 | トップページ | [TextEdit]新規ファイル »

AppleScript Sort」カテゴリの記事