001 | #!/usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | # ドロップレットとしても使えます |
---|
005 | # ファイル>書き出し>アプリケーション で書き出してください |
---|
006 | # リソースフォークと 拡張属性を削除してから圧縮します |
---|
007 | # |
---|
008 | # オリジナルのファイルをコピーしてからリソース削除するので |
---|
009 | # オリジナルのファイルを改変しないがテーマだったが |
---|
010 | # 結局macOS標準のZIPはUTF8だからWindows互換とはいかない失敗作 |
---|
011 | # com.cocolog-nifty.quicktimer.icefloe |
---|
012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
013 | use AppleScript version "2.6" |
---|
014 | use framework "Foundation" |
---|
015 | use framework "AppKit" |
---|
016 | use framework "UniformTypeIdentifiers" |
---|
017 | use scripting additions |
---|
018 | |
---|
019 | property refMe : a reference to current application |
---|
020 | property strBinPath : "/usr/bin/zip" |
---|
021 | property ocidChosenFileURLarray : missing value |
---|
022 | |
---|
023 | ########################## |
---|
024 | #ダイアログ本体 |
---|
025 | ########################## |
---|
026 | on appChooseFile:(ocidArgDirPathURL) |
---|
027 | # |
---|
028 | set ocidOpenPanel to refMe's NSOpenPanel's openPanel() |
---|
029 | ocidOpenPanel's setDirectoryURL:(ocidArgDirPathURL) |
---|
030 | ocidOpenPanel's setCanChooseFiles:(true) |
---|
031 | ocidOpenPanel's setCanChooseDirectories:(true) |
---|
032 | ocidOpenPanel's setAllowsMultipleSelection:(true) |
---|
033 | ocidOpenPanel's setAccessoryViewDisclosed:(true) |
---|
034 | ocidOpenPanel's setTitle:"ファイルを選択してください" |
---|
035 | ocidOpenPanel's setPrompt:"実行" |
---|
036 | ocidOpenPanel's setMessage:"ファイルかフォルダ選んでね" |
---|
037 | ocidOpenPanel's setShowsTagField:(true) |
---|
038 | ocidOpenPanel's setResolvesAliases:(false) |
---|
039 | ocidOpenPanel's setShowsHiddenFiles:(true) |
---|
040 | ocidOpenPanel's setExtensionHidden:(false) |
---|
041 | ocidOpenPanel's setCanCreateDirectories:(true) |
---|
042 | ocidOpenPanel's setCanDownloadUbiquitousContents:(true) |
---|
043 | ocidOpenPanel's setCanResolveUbiquitousConflicts:(true) |
---|
044 | ocidOpenPanel's setCanSelectHiddenExtension:(true) |
---|
045 | ocidOpenPanel's setTreatsFilePackagesAsDirectories:(true) |
---|
046 | #対象のUTIをてセット |
---|
047 | set ocidUTIarray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
048 | set ocidItemUTType to (refMe's UTType's typeWithIdentifier:("public.item")) |
---|
049 | (ocidUTIarray's addObject:(ocidItemUTType)) |
---|
050 | ocidOpenPanel's setAllowedContentTypes:(ocidUTIarray) |
---|
051 | #実行 |
---|
052 | set returnCode to ocidOpenPanel's runModal() |
---|
053 | #キャンセルをmissing value |
---|
054 | if returnCode = (refMe's NSModalResponseCancel) then |
---|
055 | log "キャンセル" |
---|
056 | error number -128 |
---|
057 | else if returnCode = (refMe's NSModalResponseOK) then |
---|
058 | log "OK" |
---|
059 | set my ocidChosenFileURLarray to ocidOpenPanel's |URLs|() |
---|
060 | |
---|
061 | end if |
---|
062 | end appChooseFile: |
---|
063 | |
---|
064 | ########################## |
---|
065 | #ダイアログ呼び出し |
---|
066 | ########################## |
---|
067 | on run |
---|
068 | tell current application |
---|
069 | set strName to name as text |
---|
070 | end tell |
---|
071 | ###スクリプトメニューから実行したら |
---|
072 | if strName is "osascript" then |
---|
073 | tell application "Finder" to activate |
---|
074 | else |
---|
075 | tell current application to activate |
---|
076 | end if |
---|
077 | ###デフォルトロケーション |
---|
078 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
079 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
080 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
---|
081 | ###ダイアログ呼び出し |
---|
082 | my performSelectorOnMainThread:("appChooseFile:") withObject:(ocidDesktopDirPathURL) waitUntilDone:(true) |
---|
083 | log "A" |
---|
084 | if ocidChosenFileURLarray = (missing value) then |
---|
085 | return "キャンセルしました" |
---|
086 | else |
---|
087 | log "B" |
---|
088 | set ocidOkPathURLArray to doSelectURL(ocidChosenFileURLarray) |
---|
089 | end if |
---|
090 | log "D" |
---|
091 | doSendArray2Exec(ocidOkPathURLArray) |
---|
092 | return |
---|
093 | end run |
---|
094 | |
---|
095 | ################### |
---|
096 | # ドロップ |
---|
097 | ################### |
---|
098 | on open listAliasFilePath |
---|
099 | set ocidURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
100 | repeat with itemAliasFilePath in listAliasFilePath |
---|
101 | set strItemFilePath to (POSIX path of itemAliasFilePath) as text |
---|
102 | set ocidItemFilePathStr to (refMe's NSString's stringWithString:(strItemFilePath)) |
---|
103 | set ocidItemFilePath to ocidItemFilePathStr's stringByStandardizingPath() |
---|
104 | set ocidItemFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidItemFilePath) isDirectory:false) |
---|
105 | (ocidURLArray's addObject:(ocidItemFilePathURL)) |
---|
106 | end repeat |
---|
107 | #パス判定に回す |
---|
108 | set ocidOkPathURLArray to doSelectURL(ocidURLArray) |
---|
109 | #次工程に送る |
---|
110 | doSendArray2Exec(argURLArray) |
---|
111 | return |
---|
112 | end open |
---|
113 | |
---|
114 | ################### |
---|
115 | # タスクへ送る |
---|
116 | ################### |
---|
117 | to doSendArray2Exec(argURLArray) |
---|
118 | repeat with itemFilePathURL in argURLArray |
---|
119 | log doExecCommand(itemFilePathURL) |
---|
120 | end repeat |
---|
121 | return |
---|
122 | end doSendArray2Exec |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | ################### |
---|
127 | # 対象URL判定 |
---|
128 | ################### |
---|
129 | to doSelectURL(argFileURLArray) |
---|
130 | log "C A" |
---|
131 | #戻し用のARRAY |
---|
132 | set ocidReturnURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
133 | ###ドロップの数だけ繰返し |
---|
134 | repeat with itemURL in argFileURLArray |
---|
135 | #URL判定 |
---|
136 | # ファイルとフォルダのみ iCloudを除外 |
---|
137 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsVolumeKey)) |
---|
138 | if boolURLIS is false then |
---|
139 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsAliasFileKey)) |
---|
140 | if boolURLIS is false then |
---|
141 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsSymbolicLinkKey)) |
---|
142 | if boolURLIS is false then |
---|
143 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsUbiquitousItemKey)) |
---|
144 | if boolURLIS is false then |
---|
145 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsRegularFileKey)) |
---|
146 | if boolURLIS is true then |
---|
147 | (ocidReturnURLArray's addObject:(itemURL)) |
---|
148 | end if |
---|
149 | set boolURLIS to doURLis(itemURL, (refMe's NSURLIsDirectoryKey)) |
---|
150 | if boolURLIS is true then |
---|
151 | (ocidReturnURLArray's addObject:(itemURL)) |
---|
152 | end if |
---|
153 | end if |
---|
154 | end if |
---|
155 | end if |
---|
156 | end if |
---|
157 | end repeat |
---|
158 | log "C B" |
---|
159 | return ocidReturnURLArray |
---|
160 | end doSelectURL |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | to doExecCommand(argFilePathURL) |
---|
165 | ################### |
---|
166 | #パス |
---|
167 | (* |
---|
168 | set strFilePath to (POSIX path of argFilePathURL) as text |
---|
169 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
---|
170 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath |
---|
171 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false) |
---|
172 | *) |
---|
173 | set ocidFilePathURL to argFilePathURL |
---|
174 | set strFilePath to ocidFilePathURL's |path| as text |
---|
175 | |
---|
176 | #ファイル名(またはディレクトリ名) |
---|
177 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
---|
178 | |
---|
179 | ################### |
---|
180 | #日付 |
---|
181 | set strDateno to doGetDateNo("yyyyMMddhhmm") |
---|
182 | |
---|
183 | ################### |
---|
184 | #UUID |
---|
185 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
---|
186 | set ocidUUIDString to ocidUUID's UUIDString |
---|
187 | #ARRAYにして |
---|
188 | set ocidUUIDArray to (ocidUUIDString's componentsSeparatedByString:("-")) |
---|
189 | #UUIDの最後の12桁を日付に入れ替える |
---|
190 | (ocidUUIDArray's replaceObjectAtIndex:(4) withObject:(strDateno)) |
---|
191 | set ocidUUIDString to (ocidUUIDArray's componentsJoinedByString:("-")) |
---|
192 | |
---|
193 | ################### |
---|
194 | #同階層に圧縮完了フォルダ |
---|
195 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
---|
196 | set strSetValue to ((ocidFileName as text) & "_圧縮済") as text |
---|
197 | set ocidDistDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strSetValue) isDirectory:(true)) |
---|
198 | set strDistDirPathURL to ocidDistDirPathURL's |path|() as text |
---|
199 | #フォルダ作っておく |
---|
200 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
201 | set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
---|
202 | (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)) |
---|
203 | set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidDistDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
---|
204 | |
---|
205 | ################### |
---|
206 | #出力関連ファイルパス |
---|
207 | set strSetZipFileName to ((ocidUUIDString as text) & ".zip") as text |
---|
208 | set ocidZipFilePathURL to (ocidDistDirPathURL's URLByAppendingPathComponent:(strSetZipFileName) isDirectory:(false)) |
---|
209 | set strZipFilePath to ocidZipFilePathURL's |path|() as text |
---|
210 | set strSetValue to ("設定内容.txt") as text |
---|
211 | set ocidReadMeFilePathURL to (ocidDistDirPathURL's URLByAppendingPathComponent:(strSetValue) isDirectory:(false)) |
---|
212 | set strReadMeFilePath to ocidReadMeFilePathURL's |path|() as text |
---|
213 | |
---|
214 | ################### |
---|
215 | #テンポラリ パス |
---|
216 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
---|
217 | set ocidSaveDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true) |
---|
218 | set strSaveDirPathURL to ocidSaveDirPathURL's |path|() as text |
---|
219 | |
---|
220 | ################### |
---|
221 | #フォルダを生成 |
---|
222 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
---|
223 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
---|
224 | |
---|
225 | ################### |
---|
226 | #テンポラリーにコピー |
---|
227 | #コピーする前にWINDOWSファイル名セーフを確認する |
---|
228 | set ocidSafeFileName to doSafeFileName(ocidFileName) |
---|
229 | #予約語だった場合は置換する |
---|
230 | set ocidSafeFileName to doReservedFileName(ocidSafeFileName) |
---|
231 | set ocidTempFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidSafeFileName)) |
---|
232 | # set listDone to (appFileManager's copyItemAtURL:(ocidFilePathURL) toURL:(ocidTempFilePathURL) |error| :(reference)) |
---|
233 | #DITTOを使うことにした |
---|
234 | set strTempFilePathURL to ocidTempFilePathURL's |path|() as text |
---|
235 | |
---|
236 | ################### |
---|
237 | #リソースフォーク削除 |
---|
238 | set strCommandText to ("/usr/bin/ditto --noextattr --norsrc --noacl --noqtn \"" & strFilePath & "\" \"" & strTempFilePathURL & "\"") as text |
---|
239 | log strCommandText |
---|
240 | set objDone to doExecTask(strCommandText) |
---|
241 | if objDone is false then |
---|
242 | return "dittoでエラーしました" |
---|
243 | end if |
---|
244 | |
---|
245 | ################### |
---|
246 | #拡張属性削除 |
---|
247 | set strCommandText to ("/usr/bin/xattr -cr \"" & strSaveDirPathURL & "\"") as text |
---|
248 | log strCommandText |
---|
249 | set objDone to doExecTask(strCommandText) |
---|
250 | if objDone is false then |
---|
251 | return "xattrでエラーしました" |
---|
252 | end if |
---|
253 | |
---|
254 | ################### |
---|
255 | #DOT_CLEAN |
---|
256 | set strCommandText to ("/usr/sbin/dot_clean -n -m -v \"" & strSaveDirPathURL & "\"") as text |
---|
257 | log strCommandText |
---|
258 | set objDone to doExecTask(strCommandText) |
---|
259 | if objDone is false then |
---|
260 | return "dot_cleanでエラーしました" |
---|
261 | end if |
---|
262 | |
---|
263 | ################### |
---|
264 | #FINDでDSファイル削除 |
---|
265 | set strCommandText to ("/usr/bin/find \"" & strSaveDirPathURL & "\" -name \".DS_Store\" -depth -exec rm {} \\;") as text |
---|
266 | log strCommandText |
---|
267 | set objDone to doExecTask(strCommandText) |
---|
268 | if objDone is false then |
---|
269 | log "findでエラーしました" |
---|
270 | end if |
---|
271 | |
---|
272 | ################### |
---|
273 | #パスワード用のUUID |
---|
274 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
---|
275 | set ocidUUIDString to ocidUUID's UUIDString |
---|
276 | set ocidOwnerPassword to (ocidUUIDString's stringByReplacingOccurrencesOfString:("-") withString:("")) |
---|
277 | set strOwnerPassword to ocidOwnerPassword as text |
---|
278 | |
---|
279 | ################### |
---|
280 | #圧縮 |
---|
281 | set strCommandText to ("" & strBinPath & " -j -X -e -P " & strOwnerPassword & " \"" & strZipFilePath & "\" -r \"" & strSaveDirPathURL & "\"") as text |
---|
282 | set strCommandText to ("" & strBinPath & " -s 0 -5 -o -j -X -e -P " & strOwnerPassword & " \"" & strZipFilePath & "\" -r \"" & strSaveDirPathURL & "\"") as text |
---|
283 | |
---|
284 | log strCommandText |
---|
285 | set objDone to doExecTask(strCommandText) |
---|
286 | if objDone is false then |
---|
287 | return "ZIPでエラーしました" |
---|
288 | end if |
---|
289 | |
---|
290 | ################### |
---|
291 | #ハッシュ |
---|
292 | set strCommandText to ("/usr/bin/openssl dgst -sha256 \"" & strZipFilePath & "\"") as text |
---|
293 | log strCommandText |
---|
294 | set objDone to doExecTask(strCommandText) |
---|
295 | if objDone is false then |
---|
296 | return "findでエラーしました" |
---|
297 | end if |
---|
298 | set ocidSTDoutArray to (objDone's componentsSeparatedByString:("=")) |
---|
299 | set ocidHASH to ocidSTDoutArray's lastObject() |
---|
300 | set ocidCharset to refMe's NSCharacterSet's alphanumericCharacterSet |
---|
301 | set ocidCharsetInvert to ocidCharset's invertedSet() |
---|
302 | set ocidHASH to (ocidHASH's stringByTrimmingCharactersInSet:(ocidCharsetInvert)) |
---|
303 | |
---|
304 | ################### |
---|
305 | # 出力テキスト |
---|
306 | set ocidOutputString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
---|
307 | (ocidOutputString's appendString:("先にお送りしました圧縮ファイル")) |
---|
308 | (ocidOutputString's appendString:("\n『")) |
---|
309 | (ocidOutputString's appendString:(strSetZipFileName)) |
---|
310 | (ocidOutputString's appendString:("』の\n\n")) |
---|
311 | (ocidOutputString's appendString:("解凍パスワードをお知らせします")) |
---|
312 | (ocidOutputString's appendString:("\n\n")) |
---|
313 | (ocidOutputString's appendString:(strOwnerPassword)) |
---|
314 | (ocidOutputString's appendString:("\n\n")) |
---|
315 | (ocidOutputString's appendString:("解凍後のファイル名は")) |
---|
316 | (ocidOutputString's appendString:("\n『")) |
---|
317 | (ocidOutputString's appendString:(ocidFileName)) |
---|
318 | (ocidOutputString's appendString:("』になります。\n\n")) |
---|
319 | (ocidOutputString's appendString:("解凍出来ない等ありましたらお知らせください。\n")) |
---|
320 | (ocidOutputString's appendString:("(パスワードをコピー&ペーストする際に\n")) |
---|
321 | (ocidOutputString's appendString:(" 改行やスペースが入らないように留意ください)\n")) |
---|
322 | (ocidOutputString's appendString:("\n----\n")) |
---|
323 | (ocidOutputString's appendString:("HASH: ")) |
---|
324 | (ocidOutputString's appendString:(ocidHASH)) |
---|
325 | (ocidOutputString's appendString:("\n\n")) |
---|
326 | (ocidOutputString's appendString:("Macでの確認方法: \n")) |
---|
327 | (ocidOutputString's appendString:("/usr/bin/openssl dgst -sha256 /Users/ユーザー名/Desktop/" & strSetZipFileName & "\"\n")) |
---|
328 | (ocidOutputString's appendString:("Windows: \n")) |
---|
329 | (ocidOutputString's appendString:("certutil -hashfile \"C:\\Users\\ユーザー名\\Desktop\\" & strSetZipFileName & "\" SHA256")) |
---|
330 | (ocidOutputString's appendString:("\n----\n")) |
---|
331 | (ocidOutputString's appendString:("Windows:解凍時のファイル名の文字バケ\n")) |
---|
332 | (ocidOutputString's appendString:("7Zipをご利用ください\n")) |
---|
333 | (ocidOutputString's appendString:("\thttps://www.7-zip.org/\n")) |
---|
334 | |
---|
335 | ################### |
---|
336 | #テキスト保存 |
---|
337 | set ocidSaveData to (ocidOutputString's dataUsingEncoding:(refMe's NSUTF8StringEncoding)) |
---|
338 | set ocidOption to refMe's NSDataWritingAtomic |
---|
339 | set boolDone to (ocidSaveData's writeToURL:(ocidReadMeFilePathURL) options:(ocidOption) |error| :(reference)) |
---|
340 | |
---|
341 | end doExecCommand |
---|
342 | |
---|
343 | |
---|
344 | |
---|
345 | |
---|
346 | ########################## |
---|
347 | # リソースキーチェック |
---|
348 | ########################## |
---|
349 | to doURLis(argFilePathURL, argNSURLResourceKey) |
---|
350 | #キーを格納するArray |
---|
351 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
352 | ocidKeyArray's addObject:(refMe's NSURLIsApplicationKey) |
---|
353 | ocidKeyArray's addObject:(refMe's NSURLIsDirectoryKey) |
---|
354 | ocidKeyArray's addObject:(refMe's NSURLIsAliasFileKey) |
---|
355 | ocidKeyArray's addObject:(refMe's NSURLIsRegularFileKey) |
---|
356 | ocidKeyArray's addObject:(refMe's NSURLIsPackageKey) |
---|
357 | ocidKeyArray's addObject:(refMe's NSURLIsVolumeKey) |
---|
358 | ocidKeyArray's addObject:(refMe's NSURLIsUbiquitousItemKey) |
---|
359 | ocidKeyArray's addObject:(refMe's NSURLIsSymbolicLinkKey) |
---|
360 | ocidKeyArray's addObject:(refMe's NSURLIsWritableKey) |
---|
361 | ocidKeyArray's addObject:(refMe's NSURLIsReadableKey) |
---|
362 | ocidKeyArray's addObject:(refMe's NSURLIsExecutableKey) |
---|
363 | ocidKeyArray's addObject:(refMe's NSURLIsUserImmutableKey) |
---|
364 | ocidKeyArray's addObject:(refMe's NSURLIsSystemImmutableKey) |
---|
365 | ocidKeyArray's addObject:(refMe's NSURLIsHiddenKey) |
---|
366 | ocidKeyArray's addObject:(refMe's NSURLIsExcludedFromBackupKey) |
---|
367 | ocidKeyArray's addObject:(refMe's NSURLIsPurgeableKey) |
---|
368 | ocidKeyArray's addObject:(refMe's NSURLIsSparseKey) |
---|
369 | ocidKeyArray's addObject:(refMe's NSURLIsMountTriggerKey) |
---|
370 | #リソース取得 |
---|
371 | set listResponse to (argFilePathURL's resourceValuesForKeys:(ocidKeyArray) |error| :(reference)) |
---|
372 | set ocidResourceValueDict to (item 1 of listResponse) |
---|
373 | #判定 |
---|
374 | set boolVolume to ocidResourceValueDict's valueForKey:(argNSURLResourceKey) |
---|
375 | #判定値を戻す |
---|
376 | if boolVolume = (missing value) then |
---|
377 | return false as boolean |
---|
378 | else |
---|
379 | return boolVolume as boolean |
---|
380 | end if |
---|
381 | |
---|
382 | end doURLis |
---|
383 | |
---|
384 | |
---|
385 | ########################## |
---|
386 | # ファイル名互換チェック |
---|
387 | ########################## |
---|
388 | to doSafeFileName(argFileName) |
---|
389 | #テキストかOCIDかわからないので一度テキストにして |
---|
390 | set strFileName to argFileName as text |
---|
391 | #その後でNSStringにする |
---|
392 | set ocidReplacedStrings to (refMe's NSString's stringWithString:(strFileName)) |
---|
393 | #置換テーブル |
---|
394 | set recordUnsupported to {|\\|:"_", |/|:"_", ||:":", _:"", |*|:"_", |?|:"_", |"|:"_", |<|:"_", |>|:"_"} as record |
---|
395 | set ocidUnsupportedDict to refMe's NSDictionary's dictionaryWithDictionary:(recordUnsupported) |
---|
396 | set ocidAllkey to ocidUnsupportedDict's allKeys() |
---|
397 | set numCntArray to ocidAllkey's |count|() |
---|
398 | #キーの数だけ繰り返し |
---|
399 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
---|
400 | #キーの取得 |
---|
401 | set ocidKey to (ocidAllkey's objectAtIndex:(itemNo)) |
---|
402 | #置換デーブルの値を取得 |
---|
403 | set ocidValue to (ocidUnsupportedDict's valueForKey:(ocidKey)) |
---|
404 | #文字列置換 |
---|
405 | set ocidReplacedStrings to (ocidReplacedStrings's stringByReplacingOccurrencesOfString:(ocidKey) withString:(ocidValue)) |
---|
406 | end repeat |
---|
407 | #OCIDで戻す場合 |
---|
408 | return ocidReplacedStrings |
---|
409 | #テキストで戻す場合 |
---|
410 | # return ocidReplacedStrings as text |
---|
411 | # |
---|
412 | |
---|
413 | end doSafeFileName |
---|
414 | |
---|
415 | |
---|
416 | ########################## |
---|
417 | # 日付取得 |
---|
418 | ########################## |
---|
419 | to doGetDateNo(strDateFormat) |
---|
420 | ################### |
---|
421 | #日付情報の取得 |
---|
422 | set ocidDate to current application's NSDate's |date|() |
---|
423 | ################### |
---|
424 | #日付のフォーマットを定義 |
---|
425 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
---|
426 | set ocidLocale to current application's NSLocale's localeWithLocaleIdentifier:("ja_JP_POSIX") |
---|
427 | ocidNSDateFormatter's setLocale:(ocidLocale) |
---|
428 | ocidNSDateFormatter's setDateFormat:(strDateFormat) |
---|
429 | ################### |
---|
430 | #適応してテキストで戻す |
---|
431 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate) |
---|
432 | set strDateAndTime to ocidDateAndTime as text |
---|
433 | return strDateAndTime |
---|
434 | end doGetDateNo |
---|
435 | |
---|
436 | ########################## |
---|
437 | # コマンド実行 |
---|
438 | ########################## |
---|
439 | to doExecTask(argCommandText) |
---|
440 | ################### |
---|
441 | #引数をテキストに確定 |
---|
442 | set strCommandText to argCommandText as text |
---|
443 | |
---|
444 | ################### |
---|
445 | #テンポラリ |
---|
446 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
447 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
---|
448 | |
---|
449 | ################### |
---|
450 | #ログファイル |
---|
451 | set strDateno to doGetDateNo("yyyyMMddHHmmss") |
---|
452 | set strLogFileName to ("scriptlog." & strDateno & ".log") as text |
---|
453 | set ocidLogFilePathURL to ocidTempDirURL's URLByAppendingPathComponent:(strLogFileName) isDirectory:(false) |
---|
454 | set ocidLogFilePath to ocidLogFilePathURL's |path|() |
---|
455 | |
---|
456 | ################## |
---|
457 | #ログファイル生成 |
---|
458 | set ocidNulString to refMe's NSString's alloc()'s init() |
---|
459 | set listDone to ocidNulString's writeToURL:(ocidLogFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
---|
460 | |
---|
461 | ################## |
---|
462 | #ログファイルのアクセス権 644 |
---|
463 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
464 | ocidAttrDict's setValue:(420) forKey:(refMe's NSFilePosixPermissions) |
---|
465 | set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidLogFilePath) |error| :(reference) |
---|
466 | |
---|
467 | ################## |
---|
468 | #実行時のカレント |
---|
469 | set ocidCurrentDirPathURL to ocidTempDirURL |
---|
470 | |
---|
471 | ################## |
---|
472 | #コマンド実行 |
---|
473 | set ocidComString to refMe's NSString's stringWithString:(strCommandText) |
---|
474 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
---|
475 | ocidTermTask's setLaunchPath:("/bin/zsh") |
---|
476 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
---|
477 | ocidArgumentsArray's addObject:("-c") |
---|
478 | ocidArgumentsArray's addObject:(ocidComString) |
---|
479 | ocidTermTask's setArguments:(ocidArgumentsArray) |
---|
480 | set ocidOutPut to refMe's NSPipe's pipe() |
---|
481 | set ocidError to refMe's NSPipe's pipe() |
---|
482 | ocidTermTask's setStandardOutput:(ocidOutPut) |
---|
483 | ocidTermTask's setStandardError:(ocidError) |
---|
484 | ocidTermTask's setCurrentDirectoryURL:(ocidCurrentDirPathURL) |
---|
485 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
---|
486 | if (item 1 of listDoneReturn) is (false) then |
---|
487 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
---|
488 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
---|
489 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
---|
490 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
---|
491 | end if |
---|
492 | |
---|
493 | ################## |
---|
494 | #終了待ち |
---|
495 | ocidTermTask's waitUntilExit() |
---|
496 | |
---|
497 | ################## |
---|
498 | #標準出力をログに |
---|
499 | set ocidOutPutData to ocidOutPut's fileHandleForReading() |
---|
500 | set listResponse to ocidOutPutData's readDataToEndOfFileAndReturnError:(reference) |
---|
501 | set ocidStdOut to (item 1 of listResponse) |
---|
502 | ##これが戻り値 |
---|
503 | set ocidStdOut to refMe's NSString's alloc()'s initWithData:(ocidStdOut) encoding:(refMe's NSUTF8StringEncoding) |
---|
504 | set listDone to ocidStdOut's writeToURL:(ocidLogFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
---|
505 | |
---|
506 | ################## |
---|
507 | #戻り値チェック |
---|
508 | set numReturnNo to ocidTermTask's terminationStatus() as integer |
---|
509 | #正常 |
---|
510 | if numReturnNo = 0 then |
---|
511 | return ocidStdOut |
---|
512 | else |
---|
513 | #エラー |
---|
514 | set ocidErrorData to ocidError's fileHandleForReading() |
---|
515 | set listResponse to ocidErrorData's readDataToEndOfFileAndReturnError:(reference) |
---|
516 | set ocidErrorOutData to (item 1 of listResponse) |
---|
517 | set ocidErrorOutString to refMe's NSString's alloc()'s initWithData:(ocidErrorOutData) encoding:(refMe's NSUTF8StringEncoding) |
---|
518 | set listResponse to refMe's NSFileHandle's fileHandleForWritingToURL:(ocidLogFilePathURL) |error| :(reference) |
---|
519 | set ocidReadHandle to (item 1 of listResponse) |
---|
520 | set listDone to ocidReadHandle's seekToEndReturningOffset:(0) |error| :(reference) |
---|
521 | log (item 1 of listDone) as boolean |
---|
522 | set listDone to ocidReadHandle's writeData:(ocidErrorOutData) |error| :(reference) |
---|
523 | log (item 1 of listDone) as boolean |
---|
524 | set listDone to ocidReadHandle's closeAndReturnError:(reference) |
---|
525 | log (item 1 of listDone) as boolean |
---|
526 | return false |
---|
527 | end if |
---|
528 | |
---|
529 | return false |
---|
530 | |
---|
531 | end doExecTask |
---|
532 | |
---|
533 | |
---|
534 | ############################## |
---|
535 | # 予約語置換 |
---|
536 | ############################## |
---|
537 | to doReservedFileName(argFileName) |
---|
538 | #テキストかOCIDかわからないので一度テキストにして |
---|
539 | set strFileName to argFileName as text |
---|
540 | #置換テーブル |
---|
541 | set listReserved to {"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "CON", "PRN", "AUX", "NUL"} as list |
---|
542 | set numCntArray to (count of every item of listReserved) as integer |
---|
543 | #キーの数だけ繰り返し |
---|
544 | repeat with itemNo from 1 to numCntArray by 1 |
---|
545 | #キーの取得 |
---|
546 | set strKey to (item itemNo of listReserved) |
---|
547 | #置換デーブルの値を取得 |
---|
548 | set strValue to ("_" & strKey) as text |
---|
549 | #文字列置換 |
---|
550 | if strFileName is strKey then |
---|
551 | set strFileName to strValue as text |
---|
552 | end if |
---|
553 | end repeat |
---|
554 | #OCIDで戻す場合 |
---|
555 | # return ocidReplacedStrings |
---|
556 | #テキストで戻す場合 |
---|
557 | return strFileName as text |
---|
558 | |
---|
559 | end doReservedFileName |
---|