--filePath.scpt Ver.3.2 Copyright 2021- ataruchi. [ https://twitter.com/ataruchi ] -- AppleScript を学んで1.5ヶ月の作者が実装したため、問題があれば教えて頂けると助かります。 -- 著作権は放棄していませんが、ご自由に 利用・改造して頂いて構いません -- なお 不都合等ありましても、作者は 何の補償も致しかねます。 ご注意ください! --  1行めの「スクリプトの著作権表示部分」を削除することはできません。 --  また、改造したことが分かるよう 改造者へのリンクを表示するようにしてください。 --2021/08/26 Ver.1.0 全ハンドラーにて、実行結果テキストを クリップボードにも 保存(Excel対応) --2021/08/31 Ver.1.1 「フィルタ参照による指定拡張子の抽出」版に変更 --2021/09/08 Ver.2.0 Vanilla AppleScript 最終版 → 文字列連結を止め、「文字列リストに要素(1行分の文字列)を追加」版に書き替え --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 初版 ("NSFileManager","NSMutableArray" 利用の実装) -- 「2度のソート等を含めた実装でも、Vanilla AS "Finder" 利用の実装と比べて 10倍は高速!」 -- Cocoa AppleScript ハンドラーの基本部分は、Piyomaru Software さん 以下記事の「独自実装」版となります! -- http://piyocast.com/as/archives/1660 "Finder" と同等のソート(並び)順で取得できるよう改良+機能追加。 -- (Cocoa AppleScript の書き方を示して頂いた)著者の Takaaki Naganoya さんに感謝いたします。 --2021/09/26 Ver.3.1 Cocoa AppleScript 対応 第2版 (利便性のため、以下5ハンドラーを追加) getFolderPath() -- getMultiFolderPath() checkFilePath() checkDirPath() getUtiByChooseFile() --2021/10/03 Ver.3.2 Cocoa AppleScript 対応 第3版 UTF-8 TextFile Read/Write/Appendハンドラーを追加 use AppleScript version "2.4" -- macOS10.10 [Yosemite] ~ use scripting additions -- use OSAX use framework "Foundation" -- use AppleScriptObjC (ASOC) == Cocoa AppleScript --NSObjectを最上位とする以下NSオブジェクトは Cocoa 用のオブジェクトで、Objective-C で実装されている! property |NSURL| : a reference to current application's |NSURL| --SubDir内を除外したDir内の列挙時に利用 property NSString : a reference to current application's NSString --dirPath, fileName, ext 取得可 property NSPredicate : a reference to current application's NSPredicate --正規表現 Esc処理に一部問題あり property NSEnumerator : a reference to current application's NSEnumerator --列挙用 property NSFileManager : a reference to current application's NSFileManager --"Finder" の代替Obj property NSMutableArray : a reference to current application's NSMutableArray --変更可能な NSArray property NSMutableString : a reference to current application's NSMutableString --変更可能な NSString property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding --UTF-8Encode property NSDirectoryEnumerationSkipsSubdirectoryDescendants : a reference to current application's NSDirectoryEnumerationSkipsSubdirectoryDescendants --Skip SubDir's (File or Dir) property NSDirectoryEnumerationSkipsPackageDescendants : a reference to current application's NSDirectoryEnumerationSkipsPackageDescendants --Skip Package. ex) "Microsoft Excel.app" property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application's NSDirectoryEnumerationSkipsHiddenFiles --Skip HiddenFiles for Enumeration -- (global) 定数の宣言 property HT : ASCII character (9) --水平タブ(タブ区切りテキストファイルのタブコード) = tab property LF : ASCII character (10) -- vbLf [macOS10~ を含むUnix系の改行コード] for Unix property CR : ASCII character (13) -- vbCr [~Macintosh System9 の改行コード] for (OLD Mac) property CRLF : CR & LF -- vbCrLf [MS Windows系の改行コード] for Win10, Win7 -- Excel対応のクリップボードに 区切り文字 vbCrLf を利用する理由 -- Excelでは「セル内の改行コードとしてvbLf」が用いられているため、 -- 「改行コードとしてvbCrLf」を挟まないと [command]+[V]失敗! property HIDDEN_FILES : true --「隠しファイル&フォルダ」無効(除外する) false 有効(除外しない) --for Debug test3() on test3() set paramStr to "/Users/wito/Downloads/AppendTestData.tsv" & CRLF & "create" & CRLF & "CRLF" & CRLF & "" set theResult to putTextFileAsUTF8(paramStr) if theResult ≠ "" then display dialog "正常終了[ " & theResult & " ]" end if end test3 on test() -- Test AppleScript Handler example -- set the clipboard to {} --クリップボードの初期化 --set paramStr to "★プロンプト文字列 Test★" & LF & (POSIX path of (path to home folder)) & LF & "css" & LF & "html" & LF & "txt" & LF & "scpt" & LF & "xml" & LF & "png" --ファイル名に対し、正規表現で抽出:「末尾が .css 又は .js で、light の文字列を含む」 set paramStr to "" & LF & "" & LF & "(.*light.*\\.(css|js)+$)" getFileListOfChildFoldersByRegExp(paramStr) end test --文字列リストを任意のデリミタ(通常は改行コード)付きで、テキストに変換 on getTextWithDelimiters(stringList, withDelimiters) set returnText to "" --ハンドラーの戻り値を初期化 try set currentDelimiters to (AppleScript's text item delimiters) --デフォルトのデリミタを保存 set (AppleScript's text item delimiters) to withDelimiters --デリミタを変更 set returnText to (stringList as string) --デリミタ付きテキストに変換 Not as text! (* -- http://www.script-factory.net/monologue/FastDataOperation/index.xhtml --全要素へ高速なアクセスを行う方法(テキスト処理への応用: paragraphs要素を利用して、 script list_wrapper --スクリプトオブジェクトのpropertyのリストにアクセス) property paragList : paragraphs of returnText end script repeat with parag in paragList of list_wrapper --display dialog (get contents of parag) end repeat *) set (AppleScript's text item delimiters) to currentDelimiters --デフォルトのデリミタに戻す on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to currentDelimiters --デフォルトのデリミタに戻す display dialog "★ getTextWithDelimiters ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try return returnText --display dialog returnText end getTextWithDelimiters --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 -- 実行結果 [NSMutableArray] (子フォルダを含む)ファイルのフルパス on retSortedArrayLikeFinderWithSortKey(theFolder) try --PreSort & 先頭にSortKey6桁追加後、計2回ソートして Finder利用時のソート順を獲得 set myFM to NSFileManager's defaultManager() --Use for myFM's ~ set preArray to NSMutableArray's array() --PreSort前用A => removeAll set psArray to NSMutableArray's array() --PreSort後用B => removeAll 失敗 set theArray to NSMutableArray's array() --先頭に、SortKey6桁追加 => removeAll set childArray to NSMutableArray's array() --子(+親)フォルダのフルパスを「逆順Descending」格納 set sArray to NSMutableArray's array() --Sorted anArray => removeAll 失敗 --[0] パラメータのチェック(フォルダが有効かどうか?) set isDir to false --フォルダ? set validPath to false --(File or Folder)パスが有効か? --(reference) isDir ポインタ(参照)渡しで、値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:theFolder isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else error --パスが無効な場合 end if if (not isDir) then --(not DirPath) == FilePath set tmpNSString to NSString's stringWithString:theFolder --NSString_obj を生成 set theFolder to ((tmpNSString's stringByDeletingLastPathComponent() as string) & "/") end if --[1] サブフォルダを含む「ファイルorフォルダのフルパス」を取得 (並び順不定) childArray's addObject:theFolder --まず、親フォルダのフルパスを追加(要素番号 0 ) set thePath to NSString's stringWithString:theFolder --NSString オブジェクトを生成 set dirEnum to myFM's enumeratorAtPath:thePath --子(Sub)Dir内も含めて(File/Folder)が列挙される -- NSFileManager's enumeratorAtPath -> preArray repeat --列挙子利用で、指定ディレクトリ内の全パスを取得 子(サブ)フォルダ対応で再帰取得  set pathObj to (dirEnum's nextObject()) --NSEnumerator next(並び順不定=>PreSort必要) if (pathObj = missing value) then exit repeat --再帰の終了条件(=NULL =nil) set aFullPath to thePath's stringByAppendingPathComponent:pathObj --Dirパス時あり preArray's addObject:(aFullPath) --ファイルorフォルダのフルパスを、要素として追加 end repeat --[2] (並び順不定)のため、「ファイルorフォルダのフルパス」でソート (PreSort) -- preArray -> psArray (少なくとも、ファイル名に関してはソート有効) --PreSort like "Finder" preArray => psArray by "localizedStandardCompare:" -- from sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:" set psArray to preArray's sortedArrayUsingSelector:"localizedStandardCompare:" preArray's removeAllObjects() --preArray 全要素削除 --[3] 次STEPの[4]で「ファイルパスのフォルダ部分のみ」でソートできるよう、先頭にSortKey6桁追加 -- psArray -> theArray ①フォルダフルパスはchildArrayの先頭に挿入のみで、theArray除外 --  ②「隠しファイル&フォルダ」対応←当ファイル先頭部分の HIDDEN_FILES で除外/対象を指定 set isDir to true --フォルダ? set validPath to false --(File or Folder)パスが有効か? --指定ディレクトリのファイルのみを抽出 (再帰的 子(サブ)フォルダ対応) repeat with pathObj in psArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい --display dialog (pathObj as string) --for Debug set aFullPath to pathObj --NSString オブジェクト --(reference) isDir ポインタ(参照)渡しで値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:aFullPath isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else error --パスが無効な場合 end if if isDir then --パスが Dir を示す場合(子フォルダ) if HIDDEN_FILES then --隠しファイル&フォルダ無効(除外時) -- .(ピリオド) で始まる「隠しフォルダ」を除外する if (aFullPath as string) does not contain "/." then -- /. が含まれないか? (childArray's insertObject:((aFullPath as string) & "/") atIndex:0) --Arrayの先頭要素として追加(先頭に挿入)--逆順に処理した方が早く見つかる --子フォルダのフルパスを追加(要素番号 0 )--for sortKey 降順(Descending)格納 end if else --隠しファイル&フォルダ有効(除外しない) (childArray's insertObject:((aFullPath as string) & "/") atIndex:0) --Arrayの先頭要素として追加(先頭に挿入)--逆順に処理した方が早く見つかる --子フォルダのフルパスを追加(要素番号 0 )--for sortKey 降順(Descending)格納 end if else --パスが File を示す場合 [注] 6桁数字 -> 約100万サブ・フォルダまで正しくソート可 set newDirPath to (aFullPath's stringByDeletingLastPathComponent() as string) & "/" set sortKey to "" set sortKeyNum to (childArray's |count|()) --逆順で検索した方が早く見つかる repeat with pathObj in childArray --childArray は 降順(Descending)格納 if ((pathObj as string) = newDirPath) then --数値の書式化(前Zero挿入した6桁数字) 6桁数字なら、5文字の"0"を連結 set sortKey to (text -6 thru -1 of ("00000" & sortKeyNum)) exit repeat end if set sortKeyNum to (sortKeyNum - 1) as integer --逆順に処理した方が早く見つかる end repeat if (sortKeyNum < 1) then --if (sortKeyNum = 0) set sortKey to "000000" --隠しファイル&フォルダ無効(除外時)対応 end if --ファイルパスの先頭に、sortKey 6桁数字 を追加 set sortStr to (sortKey & (aFullPath as string)) set sortPath to (NSString's stringWithString:sortStr) --NSString 生成 if HIDDEN_FILES then --隠しファイル無効(除外する) -- .(ピリオド) で始まる「隠しファイル&フォルダ」を除外する if sortKeyNum > 0 then if (sortPath as string) does not contain "/." then -- /. が含まれないか? (theArray's addObject:sortPath) --ファイルフルパスを、要素として追加 end if end if else --「隠しファイル&フォルダ有効」(除外しない) if sortKeyNum > 0 then (theArray's addObject:sortPath) --ファイルフルパスを、要素として追加 end if end if end if end repeat --psArray's removeAllObjects() --psArray 全要素削除 実行時エラーで、削除できず --return childArray as list --for Debug --return theArray as list --for Debug childArray's removeAllObjects() --childArray 全要素削除 --[4] 「先頭のSortKey6桁」を利用し、「ファイルパスのフォルダ部分のみ」でソート -- theArray -> sArray 「ファイルフルパス」ソートのみだと、同一フォルダが複数回出現する可能性 --Sort like "Finder" theArray => sArray by "localizedStandardCompare:" -- from sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:" -- sortKey6桁を追加してSORTしないと、同一フォルダ内のファイルパスが一塊にならない set sArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:" theArray's removeAllObjects() --theArray 全要素削除 return sArray --as list --先頭に「SortKey6桁」を付加した NSMutableArray を return on error display dialog "★ retSortedArrayLikeFinderWithSortKey ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return sArray --as list end try end retSortedArrayLikeFinderWithSortKey --親フォルダ内のファイルのみを抽出するため、ソート1回のみ(SortKey6桁を付加する必要なし) -- 実行結果[NSMutableArray] (子フォルダを含まない)ファイルのフルパス on retSortedArrayNotRecursive(theFolder) try set myFM to NSFileManager's defaultManager() --Use for myFM's ~ --PreSort & 先頭にSortKey6桁追加後、計2回ソートにて Finder利用時のソート順を獲得 set preArray to NSMutableArray's array() --Sort前用Array => removeAll set sArray to NSMutableArray's array() --Sorted preArray => removeAll 失敗 set thePath to NSString's stringWithString:theFolder --NSString オブジェクトを生成 --set dirEnum to myFM's enumeratorAtPath:thePath --子(Sub)Dir内も含めて(File/Folder)が列挙される --[1] サブフォルダを含めない「ファイルorフォルダのフルパス」を取得 (並び順不定) -- NSFileManager's enumeratorAtURL -> preArray ( |NSURL|は POSIX path も対象) set isDir to false --フォルダ? set validPath to false --(File or Folder)パスが有効か? --(reference) isDir ポインタ(参照)渡しで、値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:theFolder isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else error --パスが無効な場合 end if if (not isDir) then --(not DirPath) == FilePath set tmpNSString to NSString's stringWithString:theFolder --NSString_obj を生成 set theFolder to (tmpNSString's stringByDeletingLastPathComponent() as string) & "/" end if set theURL to |NSURL|'s fileURLWithPath:theFolder --POSIX path => |NSURL| --Skip SubDir's (files or folders)! 子フォルダ(内のファイル/フォルダ)を無視する set theOptions to (NSDirectoryEnumerationSkipsSubdirectoryDescendants as integer) if HIDDEN_FILES then --SkipsHiddenFiles + SkipsPackage set theOptions to (theOptions + (NSDirectoryEnumerationSkipsHiddenFiles as integer) + (NSDirectoryEnumerationSkipsPackageDescendants as integer)) end if --set urlArray to myFM's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:theOptions |error|:(missing value) --theOptionsを満たす Array を生成 set dirEnumerator to myFM's enumeratorAtURL:theURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value) --theOptionsを満たす[列挙子]を作成 repeat set fileURL to (dirEnumerator's nextObject()) --列挙 next if fileURL = missing value then exit repeat --列挙 終了条件 set isDir to false --フォルダ? set validPath to false --(File or Folder)パスが有効か? set fileUrlPath to POSIX path of (fileURL as string) --(reference) isDir ポインタ(参照)渡しで、値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:fileUrlPath isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else error --列挙のため、(file or dir) 必ず存在するはず end if if isDir then --パスが Dir を示す場合(子フォルダ) --無条件、除外する else --パスが File を示す場合 preArray's addObject:(fileUrlPath) --ファイルのフルパスを、要素として追加 end if end repeat --列挙 --[2] (並び順不定)のため、「ファイルのフルパス」でソート (PreSort) -- preArray -> sArray --PreSort like "Finder" preArray => sArray by "localizedStandardCompare:" -- from sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:" set sArray to preArray's sortedArrayUsingSelector:"localizedStandardCompare:" preArray's removeAllObjects() --preArray 全要素削除 return sArray --as list --NSMutableArray を return on error display dialog "★ retSortedArrayNotRecursive ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return sArray --as list --NSMutableArray を return end try end retSortedArrayNotRecursive --指定フォルダ以下のすべてのファイルを再帰で取得(文字列&拡張子 絞り込み なし) -- 実行結果[List] (子フォルダを含む)フォルダのフルパス あるいは (拡張子を含めた)ファイル名 on retFileNameWithinAFolderWithRecursive(theFolder) set aArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] ①「先頭のSortKey6桁」を削除  ②「フォルダパス」+「拡張子を含むファイル名」に分割 -- sArray -> aArray [注] return aArray as list(NSMutableArray を List にキャスト) --set sArrayEnum to sArray's objectEnumerator() --enumerate for sArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in sArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (aArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 (aArray's addObject:(tempObj's lastPathComponent())) end repeat --sArray's removeAllObjects() --aArray が sArray に依存? 削除できず return aArray as list --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFileNameWithinAFolderWithRecursive ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return aArray as list end try end retFileNameWithinAFolderWithRecursive --指定フォルダ以下のすべてのファイルフルパスを再帰で取得(文字列&拡張子 絞り込み なし) -- 実行結果[List] (子フォルダを含む)ファイルのフルパス on retFullPathWithinAFolderWithRecursive(theFolder) set aArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] ①「先頭のSortKey6桁」を削除  ②ファイルのフルパスを aArray に追加 -- sArray -> aArray [注] return aArray as list(NSMutableArray を List にキャスト) --set sArrayEnum to sArray's objectEnumerator() --enumerate for sArray 列挙子 repeat with fileObj in sArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 --set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --ファイルのフルパスを、要素として追加 (aArray's addObject:tempPath) end repeat --sArray's removeAllObjects() --aArray が sArray に依存? 削除できず return aArray as list --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFullPathWithinAFolderWithRecursive ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return aArray as list end try end retFullPathWithinAFolderWithRecursive --指定フォルダ以下のすべてのファイルを再帰で取得(拡張子1つのみで 絞り込み) -- 実行結果[List] (子フォルダを含む)フォルダのフルパス あるいは (拡張子なしの)ファイル名 on retFileNameWithinAFolderWithRecursiveFilterByExt(theFolder, theExt) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension == theExt" set thePred to NSPredicate's predicateWithFormat:"pathExtension == [c]%@" argumentArray:{theExt} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②「フォルダパス」+「拡張子を含むファイル名」に分割 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set bArrayEnum to bArray's objectEnumerator() --enumerate for bArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (bArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 --(bArray's addObject:(tempObj's lastPathComponent() as string)) --(拡張子を含めない)ファイル名を、要素として追加 (bArray's addObject:(tempObj's lastPathComponent()'s stringByDeletingPathExtension())) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFileNameWithinAFolderWithRecursiveFilterByExt ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFileNameWithinAFolderWithRecursiveFilterByExt --指定フォルダ以下のすべてのファイルを再帰で取得(拡張子1つのみで 絞り込み) -- 実行結果[List] ファイルのフルパス on retFullPathWithinAFolderWithRecursiveFilterByExt(theFolder, theExt) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension == theExt" set thePred to NSPredicate's predicateWithFormat:"pathExtension == [c]%@" argumentArray:{theExt} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②ファイルのフルパスを aArray に追加 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set aArrayEnum to aArray's objectEnumerator() --enumerate for aArray 列挙子 repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 set tempPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") set tempPath to (tempPath & tempObj's lastPathComponent()'s stringByDeletingPathExtension() as string) --ファイルの(拡張子を削除した)フルパスを、要素として追加 (bArray's addObject:tempPath) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFullPathWithinAFolderWithRecursiveFilterByExt ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFullPathWithinAFolderWithRecursiveFilterByExt --指定フォルダ以下のすべてのファイルを再帰で取得(拡張子リストで絞り込み) on retFileNameWithinAFolderWithRecursiveFilterByExtList(theFolder, extList) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension IN extList" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@" argumentArray:{extList} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②「フォルダパス」+「拡張子を含むファイル名」に分割 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set bArrayEnum to bArray's objectEnumerator() --enumerate for bArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (bArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 (bArray's addObject:(tempObj's lastPathComponent())) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ AppleScriptTask retFileNameWithinAFolderWithRecursiveFilterByExtList ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFileNameWithinAFolderWithRecursiveFilterByExtList --指定フォルダ以下のすべてのファイルを再帰で取得(拡張子リストで絞り込み) on retFullPathWithinAFolderWithRecursiveFilterByExtList(theFolder, extList) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension IN extList" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@" argumentArray:{extList} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②ファイルのフルパスを aArray に追加 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set aArrayEnum to aArray's objectEnumerator() --enumerate for aArray 列挙子 repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 --set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --ファイルのフルパスを、要素として追加 (bArray's addObject:tempPath) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ AppleScriptTask retFullPathWithinAFolderWithRecursiveFilterByExtList ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFullPathWithinAFolderWithRecursiveFilterByExtList --指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子で絞り込み)ファイル名が theKey を含む on retFileNameWithinAFolderWithRecursiveFilterByExtAndFileNameString(theFolder, theExt, theKey) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension == theExt" and "fileNameExt == *theKey*" set thePred to NSPredicate's predicateWithFormat:"pathExtension == [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{theExt, theKey} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②「フォルダパス」+「拡張子を含むファイル名」に分割 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set bArrayEnum to bArray's objectEnumerator() --enumerate for bArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (bArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 (bArray's addObject:(tempObj's lastPathComponent())) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFileNameWithinAFolderWithRecursiveFilterByExtAndFileNameString ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFileNameWithinAFolderWithRecursiveFilterByExtAndFileNameString --指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子で絞り込み)ファイル名が theKey を含む on retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString(theFolder, theExt, theKey) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension == theExt" and "fileNameExt == *theKey*" set thePred to NSPredicate's predicateWithFormat:"pathExtension == [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{theExt, theKey} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②ファイルのフルパスを aArray に追加 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set aArrayEnum to aArray's objectEnumerator() --enumerate for aArray 列挙子 repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 --set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --ファイルのフルパスを、要素として追加 (bArray's addObject:tempPath) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFullPathWithinAFolderWithRecursiveFilterByExtAndFileNameString ---指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子リストで絞り込み)ファイル名が theKey を含む on retFileNameWithinAFolderWithRecursiveFilterByExtListAndFileNameString(theFolder, extList, theKey) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension IN extList" and "fileNameExt == *theKey*" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{extList, theKey} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②「フォルダパス」+「拡張子を含むファイル名」に分割 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set bArrayEnum to bArray's objectEnumerator() --enumerate for bArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (bArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 (bArray's addObject:(tempObj's lastPathComponent())) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFileNameWithinAFolderWithRecursiveFilterByExtListAndFileNameString ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFileNameWithinAFolderWithRecursiveFilterByExtListAndFileNameString ---指定フォルダ以下のすべてのファイルを再帰で取得(文字列と拡張子リストで絞り込み)ファイル名が theKey を含む on retFullPathWithinAFolderWithRecursiveFilterByExtListAndFileNameString(theFolder, extList, theKey) set aArray to NSMutableArray's array() --FileFullPath 全要素削除すると、エラー発生(sArrayが依存?) set bArray to NSMutableArray's array() --FileFullPath => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] sArray 拡張子指定 => aArray "pathExtension IN extList" and "fileNameExt == *theKey*" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{extList, theKey} set aArray to sArray's filteredArrayUsingPredicate:thePred --コンストラクタ --return (aArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --[2] ①「先頭のSortKey6桁」を削除  ②ファイルのフルパスを aArray に追加 -- aArray -> bArray [注] return bArray as list(NSMutableArray を List にキャスト) --set aArrayEnum to aArray's objectEnumerator() --enumerate for aArray 列挙子 repeat with fileObj in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 --set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --ファイルのフルパスを、要素として追加 (bArray's addObject:tempPath) end repeat --return (bArray as list) --for Debug --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return (bArray as list) --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFullPathWithinAFolderWithRecursiveFilterByExtListAndFileNameString ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFullPathWithinAFolderWithRecursiveFilterByExtListAndFileNameString --指定フォルダ以下のすべてのファイルを再帰で取得(ファイル名に対し「正規表現」を利用して、絞り込み) -- 実行結果[List] (子フォルダを含む)フォルダのフルパス あるいは (拡張子ありの)ファイル名 -- (注) 最終結果を正規表現で単純抽出のため、該当ファイルZEROでも フォルダフルパスのみの行が出来てしまう -- つまり、ファイル無しでフォルダフルパスの行のみが出来る場合があるため、呼び出し側で除外して頂きたい! on retFileNameWithinAFolderWithRecursiveFilterByRegExp(theFolder, theRegExp) set aArray to NSMutableArray's array() --FolderFullPath or FileNameExt set bArray to NSMutableArray's array() --FolderFullPath or FileNameExt => Return as list --("Finder"相当の並び順を得るため)ファイルフルパスの先頭に「SortKey6桁」を付加 [注]ソート済み -- 実行結果[NSMutableArray] (子フォルダを含む)ファイルのフルパス set sArray to my retSortedArrayLikeFinderWithSortKey(theFolder) try --[1] ①「先頭のSortKey6桁」を削除  ②ファイルフルパス -- sArray -> aArray [注] return aArray as list(NSMutableArray を List にキャスト) --set sArrayEnum to sArray's objectEnumerator() --enumerate for sArray 列挙子 set oldDirPath to "" --フォルダパスが変更されたか? repeat with fileObj in sArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい set aFullPath to (fileObj as string) set tempPath to text 7 thru -1 of (aFullPath as string) --sortKey 6桁数字部分を削除 set tempObj to (NSString's stringWithString:tempPath) --NSString 生成 --取得したFolderPathの末尾に / が無いため、付加 (POSIX PATHルール) set newDirPath to ((tempObj's stringByDeletingLastPathComponent() as string) & "/") if (newDirPath ≠ oldDirPath) then (aArray's addObject:newDirPath) --フォルダパスを、要素として追加 set oldDirPath to newDirPath end if --(拡張子を含めた)ファイル名を、要素として追加 (aArray's addObject:(tempObj's lastPathComponent())) end repeat --sArray's removeAllObjects() --aArray が sArray に依存? 削除できず --[2] aArray 正規表現で抽出 => bArray "SELF MATCHES '正規表現文字列'" --末尾が / で終わるPOSIX PATHは Dir を示しているため、対象とする => (.*/+$) OR条件付加! --[注]エスケープシーケンスのため「 . ピリオド」は 通常 \\. 表記ですが、 --predicateWithFormatメソッドに問題があるため \\\\. つまり\4つ連続でないとエラー時あり! --set theRegExp to "(^(?!\\\\.).*$)" --「 . ピリオド」で始まる「隠しファイル」を除外 --set theRegExp to "(^(a).*\\.(css|js)+$)" --先頭 a で「末尾が .css 又は .js」 --set theRegExp to "(.*light.*\\.(css|js)+$)" --ファイル名に light を含む.css又は.js --Dirを示すパスは無条件対象とするため、OR条件で (.*/+$) を付加 --パラメータの theRegExp は(上記サンプルのように)グループ化のため、前後を括弧で閉じて下さい set regExpStr to (quoted form of ("(.*/+$)|" & theRegExp)) --最後に ' で囲む set thePred to NSPredicate's predicateWithFormat:("SELF MATCHES " & regExpStr) set bArray to aArray's filteredArrayUsingPredicate:thePred --コンストラクタ --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) return bArray as list --最終格納結果の NSMutableArray を List にキャストして、return on error display dialog "★ retFileNameWithinAFolderWithRecursiveFilterByRegExp ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return bArray as list end try end retFileNameWithinAFolderWithRecursiveFilterByRegExp --選択したフォルダ階層(子フォルダを含む)に含まれるフォルダフルパスと全ファイル名を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- POSIXフルパス指定のため、先頭と末尾に / が必要 ex) "/Users/username/Downloads/" -- 実行結果[0~n] (子フォルダを含む)フォルダのフルパス あるいは (拡張子付きの)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfChildFolders(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、「paramList=拡張子文字列のList」 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFileNameWithinAFolderWithRecursive(theFolderPath, paramList) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfChildFolders) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfChildFolders --選択したフォルダ階層(子フォルダを含む)に含まれるフォルダフルパスと全ファイル名を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] ファイル拡張子:"css"のように1つだけ指定指定(2つめ以降は無視) -- 実行結果[0~n] (子フォルダを含む)フォルダのフルパス あるいは (拡張子なしの)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfChildFoldersByExt(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、「paramList=拡張子文字列のList」 if (count paramList) > 0 then --ファイル拡張子:"css"のように1つだけ指定指定(2つめ以降は無視) set extStr to ((first item of paramList) as string) else set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --return returnText(拡張子指定がない場合) end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFileNameWithinAFolderWithRecursiveFilterByExt(theFolderPath, extStr) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfChildFoldersByExt) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfChildFoldersByExt --選択したフォルダ階層(子フォルダを含む)に含まれるフォルダフルパスと全ファイル名を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3~n] ファイル拡張子:"html" "css"のように複数指定可(1つでもOK) -- 実行結果[0~n] (子フォルダを含む)フォルダのフルパス あるいは (拡張子付きの)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfChildFoldersByExts(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、「paramList=拡張子文字列のList」 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFileNameWithinAFolderWithRecursiveFilterByExtList(theFolderPath, paramList) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfChildFoldersByExts) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfChildFoldersByExts --選択したフォルダ階層(子フォルダを含む)に含まれるフォルダフルパスと全ファイル名を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] keyString [fileNameExt contains keyString] ファイル名が keyStr を含む -- パラメータ[4~n] ファイル拡張子:"html" "css"のように複数指定可(1つでもOK) -- 実行結果[0~n] (子フォルダを含む)フォルダのフルパス あるいは ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfChildFoldersByExtsAndKey(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、3番目の要素は key文字列 [fileNameExt contains keyStr] if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --key文字列が有効でない場合 end if set keyStr to ((first item of paramList) as string) if keyStr = "" then --「ファイル名」に含まれるkey文字列 [fileNameExt contains keyStr] set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --key文字列が有効でない場合 end if set paramList to (rest of paramList) --最初の要素を削除 --1,2,3番目の要素を削除したため、「paramList=拡張子文字列のList」 if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --拡張子指定が有効でない場合 end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFileNameWithinAFolderWithRecursiveFilterByExtListAndFileNameString(theFolderPath, paramList, keyStr) -- [fileNameExt contains keyStr] ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfChildFoldersByExtsAndKey) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfChildFoldersByExtsAndKey --選択したフォルダ階層(子フォルダを含む)に含まれるフォルダフルパスと全ファイル名を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] 正規表現[文字列]: グループ化して利用するため、前後を ( ) で括ること! -- ex) "(^(a).*\\.(css|js)+$)" <= ファイル名が「 a で始まり、末尾が .css 又は .js 」 -- ex) "(.*light.*\\.(css|js)+$)" <= 「末尾が .css 又は .js で、light の文字列を含む」 -- 「拡張子を含むファイル名」を正規表現にて抽出 [注]エスケープシーケンスのため「 . ピリオド」は --  通常 \\. 表記ですが、NSPredicate に問題があるため \\\\. つまり\4つ連続でないとエラーの場合あり! -- 実行結果[0~n] (子フォルダを含む)フォルダのフルパス あるいは (拡張子付きの)ファイル名 -- (注) 最終結果を正規表現で単純抽出のため、該当ファイルZEROでも フォルダフルパスのみの行が出来てしまう -- つまり、ファイル無しでフォルダフルパスの行のみが出来る場合があるため、呼び出し側で除外して頂きたい! --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfChildFoldersByRegExp(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、3番目の要素は "(正規表現を表す文字列)" [注意] ( ) で括る事! if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --正規表現文字列が有効でない場合 end if set regExpStr to ((first item of paramList) as string) if regExpStr = "" then --グループ化して利用するため、前後を ( ) で括ること!ex) "(^(a).*)" set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --正規表現文字列が有効でない場合 end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFileNameWithinAFolderWithRecursiveFilterByRegExp(theFolderPath, regExpStr) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfChildFoldersByRegExp) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfChildFoldersByRegExp --選択したフォルダ階層(子フォルダを含む)に含まれるファイルのフルパスを取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- 実行結果[0~n] フォルダ階層(子フォルダを含む)に含まれるファイルの フルパス --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getPathListOfChildFolders(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFullPathWithinAFolderWithRecursive(theFolderPath) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getPathListOfChildFolders) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getPathListOfChildFolders --選択したフォルダ階層(子フォルダを含む)に含まれるファイルのフルパスを取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3~n] ファイル拡張子:"html" "css"のように複数指定可(拡張子1つでもOK) -- 実行結果[0~n] フォルダ階層(子フォルダを含む)に含まれるファイルの フルパス --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getPathListOfChildFoldersByExts(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!(子フォルダ対応)" --初期値 try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 --1,2番目の要素を削除したため、「paramList=拡張子文字列のList」 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set theFolderPath to (POSIX path of theFolder) --aliasを POSIX path(文字列)に変換 set stringList to {} -- 文字列リストを初期化 set stringList to my retFullPathWithinAFolderWithRecursiveFilterByExtList(theFolderPath, paramList) ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error --display dialog returnText --display dialog (count stringList) set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getPathListOfChildFoldersByExts) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getPathListOfChildFoldersByExts -- 選択ディレクトリの(POSIX)パス名+「(拡張子ありの)全ファイル名」を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子を含めた)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfFolder(paramStr) -- 拡張子を指定しないため、フォルダ内の全ファイルを返す set promptStr to "フォルダを選択してください!" --初期値 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) -- 最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) -- 最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theAlias to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theAlias to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- ==== Cocoa Script 開始 ==== -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set aArray to NSMutableArray's array() --FileFullPath 拡張子で抽出済み set bArray to NSMutableArray's array() --FolderFullPath / fileName => Return as list set theFolder to (POSIX path of theAlias) --aliasを POSIX path文字列に変換 set sArray to my retSortedArrayNotRecursive(theFolder) --get SortedArray, ParentDir Only! set isFirst to true -- 最初かどうか? --[1] aArray に フォルダパス あるいは(拡張子を含めない)ファイル名 を追加 repeat with aNSString in sArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい if isFirst then set newDirPath to (aNSString's stringByDeletingLastPathComponent() as string) (aArray's addObject:(newDirPath & "/")) --フォルダパスを aArray に追加 set isFirst to false end if --以下で、(拡張子を含めた)ファイル名を aArray に追加 (aArray's addObject:(aNSString's lastPathComponent())) end repeat set stringList to {} --文字列リストを初期化 set stringList to (aArray as list) --NSMutableArray bArray をキャストして、List へ格納 aArray's removeAllObjects() --bArray 全要素削除すると、エラー発生(sArrayが依存?) --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) -- ==== Cocoa Script 終了 ==== ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error -- display dialog returnText set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfFolder) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfFolder --選択ディレクトリの(POSIX)パス名+「(拡張子無しの)全ファイル名」を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] ファイル拡張子:"css"のように1つだけ指定指定(2つめ以降は無視) -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子無しの)ファイル名 --2021/08/31 「フィルタ参照」にて、指定拡張子のみ抽出 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfFolderByExt(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別しない set promptStr to "フォルダを選択してください!" --初期値 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) -- 最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) -- 最初の要素を削除 --1,2番目の要素を削除したため、「paramList=拡張子文字列のList」 if (count paramList) > 0 then --ファイル拡張子:"css"のように1つだけ指定指定(2つめ以降は無視) set extStr to ((first item of paramList) as string) else set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --return returnText(拡張子指定がない場合) end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theAlias to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theAlias to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- ==== Cocoa Script 開始 ==== -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set aArray to NSMutableArray's array() --FileFullPath 拡張子で抽出済み set bArray to NSMutableArray's array() --FolderFullPath / fileName => Return as list set theFolder to (POSIX path of theAlias) --aliasを POSIX path文字列に変換 set sArray to my retSortedArrayNotRecursive(theFolder) --get SortedArray, ParentDir Only! --[1] sArray 拡張子で抽出 => aArray "pathExtension == extStr" set thePred to NSPredicate's predicateWithFormat:"pathExtension == [c]%@" argumentArray:{extStr} set aArray to sArray's filteredArrayUsingPredicate:thePred --上記条件で抽出 set isFirst to true -- 最初かどうか? --[2] bArray に フォルダパス あるいは(拡張子を含めない)ファイル名 を追加 repeat with aNSString in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい if isFirst then set newDirPath to (aNSString's stringByDeletingLastPathComponent() as string) (bArray's addObject:(newDirPath & "/")) --フォルダパスを bArray に追加 set isFirst to false end if --以下で、(拡張子を含めた)ファイル名を bArray に追加 (bArray's addObject:(aNSString's lastPathComponent()'s stringByDeletingPathExtension())) end repeat set stringList to {} --文字列リストを初期化 set stringList to (bArray as list) --NSMutableArray bArray をキャストして、List へ格納 bArray's removeAllObjects() --bArray 全要素削除すると、エラー発生(bArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(aArrayが依存?) --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(bArrayが依存?) -- ==== Cocoa Script 終了 ==== ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error -- display dialog returnText set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfFolderByExt) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfFolderByExt --選択ディレクトリの(POSIX)パス名+「(拡張子付きの)全ファイル名」を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3~n] ファイル拡張子:"html" "css"のように複数指定可(拡張子1つでもOK) -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子を含めた)ファイル名 --2021/08/31 「フィルタ参照」にて、指定拡張子のみ抽出 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfFolderByExts(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別しない set promptStr to "フォルダを選択してください!" --初期値 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) -- 最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) -- 最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theAlias to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theAlias to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- ==== Cocoa Script 開始 ==== -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set aArray to NSMutableArray's array() --FileFullPath 拡張子で抽出済み set bArray to NSMutableArray's array() --FolderFullPath / fileName => Return as list set theFolder to (POSIX path of theAlias) --aliasを POSIX path文字列に変換 set sArray to my retSortedArrayNotRecursive(theFolder) --get SortedArray, ParentDir Only! --[1] sArray 拡張子で抽出 => aArray "pathExtension IN paramList" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@" argumentArray:{paramList} set aArray to sArray's filteredArrayUsingPredicate:thePred --上記条件で抽出 set isFirst to true -- 最初かどうか? --[2] bArray に フォルダパス あるいは(拡張子を含めない)ファイル名 を追加 repeat with aNSString in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい if isFirst then set newDirPath to (aNSString's stringByDeletingLastPathComponent() as string) (bArray's addObject:(newDirPath & "/")) --フォルダパスを bArray に追加 set isFirst to false end if --以下で、(拡張子を含めた)ファイル名を bArray に追加 (bArray's addObject:(aNSString's lastPathComponent())) end repeat set stringList to {} --文字列リストを初期化 set stringList to (bArray as list) --NSMutableArray bArray をキャストして、List へ格納 bArray's removeAllObjects() --bArray 全要素削除すると、エラー発生(bArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(aArrayが依存?) --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(bArrayが依存?) -- ==== Cocoa Script 終了 ==== ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error -- display dialog returnText set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfFolderByExts) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfFolderByExts --選択ディレクトリの(POSIX)パス名+「(拡張子付きの)全ファイル名」を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] keyString [fileNameExt contains keyString] ファイル名に keyString を含む -- パラメータ[4~n] ファイル拡張子:"html" "css"のように複数指定可(拡張子1つでもOK) -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子を含めた)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfFolderByExtsAndKey(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!" --初期値 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) -- 最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) -- 最初の要素を削除 --1,2番目の要素を削除したため、3番目の要素は key文字列 [fileNameExt contains keyStr] if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --key文字列が有効でない場合 end if set keyStr to ((first item of paramList) as string) if keyStr = "" then --「ファイル名」に含まれるkey文字列 [fileNameExt contains keyStr] set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --key文字列が有効でない場合 end if set paramList to (rest of paramList) --最初の要素を削除 --1,2,3番目の要素を削除したため、「paramList=拡張子文字列のList」 if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --拡張子指定が有効でない場合 end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theAlias to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theAlias to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- ==== Cocoa Script 開始 ==== -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set aArray to NSMutableArray's array() --FileFullPath 拡張子で抽出済み set bArray to NSMutableArray's array() --FolderFullPath / fileName => Return as list set theFolder to (POSIX path of theAlias) --aliasを POSIX path文字列に変換 set sArray to my retSortedArrayNotRecursive(theFolder) --get SortedArray, ParentDir Only! --[1] sArray 拡張子で抽出 => aArray "pathExtension IN paramList" set thePred to NSPredicate's predicateWithFormat:"pathExtension IN [c]%@ && lastPathComponent CONTAINS %@" argumentArray:{paramList, keyStr} set aArray to sArray's filteredArrayUsingPredicate:thePred --上記条件で抽出 set isFirst to true -- 最初かどうか? --[2] bArray に フォルダパス あるいは(拡張子を含めない)ファイル名 を追加 repeat with aNSString in aArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい if isFirst then set newDirPath to (aNSString's stringByDeletingLastPathComponent() as string) (bArray's addObject:(newDirPath & "/")) --フォルダパスを bArray に追加 set isFirst to false end if --以下で、(拡張子を含めた)ファイル名を bArray に追加 (bArray's addObject:(aNSString's lastPathComponent())) end repeat set stringList to {} --文字列リストを初期化 set stringList to (bArray as list) --NSMutableArray bArray をキャストして、List へ格納 bArray's removeAllObjects() --bArray 全要素削除すると、エラー発生(bArrayが依存?) --aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(aArrayが依存?) --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(bArrayが依存?) -- ==== Cocoa Script 終了 ==== ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error -- display dialog returnText set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfFolderByExtsAndKey) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfFolderByExtsAndKey --選択ディレクトリの(POSIX)パス名+「(拡張子付きの)全ファイル名」を取得 -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3] 正規表現[文字列]: グループ化して利用するため、前後を ( ) で括ること! -- ex) "(^(a).*\\.(css|js)+$)" <= ファイル名が「 a で始まり、末尾が .css 又は .js 」 -- ex) "(.*light.*\\.(css|js)+$)" <= 「末尾が .css 又は .js で、light の文字列を含む」 -- 「拡張子を含むファイル名」を正規表現にて抽出 [注]エスケープシーケンスのため「 . ピリオド」は --  通常 \\. 表記ですが、NSPredicate に問題があるため \\\\. つまり\4つ連続でないとエラーの場合あり! -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子を含めた)ファイル名 --2021/09/20 Ver.3.0 Cocoa AppleScript 対応 on getFileListOfFolderByRegExp(paramStr) --AppleScriptは文字列比較で大文字と小文字の文字を区別せず set promptStr to "フォルダを選択してください!" --初期値 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) --現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} --デリミタを変更 set paramList to (every text item of paramStr) --分割し、List格納 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then -- not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) -- 最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) -- 最初の要素を削除 --1,2番目の要素を削除したため、3番目の要素は "(正規表現を表す文字列)" [注意] ( ) で括る事! if (count paramList) < 1 then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --正規表現文字列が有効でない場合 end if set regExpStr to ((first item of paramList) as string) if regExpStr = "" then --正規表現文字列が有効でない場合 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --正規表現文字列が有効でない場合 end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theAlias to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theAlias to (choose folder with prompt promptStr default location defaultLocation) end if --選択フォルダの「エイリアス値」が返る ----------------------------------------------------------------------------------- -- ==== Cocoa Script 開始 ==== -- Cocoa Script ハンドラーを呼び出し!  フォルダ階層(子フォルダ)対応 set aArray to NSMutableArray's array() --FileFullPath 拡張子で抽出済み set bArray to NSMutableArray's array() --FolderFullPath / fileName => Return as list set theFolder to (POSIX path of theAlias) --aliasを POSIX path文字列に変換 set sArray to my retSortedArrayNotRecursive(theFolder) --get SortedArray, ParentDir Only! --[1] sArray に フォルダパス あるいは(拡張子を含めた)ファイル名 を追加 => aArray set isFirst to true -- 最初かどうか? repeat with aNSString in sArray --Cocoa ASの場合、列挙子を利用しても最速にならないらしい if isFirst then set newDirPath to (aNSString's stringByDeletingLastPathComponent() as string) (aArray's addObject:(newDirPath & "/")) --フォルダパスを aArray に追加 set isFirst to false end if --以下で、(拡張子を含めた)ファイル名を aArray に追加 (aArray's addObject:(aNSString's lastPathComponent())) end repeat --[2] aArray 拡張子で抽出 => bArray "pathExtension IN paramList" --Dirを示すパスは無条件対象とするため、OR条件で (.*/+$) を付加 --パラメータは(上記サンプルのように)グループ化のため、前後を ( ) で閉じて下さい set regExpStr to (quoted form of ("(.*/+$)|" & regExpStr)) --最後に ' で囲む set thePred to NSPredicate's predicateWithFormat:("SELF MATCHES " & regExpStr) set bArray to aArray's filteredArrayUsingPredicate:thePred --上記条件で抽出 if (bArray's |count|() < 2) then set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す set the clipboard to {} --クリップボードの初期化 return "" --bArray内の初要素はフォルダパスのため、該当ファイルが存在しない場合 end if set stringList to {} --文字列リストを初期化 set stringList to (bArray as list) --NSMutableArray bArray をキャストして、List へ格納 aArray's removeAllObjects() --aArray 全要素削除すると、エラー発生(bArrayが依存?) --bArray's removeAllObjects() --bArray 全要素削除すると、エラー発生(aArrayが依存?) --sArray's removeAllObjects() --sArray 全要素削除すると、エラー発生(aArrayが依存?) -- ==== Cocoa Script 終了 ==== ----------------------------------------------------------------------------------- set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error -- display dialog returnText set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp --保存したデリミタに戻す display dialog "★ AppleScriptTask (getFileListOfFolderByRegExp) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFileListOfFolderByRegExp -- 選択したファイルのパスを取得する(複数ファイルの選択不可) 「単一」ファイル選択ダイアログ -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3~n] UTI(Uniform Type Identifier):"css" "html"のように複数限定可(4つまで) --  厳密には「拡張子ではなくUTI限定」のため、無効だと全ファイルが選択可能となる! -- 実行結果[0] 指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1] (拡張子を含めた)ファイル名 on getFilePath(paramStr) set promptStr to "ファイルの選択(複数ファイルの選択不可)" -- フォルダ選択時のプロンプト文字列 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} -- デリミタを変更 set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then --not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set f to (choose file with prompt promptStr of type paramList) else set defaultLocation to (POSIX path of defaultLocation) set f to (choose file with prompt promptStr of type paramList default location defaultLocation) end if --選択したファイルの「エイリアス値」が返される tell application "Finder" set fDirHFS to (f's folder as string) -- (フォルダ)HFSパス set fDir to (POSIX path of fDirHFS) --(フォルダ) POSIXパス set fNameExt to (f's name as string) -- 拡張子付きファイル名 end tell set stringList to {} -- 文字列リストを初期化 set end of stringList to fDir -- フォルダフルパスを行追加 set end of stringList to fNameExt -- ファイル名を行追加 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (getFilePath) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFilePath -- 選択した(複数)ファイルのパスを取得する(複数ファイルの選択可能) 「複数」ファイル選択ダイアログ -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3~n] UTI(Uniform Type Identifier):"css" "html"のように複数限定可(4つまで) --  厳密には「拡張子ではなくUTI限定」のため、無効だと全ファイルが選択可能となる! -- 実行結果[0]  指定フォルダ(ディレクトリ)の POSIXフルパス -- 実行結果[1~n] (拡張子を含めた)ファイル名 on getMultiFilePath(paramStr) set multiFile to true set promptStr to "ファイルの選択(複数ファイルの選択可能)" -- フォルダ選択時のプロンプト文字列 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} -- デリミタを変更 set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then --not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set aliasList to (choose file with prompt promptStr of type paramList multiple selections allowed multiFile) else set defaultLocation to (POSIX path of defaultLocation) set aliasList to (choose file with prompt promptStr of type paramList default location defaultLocation multiple selections allowed multiFile) end if --選択したファイルの「エイリアス値」が返される set stringList to {} --文字列リストを初期化 set isFirst to true -- 最初かどうか? tell application "Finder" repeat with f in aliasList set fDirHFS to (f's folder as string) -- (フォルダ)HFSパス set fDir to (POSIX path of fDirHFS) -- (フォルダ)POSIXパス set fNameExt to (f's name as string) -- 拡張子付きファイル名 if (isFirst) then set end of stringList to fDir -- フォルダパスを行追加 set end of stringList to fNameExt --ファイル名を行追加 set isFirst to false else set end of stringList to fNameExt --ファイル名を行追加 end if end repeat end tell set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (getMultiFilePath) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getMultiFilePath -- 選択したフォルダのフルパスを取得する(複数フォルダの選択不可) 「単一」フォルダ選択ダイアログ -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- 実行結果 選択フォルダ(ディレクトリ)の POSIXフルパス on getFolderPath(paramStr) set promptStr to "フォルダの選択(複数フォルダの選択不可)" -- フォルダ選択時のプロンプト文字列 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} -- デリミタを変更 set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then --not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set theFolder to (choose folder with prompt promptStr) else set defaultLocation to (POSIX path of defaultLocation) set theFolder to (choose folder with prompt promptStr default location defaultLocation) end if --選択したファイルの「エイリアス値」が返される tell application "Finder" set fDirHFS to (theFolder as string) -- (フォルダ)HFSパス set fDir to (POSIX path of fDirHFS) --(フォルダ) POSIXパス end tell set stringList to {} -- 文字列リストを初期化 set end of stringList to fDir -- フォルダフルパスを行追加 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error set returnText to "" -- ハンドラーの戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (getFolderPath) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getFolderPath -- 選択した(複数)フォルダのフルパスを取得する(複数ファイルの選択可能) 「複数」フォルダ選択ダイアログ -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- 実行結果[0~n] 選択フォルダ(ディレクトリ)の POSIXフルパス on getMultiFolderPath(paramStr) set multiFolder to true set promptStr to "フォルダの選択(複数フォルダの選択可能)" -- フォルダ選択時のプロンプト文字列 set paramList to {} --パラメータのリスト try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} -- デリミタを変更 set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then --not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 activate --ウィンドウの前面に表示! if (defaultLocation = "") then set aliasList to (choose folder with prompt promptStr multiple selections allowed multiFolder) else set defaultLocation to (POSIX path of defaultLocation) set aliasList to (choose folder with prompt promptStr default location defaultLocation multiple selections allowed multiFolder) end if --選択したファイルの「エイリアス値」が返される set stringList to {} --文字列リストを初期化 tell application "Finder" repeat with aFolder in aliasList set fDirHFS to (aFolder as string) -- (フォルダ)HFSパス set fDir to (POSIX path of fDirHFS) -- (フォルダ)POSIXパス set end of stringList to fDir -- フォルダパスを行追加 end repeat end tell set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {CRLF}) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --クリップボードへ格納 set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to getTextWithDelimiters(stringList, {LF}) --ハンドラ-の戻り値へ 格納 set stringList to {} -- 文字列リストを初期化 on error set returnText to "" -- ハンドラ-の戻り値を初期化 set stringList to {} -- 文字列リストを初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (getMultiFolderPath) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getMultiFolderPath --ファイル存在チェック(引数のパスが有効で、ファイルパスかどうか?) --引数 thePath ( / から始まる)絶対パス --結果 ""「パスが無効 もしくは フォルダパス」の場合 又は(拡張子を含めた)ファイル名 on checkFilePath(thePath) set returnText to "" --ハンドラーの戻り値を初期化 try set myFM to NSFileManager's defaultManager() --Use for myFM's ~ set isDir to true --フォルダ? set validPath to false --(File or Folder)パスが有効か? --(reference) isDir ポインタ(参照)渡しで、値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:thePath isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else return "" --引数の「ファイルパス」が無効(存在しない)場合 end if if isDir then return "" --引数が「フォルダパス」の場合 else --isFile set aNSString to NSString's stringWithString:thePath return (aNSString's lastPathComponent() as string) --(拡張子を含めた)ファイル名 end if on error set returnText to "" -- ハンドラーの戻り値を初期化 display dialog "★ checkFilePath ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try return returnText --display dialog returnText end checkFilePath --フォルダ存在チェック(引数のパスが有効で、フォルダパスかどうか?) --引数 thePath ( / から始まる)絶対パス --結果 ""「パスが無効 もしくは ファイルパス」の場合 又は(末尾の)フォルダ名 on checkDirPath(thePath) set returnText to "" --ハンドラーの戻り値を初期化 try set myFM to NSFileManager's defaultManager() --Use for myFM's ~ set isDir to true --フォルダ? set validPath to false --(File or Folder)パスが有効か? (* if (text -1 of thePath) ≠ "/" then --not = は、/= と入力 set thePath to (thePath & "/") --末尾に "/" 無しでも、以下メソッド問題無し end if *) --(reference) isDir ポインタ(参照)渡しで、値が返る set {validPath, isDir} to (myFM's fileExistsAtPath:thePath isDirectory:(reference)) if (validPath as boolean) then set isDir to (isDir as boolean) --Dirならtrue、Fileならfalse else return "" --引数の「フォルダパス」が無効(存在しない)場合 end if if isDir then --引数が「フォルダパス」の場合 set aNSString to NSString's stringWithString:thePath return (aNSString's lastPathComponent() as string) --(末尾の)フォルダ名 else --isFile return "" --引数が「ファイルパス」の場合 end if on error set returnText to "" -- ハンドラーの戻り値を初期化 display dialog "★ checkDirPath ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try return returnText --display dialog returnText end checkDirPath --「choose file」で選択したファイルの UTI(Uniform Type Identifier) を取得 on getUtiByChooseFile(paramStr) try set theFile to choose file --get File's alias tell application "System Events" set theUTI to (type identifier of theFile) --UTI set theFileNameExt to (theFile's name) --File's nameExt end tell set returnText to (theFileNameExt & " => " & theUTI & " " & return) --Edit result return returnText on error return "" end try end getUtiByChooseFile --ファイルの書き出しルーチン「write_to_text_file_asUTF8」クリップボード又は「入力テキスト」を「出力」又は「追記」(無条件、ReWriteされる) --①UTF-8、TSVファイルで複数「列」なら「HT:Chr(09)」を挟む ②ファイルのフルパス(POSIX path、UTF-8) --③append(falseなら「出力:Createモード」、trueなら「追記:Appendモード」) --④newLineCode: 改行コード LF or CRLF (Appendモード時のみ、利用される) --Excel用TSVファイル(CRLF改行コード、タブ区切り「HT:Chr(09)」、セル内改行はLF)にて テスト済み --「Cocoa AppleScript」 on write_to_text_file_asUTF8(textData as text, filePath as text, append as boolean, newLineCode as text) try set beforeTextData to "" --初期化 --set mutableStr to NSMutableString's stringWithString:"" --NSMutableString's obj 生成(空文字列) set mutableStr to NSMutableString's stringWithCapacity:4096 --初期4096Byte(自動拡張)の Obj 生成 -- 書き込むテキストデータの形式:(複数「列」なら「水平タブ:HT」を挟む、UTF-8) if textData = "" then --クリップボードから「書き込むテキストデータ」を取得 if (clipboard info for «class utf8») is {} then -- «class utf8» or string(UTF-8テキストデータ) display dialog "★ write_to_text_file_asUTF8 ★" & return & return & "   クリップボードから、UTF-8テキストデータの取得【失敗】" buttons {"10秒で閉じます!"} with icon caution giving up after 10 error --クリップボードが空、又はテキスト形式以外 else set textData to (the clipboard as string) --(複数「列」は「水平タブ:HT」挿入、UTF-8) end if else --textDataのチェックは、一切なし(UTF-8テキストデータの検証は事前に行っておくこと) end if if (append and (checkFilePath(filePath) ≠ "")) then --Appendモードで、ファイルが存在する場合 set beforeTextData to (my read_text_file_asUTF8(filePath)) --UTF-8指定で、ファイル読み込み if beforeTextData ≠ "" then --空文字列の時は、何もしない mutableStr's appendString:beforeTextData --「読込テキストデータ」を追加 end if end if if beforeTextData = "" then --Createモード mutableStr's appendString:textData --クリップボード or AppleScriptTaskパラメータ else --Appnedモード mutableStr's appendString:(newLineCode & textData) --クリップボード or AppleScriptTaskパラ end if --atomically:true→データは一時ファイルに書き込まれ、エラーが発生しなければ指定パス名にリネーム! --つまり、ファイルの書き込みに失敗した場合、(既に同一ファイルが存在時)元のファイルのまま set theResult to mutableStr's writeToFile:filePath atomically:true encoding:NSUTF8StringEncoding |error|:(missing value) if (theResult as boolean) then return true else return false end if on error display dialog "★ write_to_text_file_asUTF8 ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return false end try end write_to_text_file_asUTF8 -- UTF-8エンコードのテキストデータをファイル書き込み(常に置換) --パラメータ区切りは、LF ではなく CRLF を利用!(Excel TSVファイルデータ対応) -- パラメータ[1]  (書き込む)ファイルのフルパス -- パラメータ[2]  Appendなら "append" 、Createなら "create" -- パラメータ[3]  改行コード "CRLF" 又は "LF" 又は "CR" -- パラメータ[4〜n]パラ3指定の改行コード区切りのUTF-8テキストデータ 連結してテキストデータとする -- 【注意】"" の場合は、クリップボード内のUTF-8テキストデータを書き込む -- 実行結果 書き込み(置き換え)エラー時は ""、 正常終了なら 書き込みファイルのファイル名 を返す --Excel用TSVファイル(CRLF改行コード、タブ区切り「HT:Chr(09)」、セル内改行はLF)にて テスト済み on putTextFileAsUTF8(paramStr) set returnText to "" -- ハンドラ-の戻り値を初期化 set theResult to false -- ハンドラ-の戻り値を初期化 try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {CRLF} -- デリミタを変更 【注】CRLF set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set filePath to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 set append to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 if append = "append" then set append to true else set append to false end if set newLineCode to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 if newLineCode is not in {"CRLF", "LF", "CR"} then error else if newLineCode = "CRLF" then set newLineCode to CRLF end if if newLineCode = "LF" then set newLineCode to LF end if if newLineCode = "CR" then set newLineCode to CR end if end if set textData to getTextWithDelimiters(paramList, CRLF) --データ行の区切り文字として、CRLF付加 --textData ""なら、クリップボード内のUTF-8テキストデータを書き込む set theResult to my write_to_text_file_asUTF8(textData, filePath, append, newLineCode) if theResult then set thePath to NSString's stringWithString:filePath --NSString オブジェクトを生成 set returnText to (thePath's lastPathComponent() as string) --プロパティで、ファイル名を取得 else set returnText to "" -- ハンドラ-の戻り値を初期化 end if on error set returnText to "" -- ハンドラ-の戻り値を初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (putTextFileAsUTF8) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end putTextFileAsUTF8 -- filePathファイル(UTF-8)を読み込み、そのデータ全体をクリップボード(UTF-8)へ格納「Cocoa AppleScript」 --戻り値にも格納(エラー時は、""の戻り値を返す) --Excel用TSVファイル(CRLF改行コード、タブ区切り「HT:Chr(09)」、セル内改行はLF)にて テスト済み on read_text_file_asUTF8(filePath as text) --POSIX path try set returnText to "" -- ハンドラ-の戻り値を初期化 if my checkFilePath(filePath) = "" then error --ファイルとして、パスが不正な場合 end if --テキストデータファイルを UTF-8 で読み込み set theNSString to NSString's stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding |error|:(missing value) --theNSString is instance of NSString(UTF-8) set returnText to (theNSString as string) --(UTF-8) set the clipboard to {} --クリップボードの初期化 set the clipboard to returnText --読み込みファイルデータを、クリップボードへ格納(UTF-8) return returnText --(UTF-8) on error display dialog "★ read_text_file_asUTF8 ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 return returnText --return "" end try end read_text_file_asUTF8 -- ファイル選択ダイアログで指定テキストデータをUTF-8で読み込み、データ全体を返す(クリップボードにもセット) -- パラメータ[1]  プロンプト文字列:""なら、当ハンドラー設定の初期値 -- パラメータ[2]  初期表示フォルダ:""なら、無指定(前回と同じフォルダを開く) -- パラメータ[3〜6]UTIを4つまで指定可能 ""(無指定)なら、"public.plain-text"を利用 -- 実行結果[0〜n] エラー時は""、 正常終了なら テキストファイル全体を文字列として返す(改行コードに注意) --Excel用TSVファイル(CRLF改行コード、タブ区切り「HT:Chr(09)」、セル内改行はLF)にて テスト済み on getTextFileAsUTF8(paramStr) set returnText to "" set promptStr to "テキストファイルの選択(単一選択)" -- ファイル選択時のプロンプト文字列 try set tmp to (AppleScript's text item delimiters) -- 現在のデリミタを保存 set (AppleScript's text item delimiters) to {LF} -- デリミタを変更 set paramList to (every text item of paramStr) -- 分割し、List格納 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す set str to ((first item of paramList) as string) if (str ≠ "") then --not=は「/=」と記述 set promptStr to str end if set paramList to (rest of paramList) --最初の要素を削除 set defaultLocation to ((first item of paramList) as string) set paramList to (rest of paramList) --最初の要素を削除 set utiStr to ((first item of paramList) as string) if utiStr = "" then set paramList to {"public.plain-text"} --テキストデータ用 UTI end if activate --ウィンドウの前面に表示! if (defaultLocation = "") then set f to (choose file with prompt promptStr of type paramList) else set defaultLocation to (POSIX path of defaultLocation) set f to (choose file with prompt promptStr of type paramList default location defaultLocation) end if --選択したファイルの「エイリアス値」が返される tell application "Finder" set filePath to (POSIX path of (f as string)) -- 指定ファイルのフルパス end tell set returnText to "" -- ハンドラ-の戻り値を初期化 set returnText to my read_text_file_asUTF8(filePath) --UTF-8で、クリップボードにもセット on error set returnText to "" -- ハンドラ-の戻り値を初期化 set (AppleScript's text item delimiters) to tmp -- 保存したデリミタに戻す display dialog "★ AppleScriptTask (getTextFileAsUTF8) ★" & return & return & "   キャンセル あるいは 異常終了" buttons {"10秒で閉じます!"} with icon caution giving up after 10 end try -- 「キャンセル」ボタン押下時用のエラーハンドリング return returnText -- display dialog returnText end getTextFileAsUTF8