特定のディレクトリにあるファイルサイズ最大と最小
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
005 | use AppleScript version "2.8" |
006 | use framework "Foundation" |
007 | use framework "AppKit" |
008 | use framework "UniformTypeIdentifiers" |
009 | use scripting additions |
010 | property refMe : a reference to current application |
011 | |
012 | set strTargetDirPath to ("~/Downloads") as text |
013 | set ocidTargetDirPathStr to refMe's NSString's stringWithString:(strTargetDirPath) |
014 | set ocidTargetDirPath to ocidTargetDirPathStr's stringByStandardizingPath() |
015 | set ocidTargetDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidTargetDirPath) isDirectory:false) |
016 | ############## |
017 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | #非表示URLを除外するオプション |
019 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
020 | #収集するリソースキー |
021 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
022 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
023 | ocidKeyArray's addObject:(refMe's NSURLFileSizeKey) |
024 | #対象のディレクトリのコンテンツの収集 |
025 | set listResponse to (appFileManager's contentsOfDirectoryAtURL:(ocidTargetDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error| :(reference)) |
026 | if (item 2 of listResponse) = (missing value) then |
027 | #収集したURLのArray |
028 | set ocidFilePathURLArray to (item 1 of listResponse) |
029 | else if (item 2 of listResponse) ≠ (missing value) then |
030 | set strErrorNO to (item 2 of listResponse)'s code() as text |
031 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
032 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
033 | return "エラーしました" & strErrorNO & strErrorMes |
034 | end if |
035 | ############## |
036 | #ソートキー用のArray |
037 | set ocidDescriptorsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
038 | #収取したのがNSURLなのでabsoluteString |
039 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(no) selector:("localizedStandardCompare:") |
040 | ocidDescriptorsArray's addObject:(ocidDescriptor) |
041 | #ABC順にソート |
042 | set ocidSortedURLArray to ocidFilePathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorsArray) |
043 | |
044 | ############## |
045 | #リソースキー取得用のArray |
046 | set ocidResourceArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
047 | #収集したNSURL順に処理 |
048 | repeat with itemSortedURLArray in ocidSortedURLArray |
049 | #リソースキーでDICTを取得 |
050 | set listResponse to (itemSortedURLArray's resourceValuesForKeys:(ocidKeyArray) |error| :(reference)) |
051 | set ocidResourceValues to (item 1 of listResponse) |
052 | #取得用のArrayに追加 |
053 | (ocidResourceArray's addObject:(ocidResourceValues)) |
054 | end repeat |
055 | |
056 | ############## |
057 | #ソート用のArray |
058 | set ocidDescriptorsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
059 | #ABC順 |
060 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:(refMe's NSURLLocalizedTypeDescriptionKey) ascending:(yes) selector:("localizedStandardCompare:") |
061 | ocidDescriptorsArray's addObject:(ocidDescriptor) |
062 | #ファイル修正日でソート |
063 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:(refMe's NSURLFileSizeKey) ascending:(no) |
064 | ocidDescriptorsArray's addObject:(ocidDescriptor) |
065 | #ソート実行 |
066 | set ocidSortedResourceArray to ocidResourceArray's sortedArrayUsingDescriptors:(ocidDescriptorsArray) |
067 | #ソートされた項目の個数 |
068 | set numCntArray to ocidSortedResourceArray's |count|() |
069 | |
070 | |
071 | |
072 | ##一番サイズ大きいの新しいパス |
073 | log ((ocidSortedResourceArray's firstObject())'s valueForKey:("_NSURLPathKey")) as text |
074 | |
075 | ##一番サイズの小さいパス |
076 | log ((ocidSortedResourceArray's lastObject())'s valueForKey:("_NSURLPathKey")) as text |
077 | |
AppleScriptで生成しました |
| 固定リンク