指定フォルダ以下に存在するYYYY/MM/DDの3階層のフォルダで最新のものを抽出してFinder上で選択状態にするAppleScriptです。
もともと、Safariのダウンロードフォルダに対してFolder Action Scriptを設定して、当日のYYYY/MM/DDの3階層のフォルダを作成してそこにダウンロードしたファイルを移動する処理を行っていました。
その最新フォルダをFinder上で表示する必要があったので、作成したものです。
AppleScriptのPOSIX pathとCocoaのPOSIX path(NSString)では末尾のスラッシュの有無が異なっており、専用ルーチンから切り分けて汎用ルーチンとして使いまわそうとして久しぶりにハマりました。
AppleScript名:指定フォルダのサブフォルダの中からYYYY_MM_DDで最新のものを抽出してFinderでオープン |
— Created 2018-04-09 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html set sDL to POSIX path of (choose folder) –> "/Users/me/Downloads/"–AppleScript HFS to POSIX path (ends with slash) retLatestYYYYMMDDFolderUnder(sDL, 2017) of me on retLatestYYYYMMDDFolderUnder(aPOSIXFolderPath as string, minYYYY as integer) load framework –load BridgePlus framework if aPOSIXFolderPath ends with "/" then set aPOSIXFolderPath to text 1 thru -2 of aPOSIXFolderPath end if –> "/Users/maro/Downloads"–Cocoa POSIX path (does not end with) set sList to (current application’s NSString’s stringWithString:aPOSIXFolderPath)’s pathComponents() as list –> {"/", "Users", "me", "Downloads"} set sLen to length of sList –> 4 set fListRes to getFolderspathComponentsIn(aPOSIXFolderPath) of me –> {{"/", "Users", "me", "Downloads", "2016"}, {"/", "Users", "me", "Downloads", "2018", "03", "20"}…} set anArray to current application’s NSMutableArray’s arrayWithArray:fListRes set predStr to "SELF[SIZE] = " & (sLen + 3) as string set aPredicate to current application’s NSPredicate’s predicateWithFormat:predStr set t1Array to (anArray’s filteredArrayUsingPredicate:aPredicate) –> {{"/", "Users", "me", "Downloads", "2018", "03", "20"}…} set predStr1 to ("SELF[" & (sLen) as string) & "].floatValue >= " & (minYYYY as string) & " && SELF[" & ((sLen + 1) as string) & "].floatValue >= 1 && SELF[" & ((sLen + 2) as string) & "].floatValue >= 1" set aPredicate1 to current application’s NSPredicate’s predicateWithFormat:predStr1 set t2Array to (t1Array’s filteredArrayUsingPredicate:aPredicate1) as list –YYYY, MM, DDでそれぞれ降順ソート。最新のものを取得 set theResult to current application’s SMSForder’s subarraysIn:t2Array sortedByIndexes:{sLen, sLen + 1, sLen + 2} ascending:{false, false, false} sortTypes:{"compare:", "compare:", "compare:"} |error|:(missing value) if (theResult as list = {} or theResult = missing value) then return {} –最新のYYYY/MM/DDフォルダを取得してFinder上で選択表示 set theNewest to contents of first item of (theResult as list) set theNewestStr to (current application’s NSString’s pathWithComponents:theNewest) set parentPath to theNewestStr’s stringByDeletingLastPathComponent() set aRes to current application’s NSWorkspace’s sharedWorkspace()’s selectFile:theNewestStr inFileViewerRootedAtPath:parentPath end retLatestYYYYMMDDFolderUnder on getFolderspathComponentsIn(posixPath) script spd property allItems : {} end script set allItems of spd to {} set theNSURL to current application’s |NSURL|’s fileURLWithPath:posixPath set theNSFileManager to current application’s NSFileManager’s new() — get URL enumerator set theNSFileEnumerator to theNSFileManager’s enumeratorAtURL:theNSURL includingPropertiesForKeys:{current application’s NSURLIsDirectoryKey, current application’s NSURLIsPackageKey} options:((current application’s NSDirectoryEnumerationSkipsPackageDescendants) + (current application’s NSDirectoryEnumerationSkipsHiddenFiles as integer)) errorHandler:(missing value) — get all items from enumerator set (allItems of spd) to theNSFileEnumerator’s allObjects() set theFolders to {} — to store folders — loop through repeat with i from 1 to count of (allItems of spd) — is it a directory? set {theResult, isDirectory} to ((item i of (allItems of spd))’s getResourceValue:(reference) forKey:(current application’s NSURLIsDirectoryKey) |error|:(missing value)) if isDirectory as boolean = true then set {theResult, isPackage} to ((item i of (allItems of spd))’s getResourceValue:(reference) forKey:(current application’s NSURLIsPackageKey) |error|:(missing value)) — is it not a package? if not isPackage as boolean = true then set end of theFolders to (item i of (allItems of spd))’s pathComponents() as list –parse each directory into list end if end if end repeat return theFolders end getFolderspathComponentsIn |
More from my site
(Visited 124 times, 1 visits today)