20260521

[Applescript]enumeratorAtURLでフォルダのパスを収集するallObjects

[Applescript]フォルダのパスを収集するallObjects

NOTE記事一覧ですnote.com

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

フォルダのパスを収集するallObjects.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005
006指定したフォルダの下位層を再起的にURLを取得して
007フォルダのパスのみ収集する
008
009findのtyep -d相当
010
011ループをnextObjectで行う方法
012
013
014com.cocolog-nifty.quicktimer.icefloe *)
015----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
016use AppleScript version "2.8"
017use framework "Foundation"
018use framework "AppKit"
019use framework "CoreFoundation"
020use scripting additions
021
022property refMe : a reference to current application
023
024
025#ダイアログ
026tell current application
027   set strName to name as text
028end tell
029if strName is "osascript" then
030   tell application "Finder" to activate
031else
032   tell current application to activate
033end if
034#デスクトップ
035set aliasDesktopDirPath to (path to desktop from user domain) as alias
036set listUTI to {"public.movie"} as list
037set strTitle to ("フォルダ選択") as text
038set strPrompt to ("フォルダ一覧を出力する親フォルダを選択してください") as text
039tell application "SystemUIServer"
040   activate
041   set aliasDirPath to (choose folder with prompt (strPrompt) default location (aliasDesktopDirPath) with invisibles and showing package contents without multiple selections allowed) as alias
042end tell
043
044
045#スタート時間
046set ocidStart to refMe's CFAbsoluteTimeGetCurrent()
047#フォルダのURL
048set strDirPath to (POSIX path of aliasDirPath) as text
049set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath)
050set ocidDirPathStr to ocidDirPathStr's precomposedStringWithCanonicalMapping()
051set ocidDirPath to ocidDirPathStr's stringByStandardizingPath()
052set ocidDirPath to ocidDirPath's stringByExpandingTildeInPath()
053set ocidDirPathURL to refMe's NSURL's fileURLWithPath:(ocidDirPath) isDirectory:(true)
054
055#取得キー
056set ocidKeyArrayM to refMe's NSMutableArray's alloc()'s init()
057ocidKeyArrayM's addObject:(refMe's NSURLIsDirectoryKey)
058ocidKeyArrayM's addObject:(refMe's NSURLIsRegularFileKey)
059
060#ファイルマネージャー初期化
061set appFileManager to refMe's NSFileManager's defaultManager()
062#コンテンツの収集
063set ocidOptions to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
064set ocidEmuDict to appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidKeyArrayM) options:(ocidOptions) errorHandler:(missing value)
065#allObjectでArrayにする
066set ocidEnuArray to ocidEmuDict's allObjects()
067
068#出力用おARRAY
069set ocidURLArrayM to refMe's NSMutableArray's alloc()'s init()
070
071#収集したコンテンツを
072repeat with ocidEnuURL in ocidEnuArray
073   
074   #リソースキーで値を取り出して
075   set listResponse to (ocidEnuURL's resourceValuesForKeys:(ocidKeyArrayM) |error|:(reference))
076   set ocidItemDictM to (first item of listResponse)'s mutableCopy()
077   #URLを追加して
078   (ocidItemDictM's setObject:(ocidEnuURL) forKey:("URLKey"))
079   #ARRAYに格納
080   (ocidURLArrayM's addObject:(ocidItemDictM))
081   
082end repeat
083
084#キーがフォルダのDICTのみ収集して
085set appPredicate to refMe's NSPredicate's predicateWithFormat_("%K == TRUE", (refMe's NSURLIsDirectoryKey))
086ocidURLArrayM's filterUsingPredicate:(appPredicate)
087#終了
088set ocidDirURLArray to ocidURLArrayM's valueForKey:("URLKey")
089#URLのArrayをPathのArrayに変換して
090set ocidDirPathArray to ocidDirURLArray's valueForKey:("path")
091#ソート
092set ocidDirPathArray to ocidDirPathArray's sortedArrayUsingSelector:("localizedStandardCompare:")
093
094#改行区切りのテキストに
095set ocidJoinText to ocidDirPathArray's componentsJoinedByString:(linefeed)
096set strJoinText to ocidJoinText as text
097set strDesktopPath to (POSIX path of aliasDesktopDirPath) as text
098set strDate to (current date) as text
099set strFileName to ("" & strDate & "フォルダパスリスト.txt")
100set strSaveFilePath to ("" & strDesktopPath & strFileName & "")
101tell application "TextEdit"
102   activate
103   make new document with properties {name:strFileName, path:strSaveFilePath}
104   tell document strFileName
105      activate
106      save in (POSIX file strSaveFilePath)
107      close
108   end tell
109   set docActiv to open (POSIX file strSaveFilePath)
110   
111   tell docActiv
112      set its text to strJoinText
113   end tell
114end tell
115
116#処理終了時間
117set ocidEnd to refMe's CFAbsoluteTimeGetCurrent()
118set strTime to (ocidEnd - ocidStart) as text
119log (strTime) as text
120
121return strJoinText
AppleScriptで生成しました