AppleScript Sort

[NSArray]ソート 2種

【A】sortedArrayUsingDescriptors:
複数のソート条件を指定できる 
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-4c46eb.html

【B】sortedArrayUsingSelector:
特定の条件にあわせてソートできる 簡単
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-dae2be.html


【A】sortedArrayUsingDescriptors:({Array})
複数のソート条件を指定できる
Descriptor
sortDescriptorWithKey:ascending:selector:
Sortdescriptorwithkey0021920x108072ppirg


【スクリプトエディタで開く】|

#!/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



######【KEY】
(*
■ARRAYの値が 文字列や数値の場合
self
■ARRAYの値が NSURLなら
absoluteString
host
path
lastPathComponent 等が利用できます
■ARRAYの値が DICTなら
dictのKEYを利用できる
*)
##一般的なARRAYの場合は self 一択
set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

##NSURLが格納されているArrayの場合
set ocidDirPathStr to refMe's NSString's stringWithString:("/System/Library/Templates/Data/Library/User Pictures/Instruments")
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true)
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidArrayM to appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:({(refMe's NSURLPathKey)}) options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
##ファイル名(lastPathComponent)でソート
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

set listForArray to {{reg:"東京"}, {reg:"神奈川"}, {reg:"千葉"}, {reg:"埼玉"}, {reg:"茨城"}, {reg:"群馬"}} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("reg") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list


set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

######【ascending】
# TRUE YES 正順
# FALSE NO 逆順

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(no) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

######【selector】
(*
漢字が入るなら通常これ
localizedStandardCompare
英数小文字を『先』
compare
localizedCompare
英数大文字小文字判定しない
caseInsensitiveCompare
localizedCaseInsensitiveCompare
*)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"compare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"caseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCaseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

【B】sortedArrayUsingSelector:
特定の条件にあわせてソートできる



【スクリプトエディタで開く】|

#!/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 listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list

set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

#######漢字が入るなら通常これ
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedStandardCompare:")
log ocidSortedArray as list
#######英数小文字を『先』なら
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("compare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCompare:")
log ocidSortedArray as list
#######英数大文字小文字判定しない
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("caseInsensitiveCompare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCaseInsensitiveCompare:")
log ocidSortedArray as list

|

【A】sortedArrayUsingDescriptors:

【A】sortedArrayUsingDescriptors:({Array}) 複数のソート条件を指定できる Descriptor sortDescriptorWithKey:ascending:selector: Sortdescriptorwithkey0021920x108072ppirg

【スクリプトエディタで開く】|

#!/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



######【KEY】
(*
■ARRAYの値が 文字列や数値の場合
self
■ARRAYの値が NSURLなら
absoluteString
host
path
lastPathComponent 等が利用できます
■ARRAYの値が DICTなら
dictのKEYを利用できる
*)
##一般的なARRAYの場合は self 一択
set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

##NSURLが格納されているArrayの場合
set ocidDirPathStr to refMe's NSString's stringWithString:("/System/Library/Templates/Data/Library/User Pictures/Instruments")
set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true)
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidArrayM to appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:({(refMe's NSURLPathKey)}) options:(refMe's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
##ファイル名(lastPathComponent)でソート
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("lastPathComponent") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

set listForArray to {{reg:"東京"}, {reg:"神奈川"}, {reg:"千葉"}, {reg:"埼玉"}, {reg:"茨城"}, {reg:"群馬"}} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("reg") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list


set listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list
set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

######【ascending】
# TRUE YES 正順
# FALSE NO 逆順

set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(no) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

######【selector】
(*
漢字が入るなら通常これ
localizedStandardCompare
英数小文字を『先』
compare
localizedCompare
英数大文字小文字判定しない
caseInsensitiveCompare
localizedCaseInsensitiveCompare
*)
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"compare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"caseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(yes) selector:"localizedCaseInsensitiveCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidArrayM's sortedArrayUsingDescriptors:(ocidDescriptorArray)
log ocidSortedArray as list

|

【B】sortedArrayUsingSelector:

【B】sortedArrayUsingSelector: 特定の条件にあわせてソートできる

【スクリプトエディタで開く】|

#!/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 listForArray to {"東京", "神奈川", "千葉", "埼玉", "茨城", "群馬", "AA", "bb", "aa", "1234"} as list

set ocidArrayM to refMe's NSMutableArray's arrayWithArray:(listForArray)

#######漢字が入るなら通常これ
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedStandardCompare:")
log ocidSortedArray as list
#######英数小文字を『先』なら
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("compare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCompare:")
log ocidSortedArray as list
#######英数大文字小文字判定しない
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("caseInsensitiveCompare:")
log ocidSortedArray as list
set ocidSortedArray to ocidArrayM's sortedArrayUsingSelector:("localizedCaseInsensitiveCompare:")
log ocidSortedArray as list

|

sortDescriptorWithKey:ascending:

Sortdescriptorwithkey0011920x108072ppirgSortdescriptorwithkey0021920x108072ppirg

|

[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

|

[プレビュー]ファイル名順にして開き直す

うーん他に何かいい方法ないんか…

#!/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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSMutableArray : a reference to objMe's NSMutableArray


(*
tell application "Preview"
set listEveryDoc to every document
log "listEveryDoc"
log listEveryDoc
end tell



tell application "Preview"
set listEveryDocName to name of every document
log "name"
log listEveryDocName
end tell

tell application "Preview"
set listEveryDocPath to path of every document
log "path"
log listEveryDocPath
end tell

log listEveryDocPath
*)
######
tell application "Preview"
set listEveryDocPath to path of every document
log "path"
log listEveryDocPath
activate
end tell

if listEveryDocPath is {} then
return "ドキュメントを開いていません"
end if


###可変リストに変更
set ocidNSArrayM to (objNSMutableArray's arrayWithArray:listEveryDocPath)
###並び替え
set ocidSortedArray to (ocidNSArrayM's sortedArrayUsingSelector:"compare:")
###ocidアレイ を リストに変換
set listSortedArray to ocidSortedArray as list

#tell application "Preview"
#activate
#tell application "System Events"
#keystroke "W" using {command down}
#end tell
#end tell
#return

tell application "Preview"
close every document
end tell

tell application "Preview"
tell window 1
####同じウィンドウで
####ファイル名順に開き直す
open listSortedArray
end tell
end tell

|

[valueForKeyPath] pathExtension

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# 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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

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

#####
set ocidNSURLIsAliasFileKey to objMe's NSURLIsAliasFileKey
set ocidNSURLIsRegularFileKey to objMe's NSURLIsRegularFileKey
set ocidNSURLFileSizeKey to objMe's NSURLFileSizeKey
#####

try
set listAliasDir to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location (path to desktop folder from user domain) with multiple selections allowed without invisibles and showing package contents)
on error
log "エラーしました"
return
end try


######フォルダ毎の処理
repeat with objAliasDir in listAliasDir

###エリアス
set aliasAliasDir to objAliasDir as alias
###パス
set strDirPath to POSIX path of aliasAliasDir as text
###NSStringテキスト
set ocidDirPath to (objNSString's stringWithString:strDirPath)
#####NSURL NSStringURL
set ocidUrlPath to (objNSURL's fileURLWithPath:ocidDirPath)
###フォルダの内容をリストで取得
set objContentsArray to (objFileManager's contentsOfDirectoryAtURL:ocidUrlPath includingPropertiesForKeys:{ocidNSURLIsAliasFileKey} options:(4) |error|:(missing value))
###可変リストに変更
set ocidNSArrayM to (objNSMutableArray's arrayWithArray:objContentsArray)
###ファイルパスリストに変更
set ocidNSArrayMpath to (ocidNSArrayM's valueForKeyPath:"pathExtension")
###並び替え
set ocidSortedArray to (ocidNSArrayMpath's sortedArrayUsingSelector:"compare:")
log ocidSortedArray's className() as text
log ocidSortedArray as list
###並び変わった順
repeat with objSortedArray in ocidSortedArray
log objSortedArray as text
--->>ここに順番に処理する操作等を記述する
end repeat
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)
(*png*)

end repeat

|

[valueForKeyPath] relativeString

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# 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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

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

#####
set ocidNSURLIsAliasFileKey to objMe's NSURLIsAliasFileKey
set ocidNSURLIsRegularFileKey to objMe's NSURLIsRegularFileKey
set ocidNSURLFileSizeKey to objMe's NSURLFileSizeKey
#####

try
set listAliasDir to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location (path to desktop folder from user domain) with multiple selections allowed without invisibles and showing package contents)
on error
log "エラーしました"
return
end try


######フォルダ毎の処理
repeat with objAliasDir in listAliasDir

###エリアス
set aliasAliasDir to objAliasDir as alias
###パス
set strDirPath to POSIX path of aliasAliasDir as text
###NSStringテキスト
set ocidDirPath to (objNSString's stringWithString:strDirPath)
#####NSURL NSStringURL
set ocidUrlPath to (objNSURL's fileURLWithPath:ocidDirPath)
###フォルダの内容をリストで取得
set objContentsArray to (objFileManager's contentsOfDirectoryAtURL:ocidUrlPath includingPropertiesForKeys:{ocidNSURLIsAliasFileKey} options:(4) |error|:(missing value))
###可変リストに変更
set ocidNSArrayM to (objNSMutableArray's arrayWithArray:objContentsArray)
###ファイルパスリストに変更
set ocidNSArrayMpath to (ocidNSArrayM's valueForKeyPath:"relativeString")
###並び替え
set ocidSortedArray to (ocidNSArrayMpath's sortedArrayUsingSelector:"compare:")
log ocidSortedArray's className() as text
log ocidSortedArray as list
###並び変わった順
repeat with objSortedArray in ocidSortedArray
log objSortedArray as text
--->>ここに順番に処理する操作等を記述する
end repeat

(*file:///Users/Shared/Desktop/1280x720/1280x720.001.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.002.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.003.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.004.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.005.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.006.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.007.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.008.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.009.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.010.png*)
end repeat

|

[valueForKeyPath] relativePath

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# 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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

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

#####
set ocidNSURLIsAliasFileKey to objMe's NSURLIsAliasFileKey
set ocidNSURLIsRegularFileKey to objMe's NSURLIsRegularFileKey
set ocidNSURLFileSizeKey to objMe's NSURLFileSizeKey
#####

try
set listAliasDir to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location (path to desktop folder from user domain) with multiple selections allowed without invisibles and showing package contents)
on error
log "エラーしました"
return
end try


######フォルダ毎の処理
repeat with objAliasDir in listAliasDir

###エリアス
set aliasAliasDir to objAliasDir as alias
###パス
set strDirPath to POSIX path of aliasAliasDir as text
###NSStringテキスト
set ocidDirPath to (objNSString's stringWithString:strDirPath)
#####NSURL NSStringURL
set ocidUrlPath to (objNSURL's fileURLWithPath:ocidDirPath)
###フォルダの内容をリストで取得
set objContentsArray to (objFileManager's contentsOfDirectoryAtURL:ocidUrlPath includingPropertiesForKeys:{ocidNSURLIsAliasFileKey} options:(4) |error|:(missing value))
###可変リストに変更
set ocidNSArrayM to (objNSMutableArray's arrayWithArray:objContentsArray)
###ファイルパスリストに変更
set ocidNSArrayMpath to (ocidNSArrayM's valueForKeyPath:"relativePath")
###並び替え
set ocidSortedArray to (ocidNSArrayMpath's sortedArrayUsingSelector:"compare:")
log ocidSortedArray's className() as text
log ocidSortedArray as list
###並び変わった順
repeat with objSortedArray in ocidSortedArray
log objSortedArray as text
--->>ここに順番に処理する操作等を記述する
end repeat

(*/Users/Shared/Desktop/1280x720/1280x720.001.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.002.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.003.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.004.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.005.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.006.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.007.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.008.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.009.png*)
(*/Users/Shared/Desktop/1280x720/1280x720.010.png*)
end repeat

|

[valueForKeyPath] absoluteString

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# 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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

property objNSArray : a reference to objMe's NSArray
property objNSMutableArray : a reference to objMe's NSMutableArray

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

#####
set ocidNSURLIsAliasFileKey to objMe's NSURLIsAliasFileKey
set ocidNSURLIsRegularFileKey to objMe's NSURLIsRegularFileKey
set ocidNSURLFileSizeKey to objMe's NSURLFileSizeKey
#####

try
set listAliasDir to (choose folder "画像が入っているフォルダを選んでください" with prompt "フォルダを選択してください" default location (path to desktop folder from user domain) with multiple selections allowed without invisibles and showing package contents)
on error
log "エラーしました"
return
end try


######フォルダ毎の処理
repeat with objAliasDir in listAliasDir

###エリアス
set aliasAliasDir to objAliasDir as alias
###パス
set strDirPath to POSIX path of aliasAliasDir as text
###NSStringテキスト
set ocidDirPath to (objNSString's stringWithString:strDirPath)
#####NSURL NSStringURL
set ocidUrlPath to (objNSURL's fileURLWithPath:ocidDirPath)
###フォルダの内容をリストで取得
set objContentsArray to (objFileManager's contentsOfDirectoryAtURL:ocidUrlPath includingPropertiesForKeys:{ocidNSURLIsAliasFileKey} options:(4) |error|:(missing value))
###可変リストに変更
set ocidNSArrayM to (objNSMutableArray's arrayWithArray:objContentsArray)
###ファイルパスリストに変更
set ocidNSArrayMpath to (ocidNSArrayM's valueForKeyPath:"absoluteString")
###並び替え
set ocidSortedArray to (ocidNSArrayMpath's sortedArrayUsingSelector:"compare:")
log ocidSortedArray's className() as text
log ocidSortedArray as list
###並び変わった順
repeat with objSortedArray in ocidSortedArray
log objSortedArray as text
--->>ここに順番に処理する操作等を記述する
end repeat

(*file:///Users/Shared/Desktop/1280x720/1280x720.001.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.002.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.003.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.004.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.005.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.006.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.007.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.008.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.009.png*)
(*file:///Users/Shared/Desktop/1280x720/1280x720.010.png*)
end repeat

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom