指定フォルダ内のファイルのうち、指定のUTIに該当するものをすべて取得してフルパス(file/POSIX)で返すAppleScriptです。
指定フォルダの直下のみを走査し、サブフォルダ内は走査しません。調べてみるとUTIでファイルを絞り込む機能は存在していないようなので(見落としているだけ?)、すべてファイルを取得してからループでUTIを調べつつ該当するかどうかチェックしています。
書けば書くほど「それってSpotlightでよくね?」という気がしますが、確実に取得したい(Spotlightインデックスが破損していたり、サーバー上のファイルではSpotlightが効かない場合もある)場合に使うとよいでしょうか。
| AppleScript名:指定フォルダ内のファイルのうち、指定UTIに該当するものをすべて取得してフルパスを返す |
| — – Created by: Takaaki Naganoya – Created on: 2019/04/13 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.5" use scripting additions use framework "Foundation" property |NSURL| : a reference to current application’s |NSURL| property NSArray : a reference to current application’s NSArray property NSPredicate : a reference to current application’s NSPredicate property NSFileManager : a reference to current application’s NSFileManager property NSURLTypeIdentifierKey : a reference to current application’s NSURLTypeIdentifierKey property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application’s NSDirectoryEnumerationSkipsHiddenFiles set aUTI to "com.adobe.pdf" set libPath to ((path to documents folder) as string) set posixLibPath to POSIX path of libPath set f1List to getFilepathListByUTI(posixLibPath, aUTI, "file") of me –> {file "Cherry:Users:me:Documents:0718kenpo.pdf", file "Cherry:Users:me:Documents:2013-09Rekihaku.pdf", file "Cherry:Users:maro:Documents:airserver.pdf"} set f2List to getFilepathListByUTI(posixLibPath, aUTI, "POSIX") of me –> {"/Users/me/Documents/0718kenpo.pdf", "/Users/me/Documents/2013-09Rekihaku.pdf", "/Users/me/Documents/airserver.pdf"} on getFilepathListByUTI(aFolPOSIX, aUTI as string, aFileType as string) script spdFile property urlList : {} end script if aFileType is not in {"file", "POSIX"} then return {} set aFM to NSFileManager’s defaultManager() set aFolExt to (aFM’s fileExistsAtPath:aFolPOSIX isDirectory:true) as boolean if aFolExt = false then return {} –フォルダ自体が存在しなければヌルリストを返す set aURL to |NSURL|’s fileURLWithPath:aFolPOSIX set theOptions to ((current application’s NSDirectoryEnumerationSkipsPackageDescendants) as integer) + ((current application’s NSDirectoryEnumerationSkipsHiddenFiles) as integer) set urlArray to (aFM’s contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value)) if urlArray = missing value then return {} set (urlList of spdFile) to urlArray as list set newList to {} repeat with i in (urlList of spdFile) set j to POSIX path of i set tmpUTI to my retUTIfromPath(j) set utiRes to my filterUTIList({tmpUTI}, aUTI) if utiRes is not equal to {} then if aFileType = "POSIX" then set the end of newList to j else if aFileType = "file" then set the end of newList to POSIX file j end if end if end repeat return newList end getFilepathListByUTI –指定のPOSIX pathのファイルのUTIを求める on retUTIfromPath(aPOSIXPath) set aURL to |NSURL|’s fileURLWithPath:aPOSIXPath set {theResult, theValue} to aURL’s getResourceValue:(reference) forKey:NSURLTypeIdentifierKey |error|:(missing value) if theResult = true then return theValue as string else return theResult end if end retUTIfromPath –UTIリストが指定UTIに含まれているかどうか演算を行う on filterUTIList(aUTIList, aUTIstr) set anArray to NSArray’s arrayWithArray:aUTIList set aPred to NSPredicate’s predicateWithFormat_("SELF UTI-CONFORMS-TO %@", aUTIstr) set bRes to (anArray’s filteredArrayUsingPredicate:aPred) as list return bRes end filterUTIList |































