名前+連番でリネーム
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # ファイル名を内包されているフォルダ名+連番にリネーム |
005 | #com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use scripting additions |
010 | |
011 | property refMe : a reference to current application |
012 | |
013 | set appFileManager to refMe's NSFileManager's defaultManager() |
014 | |
015 | ############################# |
016 | ###設定項目 桁数だけ0を |
017 | ###ゼロパディング(ゼロサプレス)=数値の桁揃え |
018 | set strZeroSupp to ("000") as text |
019 | ############################# |
020 | ###設定項目 接尾語 ファイル名と連番の間 |
021 | set strDemText to ("@") as text |
022 | |
023 | ############################# |
024 | ###ダイアログ |
025 | tell current application |
026 | set strName to name as text |
027 | end tell |
028 | ####スクリプトメニューから実行したら |
029 | if strName is "osascript" then |
030 | tell application "Finder" to activate |
031 | else |
032 | tell current application to activate |
033 | end if |
034 | ###デフォルトロケーション |
035 | set appFileManager to refMe's NSFileManager's defaultManager() |
036 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
037 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
038 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
039 | |
040 | ###UTIリスト |
041 | set listUTI to {"public.item"} |
042 | |
043 | set strMes to ("ファイルを選んでくださいA") as text |
044 | set strPrompt to ("ファイルを選んでください\r名前+連番でリネームします") as text |
045 | try |
046 | ### ファイル選択時 |
047 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, multiple selections allowed and showing package contents) as list |
048 | on error |
049 | log "エラーしました" |
050 | return "エラーしました" |
051 | end try |
052 | if listAliasFilePath is {} then |
053 | log "エラーしました" |
054 | return "選んでください" |
055 | end if |
056 | |
057 | |
058 | ######################## |
059 | ##ダイアログ |
060 | ######################## |
061 | set strReadString to doGetPasteboard() |
062 | set aliasIconPath to doGetAppIconAliasFilePath("com.apple.finder") |
063 | #前面に |
064 | set strName to (name of current application) as text |
065 | if strName is "osascript" then |
066 | tell application "Finder" to activate |
067 | else |
068 | tell current application to activate |
069 | end if |
070 | # |
071 | set strTitle to ("入力してください") as text |
072 | set strMes to ("ファイル名の先頭になる語句を入力\nファイル名" & strDemText & "連番でリネームされます") as text |
073 | set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer) |
074 | if (gave up of recordResult) is true then |
075 | return "時間切れです" |
076 | else if (button returned of recordResult) is "キャンセル" then |
077 | return "キャンセルです" |
078 | else |
079 | set strReturnedText to (text returned of recordResult) as text |
080 | end if |
081 | |
082 | |
083 | |
084 | |
085 | ############################# |
086 | ###ファイルURLのみを格納するリスト |
087 | set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
088 | ###まずは全部のURLをArrayに入れる |
089 | repeat with itemAliasFilePath in listAliasFilePath |
090 | ######パス |
091 | set aliasFilePath to itemAliasFilePath as alias |
092 | ###UNIXパスにして |
093 | set strFilePath to (POSIX path of aliasFilePath) as text |
094 | ###Stringsに |
095 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
096 | ###パス確定させて |
097 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath |
098 | ###NSURLに |
099 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
100 | ###戻り値をリストに格納 |
101 | (ocidFilePathURLAllArray's addObject:(ocidFilePathURL)) |
102 | end repeat |
103 | |
104 | ###ファイルリストのソート |
105 | set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"localizedStandardCompare:") |
106 | (ocidFilePathURLAllArray's sortUsingDescriptors:{ocidSortDescriptor}) |
107 | |
108 | ############################# |
109 | ###リネーム |
110 | ###リストの数 objectAtIndexはゼロスタートなので1引く |
111 | set numCntArray to ((count of listAliasFilePath) - 1) as integer |
112 | ###リストの数だけ繰り返し |
113 | repeat with itemIntNo from 0 to numCntArray by 1 |
114 | set strItemNo to (itemIntNo + 1 ) as text |
115 | set strSeroSup to (strZeroSupp & strItemNo) as text |
116 | set strFileNO to (text -3 through -1 of strSeroSup) as text |
117 | ###元ファイルURL |
118 | set ocidItemPathURL to (ocidFilePathURLAllArray's objectAtIndex:(itemIntNo)) |
119 | ###拡張子取得 |
120 | set strExtension to (ocidItemPathURL's pathExtension()) as text |
121 | ###コンテナディレクトリ |
122 | set ocidContainerDirURL to ocidItemPathURL's URLByDeletingLastPathComponent() |
123 | ###ファイル名 |
124 | set strNewFileName to (strReturnedText & strDemText & strFileNO & "." & strExtension) |
125 | ###リネーム済みURLL |
126 | set ocidNewFilePathURL to (ocidContainerDirURL's URLByAppendingPathComponent:(strNewFileName) isDirectory:false) |
127 | ###リネーム |
128 | set listDone to (appFileManager's moveItemAtURL:(ocidItemPathURL) toURL:(ocidNewFilePathURL) |error| :(reference)) |
129 | end repeat |
130 | |
131 | |
132 | |
133 | |
134 | ######################## |
135 | ## クリップボードの中身取り出し |
136 | ######################## |
137 | on doGetPasteboard() |
138 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
139 | set ocidPastBoardTypeArray to appPasteboard's types |
140 | ###テキストがあれば |
141 | set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text" |
142 | if boolContain = true then |
143 | ###値を格納する |
144 | tell application "Finder" |
145 | set strReadString to (the clipboard as text) as text |
146 | end tell |
147 | ###Finderでエラーしたら |
148 | else |
149 | set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType" |
150 | if boolContain = true then |
151 | set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value) |
152 | set strReadString to ocidReadString as text |
153 | else |
154 | log "テキストなし" |
155 | set strReadString to "入力してください" as text |
156 | end if |
157 | end if |
158 | return strReadString |
159 | end doGetPasteboard |
160 | |
161 | |
162 | ######################## |
163 | #AppIconのパスを求める |
164 | ######################## |
165 | on doGetAppIconAliasFilePath(argBundleID) |
166 | # |
167 | set appFileManager to refMe's NSFileManager's defaultManager() |
168 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
169 | set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(argBundleID)) |
170 | if ocidAppBundle ≠ (missing value) then |
171 | set ocidAppPathURL to ocidAppBundle's bundleURL() |
172 | else if ocidAppBundle = (missing value) then |
173 | set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)) |
174 | end if |
175 | if ocidAppBundle ≠ (missing value) then |
176 | set ocidAppPathURL to ocidAppBundle's bundleURL() |
177 | else if ocidAppBundle = (missing value) then |
178 | set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(strBundleID)) |
179 | end if |
180 | if ocidAppPathURL = (missing value) then |
181 | tell application "Finder" |
182 | try |
183 | set aliasAppApth to (application file id strBundleID) as alias |
184 | on error |
185 | return "アプリケーションが見つかりませんでした" |
186 | end try |
187 | end tell |
188 | set strAppPath to POSIX path of aliasAppApth as text |
189 | set strAppPathStr to refMe's NSString's stringWithString:(strAppPath) |
190 | set strAppPath to strAppPathStr's stringByStandardizingPath() |
191 | set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:true |
192 | end if |
193 | ####ダイアログに指定アプリのアイコンを表示する |
194 | ###アイコン名をPLISTから取得 |
195 | set ocidPlistPathURL to ocidAppPathURL's URLByAppendingPathComponent:("Contents/Info.plist") isDirectory:false |
196 | set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithContentsOfURL:(ocidPlistPathURL) |
197 | set strIconFileName to (ocidPlistDict's valueForKey:("CFBundleIconFile")) as text |
198 | ###ICONのURLにして |
199 | set strPath to ("Contents/Resources/" & strIconFileName) as text |
200 | set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false |
201 | ###拡張子の有無チェック |
202 | set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text |
203 | if strExtensionName is "" then |
204 | set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns" |
205 | end if |
206 | ##-->これがアイコンパス |
207 | log ocidIconFilePathURL's absoluteString() as text |
208 | ###ICONファイルが実際にあるか?チェック |
209 | set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePathURL's |path|) |
210 | ###ICONがみつかない時用にデフォルトを用意する |
211 | if boolExists is false then |
212 | set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns" |
213 | else |
214 | set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias |
215 | end if |
216 | return aliasIconPath |
217 | end doGetAppIconAliasFilePath |
AppleScriptで生成しました |
| 固定リンク