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