[リネーム] ファイル名の中に含まれる連続した数字をゼロサプレスにしてリネーム
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* アプリケーションのインストール調査 v1 |
004 | ファイル名の数列をとにかくゼロ埋めして |
005 | bashでファイル名順にならぶようにする |
006 | |
007 | フォルダを選択して |
008 | フォルダの中のファイル名を一括返却します |
009 | 例 |
010 | |
011 | TEXT_F1_S2_E4.csv |
012 | ↓ |
013 | TEXT_F00001_S00002_E00004.csv |
014 | のように |
015 | ファイル名の中の連続する数字をゼロサプレスします。 |
016 | |
017 | com.cocolog-nifty.quicktimer.icefloe *) |
018 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
019 | use AppleScript version "2.8" |
020 | use framework "Foundation" |
021 | use framework "UniformTypeIdentifiers" |
022 | use framework "AppKit" |
023 | use scripting additions |
024 | property refMe : a reference to current application |
025 | |
026 | ############################# |
027 | #設定項目 リーディング0の桁数 |
028 | set strAddZeroNo to ("00000") as text |
029 | |
030 | ############################# |
031 | #ダイアログ |
032 | set strName to (name of current application) as text |
033 | if strName is "osascript" then |
034 | tell application "SystemUIServer" to activate |
035 | else |
036 | tell current application to activate |
037 | end if |
038 | #デスクトップ |
039 | set appFileManager to refMe's NSFileManager's defaultManager() |
040 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
041 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
042 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
043 | set strMes to "フォルダを選んでください" as text |
044 | set strPrompt to "フォルダを選択してください" as text |
045 | try |
046 | tell application "SystemUIServer" |
047 | activate |
048 | set aliasResponse to (choose folder strMes with prompt strPrompt default location aliasDesktopDirPath with invisibles and showing package contents without multiple selections allowed) as alias |
049 | end tell |
050 | on error |
051 | log "エラーしました" |
052 | return "エラーしました" |
053 | end try |
054 | |
055 | ############################# |
056 | #コンテンツ収集 |
057 | set strDirPath to (POSIX path of aliasResponse) as text |
058 | set ocidDirPathStr to refMe's NSString's stringWithString:(strDirPath) |
059 | set ocidDirPath to ocidDirPathStr's stringByStandardizingPath() |
060 | set ocidDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidDirPath) isDirectory:true) |
061 | # |
062 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
063 | set ocidKeyArray to refMe's NSArray's arrayWithArray:({(refMe's NSURLPathKey), (refMe's NSURLIsDirectoryKey), (refMe's NSURLIsSymbolicLinkKey)}) |
064 | set listSubPathResult to (appFileManager's contentsOfDirectoryAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOption) |error| :(reference)) |
065 | set ocidSubPathURLArray to item 1 of listSubPathResult |
066 | #一応ソートしてく |
067 | set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("absoluteString") ascending:(yes) selector:("localizedStandardCompare:") |
068 | set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor) |
069 | set ocidSubPathURLArray to ocidSubPathURLArray's sortedArrayUsingDescriptors:(ocidDescriptorArray) |
070 | ############################# |
071 | #リネーム用の正規表現 |
072 | set ocidOption to (refMe's NSRegularExpressionAnchorsMatchLines) |
073 | set listResponse to refMe's NSRegularExpression's regularExpressionWithPattern:("(d+)") options:(ocidOption) |error| :(reference) |
074 | set appRegEx to (item 1 of listResponse) |
075 | #置換用のゼロ テキストの初期化 |
076 | set ocidZeroString to refMe's NSMutableString's alloc()'s init() |
077 | #コンテンツを順番に処理 |
078 | repeat with itemFileUrl in ocidSubPathURLArray |
079 | #コンテナディレクトリ |
080 | set ocidContainerDirPathURL to itemFileUrl's URLByDeletingLastPathComponent() |
081 | #ファイル名 |
082 | set ocidFileName to itemFileUrl's lastPathComponent() |
083 | #コピーして |
084 | set ocidDistFileName to ocidFileName's mutableCopy() |
085 | #文字数数えて |
086 | set ocidLegth to ocidFileName's |length|() |
087 | #レンジにして |
088 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidLegth) |
089 | #正義表現で数値にマッチした範囲をリストにする |
090 | set ocidMachArray to (appRegEx's matchesInString:(ocidFileName) options:(0) range:(ocidRange)) |
091 | #マッチした個数のカウント |
092 | set numCntArray to ocidMachArray's |count|() |
093 | #置換のリピートは後ろから |
094 | repeat with itemIntNo from (numCntArray - 1) to 0 by -1 |
095 | #ゼロテキストを初期化 |
096 | (ocidZeroString's setString:(strAddZeroNo)) |
097 | #ゼロの文字数のレンジ |
098 | set numAddZero to ocidZeroString's |length|() |
099 | #マッチしたレコードを取り出して |
100 | set ocidItemMach to (ocidMachArray's objectAtIndex:(itemIntNo)) |
101 | #レンジにする |
102 | set ocidMachRange to ocidItemMach's range() |
103 | #マッチしたレンジで文字列(数字)を取り出して |
104 | set ocidRangeString to (ocidFileName's substringWithRange:(ocidMachRange)) |
105 | #ゼロテキストの追加する |
106 | (ocidZeroString's appendString:(ocidRangeString)) |
107 | #ゼロと数字のレンジを数えて |
108 | set numTempLength to ocidZeroString's |length|() |
109 | #指定の桁数で後ろから取り出す |
110 | set ocidPddNo to (ocidZeroString's substringFromIndex:(numTempLength - numAddZero)) |
111 | #コピーしておいたファイル名のレンジの文字をゼロパディングした文字に置換 |
112 | set ocidDistFileName to (ocidDistFileName's stringByReplacingCharactersInRange:(ocidMachRange) withString:(ocidPddNo)) |
113 | end repeat |
114 | #マッチした数だけ処理が終わればそれが新しいファイル名なので |
115 | #リネーム先のファイルURLを生成しておく |
116 | set ocidDistFilePathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidDistFileName) isDirectory:(false)) |
117 | #リネーム用のファイルURLに移動=リネームする |
118 | set ListDone to (appFileManager's moveItemAtURL:(itemFileUrl) toURL:(ocidDistFilePathURL) |error| :(reference)) |
119 | |
120 | end repeat |
121 | |
122 | return |
AppleScriptで生成しました |
| 固定リンク