[FolderAction]【自分用】スクリーンキャプチャ・フォルダアクション(リサイズから日本語OCRまで)修正
ダウンロード - screencap_folderaction.zip
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | #com.cocolog-nifty.quicktimer.icefloe |
004 | # トリガーとしてファイルのパスを受け取るが使わないパターン |
005 | # OCR処理にmissing value対策を追加 |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use framework "CoreImage" |
011 | use framework "VisionKit" |
012 | use framework "Vision" |
013 | use scripting additions |
014 | property refMe : a reference to current application |
015 | |
016 | |
017 | on run {argListFilePath} |
018 | #単体実行用 |
019 | #on run |
020 | |
021 | ########################### |
022 | #必要なフォルダ |
023 | set appFileManager to refMe's NSFileManager's defaultManager() |
024 | #Picturesフォルダ |
025 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask)) |
026 | set ocidPicturesDirPathURL to ocidURLsArray's firstObject() |
027 | set ocidAutomatorExportDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/AutomatorExport") isDirectory:(true)) |
028 | set ocidExportAutomatorDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/ExportAutomator") isDirectory:(true)) |
029 | set ocidExportSmallDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/ExportSmall") isDirectory:(true)) |
030 | set ocidExportTiffDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/ExportTiff") isDirectory:(true)) |
031 | set ocidPostImageDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/PostImage") isDirectory:(true)) |
032 | set ocidScreenCaptureDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/ScreenCapture") isDirectory:(true)) |
033 | set ocidTemporaryItemsDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/TemporaryItems") isDirectory:(true)) |
034 | set ocidWorkFileDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/WorkFile") isDirectory:(true)) |
035 | |
036 | |
037 | ########################### |
038 | #【1】ファイルの収集 |
039 | set appFileManager to refMe's NSFileManager's defaultManager() |
040 | set ocidOptions to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
041 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
042 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
043 | set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidScreenCaptureDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOptions) |error| :(reference) |
044 | if (item 2 of listResponse) = (missing value) then |
045 | set ocidScreenCaptureURLArray to (item 1 of listResponse) |
046 | log "【1】ファイルの収集" |
047 | else if (item 2 of listDone) ≠ (missing value) then |
048 | log ((item 2 of listDone)'s localizedDescription()) as text |
049 | return display alert "【1】ファイルの収集に失敗しました" |
050 | end if |
051 | |
052 | |
053 | ########################### |
054 | #【2】移動 |
055 | #収集したファイルを処理 |
056 | repeat with itemURL in ocidScreenCaptureURLArray |
057 | #拡張子 |
058 | set ocidExtensionName to itemURL's pathExtension() |
059 | #UUIDを取得して |
060 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
061 | set ocidUUIDString to ocidUUID's UUIDString |
062 | #移動先 |
063 | set ocidDistBaseFilePathURL to (ocidWorkFileDirPathURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:(false)) |
064 | #拡張子追加 |
065 | set ocidDistFilePathURL to (ocidDistBaseFilePathURL's URLByAppendingPathExtension:(ocidExtensionName)) |
066 | #移動 |
067 | set listDone to (appFileManager's moveItemAtURL:(itemURL) toURL:(ocidDistFilePathURL) |error| :(reference)) |
068 | if (item 1 of listDone) is true then |
069 | log "【2】移動しました" |
070 | else if (item 2 of listDone) ≠ (missing value) then |
071 | log ((item 2 of listDone)'s localizedDescription()) as text |
072 | return display alert "【2】ファイルの移動に失敗しました" |
073 | end if |
074 | end repeat |
075 | |
076 | ########################### |
077 | #【3】ファイルの収集 |
078 | set appFileManager to refMe's NSFileManager's defaultManager() |
079 | set ocidOptions to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
080 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
081 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
082 | set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidWorkFileDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOptions) |error| :(reference) |
083 | if (item 2 of listResponse) = (missing value) then |
084 | set ocidWorkFileURLArray to (item 1 of listResponse) |
085 | log "【3】ファイルの収集" |
086 | else if (item 2 of listDone) ≠ (missing value) then |
087 | log ((item 2 of listDone)'s localizedDescription()) as text |
088 | return display alert "【3】ファイルの収集に失敗しました" |
089 | end if |
090 | |
091 | ########################### |
092 | #【4】72ppiにする |
093 | repeat with itemURL in ocidWorkFileURLArray |
094 | # |
095 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
096 | set listReadData to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference)) |
097 | if (item 2 of listReadData) ≠ (missing value) then |
098 | log (item 2 of listReadStings)'s localizedDescription() as text |
099 | return display alert "【4】データの読み込みに失敗しました" |
100 | else |
101 | log "【4】データの読み込み" |
102 | set ocidReadData to (item 1 of listReadData) |
103 | end if |
104 | #NSIMAGEに読み込む |
105 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData)) |
106 | #サイズPT |
107 | set recordSize to ocidReadImage's |size|() |
108 | set numWidthPtSize to (recordSize's width) as number |
109 | set numHeightPtSize to (recordSize's height) as number |
110 | ##BitMapRepに変換 |
111 | set ocidReadImageRepArray to ocidReadImage's representations() |
112 | set ocidReadImageRep to ocidReadImageRepArray's firstObject() |
113 | ##ピクセルサイズ取得 |
114 | set numPixelsWidth to ocidReadImageRep's pixelsWide() |
115 | set numPixelsHeight to ocidReadImageRep's pixelsHigh() |
116 | #まず72ppiにする |
117 | set ocidPixelsSize to refMe's NSSize's NSMakeSize(numPixelsWidth, numPixelsHeight) |
118 | (ocidReadImageRep's setSize:(ocidPixelsSize)) |
119 | #保存オプション |
120 | set ocidSaveImageType to (refMe's NSBitmapImageFileTypePNG) |
121 | #カラーICC |
122 | set strIccFilePath to ("/System/Library/ColorSync/Profiles/sRGB Profile.icc") as text |
123 | set ocidIccFilePathStr to (refMe's NSString's stringWithString:(strIccFilePath)) |
124 | set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath |
125 | set ocidIccFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:(false)) |
126 | set ocidProfileData to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL)) |
127 | #保存オプション |
128 | set ocidPropertiesDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
129 | (ocidPropertiesDict's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
130 | (ocidPropertiesDict's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
131 | (ocidPropertiesDict's setObject:(ocidProfileData) forKey:(refMe's NSImageColorSyncProfileData)) |
132 | #イメージ変換 |
133 | set ocidSaveImageData to (ocidReadImageRep's representationUsingType:(ocidSaveImageType) |properties|:(ocidPropertiesDict)) |
134 | #保存 |
135 | set listDone to (ocidSaveImageData's writeToURL:(itemURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
136 | if (item 2 of listDone) ≠ (missing value) then |
137 | log (item 2 of listDone)'s localizedDescription() as text |
138 | return display alert "【4】データの保存に失敗しました" |
139 | else |
140 | set ocidReadData to (item 1 of listDone) |
141 | end if |
142 | end repeat |
143 | |
144 | ########################### |
145 | #【5】移動 |
146 | #収集したファイルを処理 |
147 | repeat with itemURL in ocidWorkFileURLArray |
148 | #ファイル名 |
149 | set ocidFileName to itemURL's lastPathComponent() |
150 | #移動先 |
151 | set ocidDistFilePathURL to (ocidExportAutomatorDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false)) |
152 | #移動 |
153 | set listDone to (appFileManager's moveItemAtURL:(itemURL) toURL:(ocidDistFilePathURL) |error| :(reference)) |
154 | if (item 1 of listDone) is true then |
155 | log "【5】移動しました" |
156 | else if (item 2 of listDone) ≠ (missing value) then |
157 | log ((item 2 of listDone)'s localizedDescription()) as text |
158 | return display alert "【5】ファイルの移動に失敗しました" |
159 | end if |
160 | end repeat |
161 | |
162 | |
163 | ########################### |
164 | #【6】ファイルの収集 |
165 | set appFileManager to refMe's NSFileManager's defaultManager() |
166 | set ocidOptions to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
167 | set ocidKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
168 | ocidKeyArray's addObject:(refMe's NSURLPathKey) |
169 | set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidExportAutomatorDirPathURL) includingPropertiesForKeys:(ocidKeyArray) options:(ocidOptions) |error| :(reference) |
170 | if (item 2 of listResponse) = (missing value) then |
171 | set ocidExportAutomatorURLArray to (item 1 of listResponse) |
172 | log "【6】ファイルの収集" |
173 | else if (item 2 of listDone) ≠ (missing value) then |
174 | log ((item 2 of listDone)'s localizedDescription()) as text |
175 | return display alert "【6】ファイルの収集に失敗しました" |
176 | end if |
177 | |
178 | |
179 | ########################### |
180 | #【7】 TIFF 出力 |
181 | #日付時間の取得 |
182 | set strDateNo to doGetDateNo("yyyyMMdd-hhmmss") as text |
183 | #カウンター |
184 | set numCntNo to 1 as integer |
185 | #本処理 |
186 | repeat with itemURL in ocidExportAutomatorURLArray |
187 | #データ読み込み |
188 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
189 | set listReadData to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference)) |
190 | if (item 2 of listReadData) ≠ (missing value) then |
191 | log (item 2 of listReadStings)'s localizedDescription() as text |
192 | return display alert "【7】データの読み込みに失敗しました" |
193 | else |
194 | log "【7】データの読み込み" |
195 | set ocidReadData to (item 1 of listReadData) |
196 | #NSIMAGEに読み込む |
197 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData)) |
198 | #BitMapRepに変換 |
199 | set ocidReadImageRepArray to ocidReadImage's representations() |
200 | set ocidReadImageRep to ocidReadImageRepArray's firstObject() |
201 | ##ピクセルサイズ取得 |
202 | set numPixelsWidth to ocidReadImageRep's pixelsWide() |
203 | set numPixelsHeight to ocidReadImageRep's pixelsHigh() |
204 | #ファイル名 |
205 | set strSaveFileName to (strDateNo & "-" & numCntNo & "_" & numPixelsWidth & "x" & numPixelsHeight) as text |
206 | set ocidTiffBasePathURL to (ocidExportTiffDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
207 | set ocidTiffFilePathURL to (ocidTiffBasePathURL's URLByAppendingPathExtension:("tiff")) |
208 | #NSDATAに変換 |
209 | set ocidPropertyDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
210 | (ocidPropertyDict's setObject:(refMe's NSTIFFCompressionNone) forKey:(refMe's NSImageCompressionMethod)) |
211 | (ocidPropertyDict's setObject:(refMe's NSNumber's numberWithFloat:(0)) forKey:(refMe's NSImageCompressionFactor)) |
212 | #TIFF 変換 |
213 | set ocidSaveImageType to (refMe's NSBitmapImageFileTypeTIFF) |
214 | set ocidSaveImageData to (ocidReadImageRep's representationUsingType:(ocidSaveImageType) |properties|:(ocidPropertyDict)) |
215 | #保存 |
216 | set listDone to (ocidSaveImageData's writeToURL:(ocidTiffFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
217 | if (item 2 of listDone) ≠ (missing value) then |
218 | log (item 2 of listDone)'s localizedDescription() as text |
219 | return display alert "【7】データの保存に失敗しました" |
220 | else |
221 | set ocidReadData to (item 1 of listDone) |
222 | end if |
223 | #カウントアップ |
224 | set numCntNo to numCntNo + 1 as integer |
225 | end if |
226 | end repeat |
227 | |
228 | |
229 | ########################### |
230 | #【8】 Small 出力 |
231 | #カウンター |
232 | set numCntNo to 1 as integer |
233 | #本処理 |
234 | repeat with itemURL in ocidExportAutomatorURLArray |
235 | #読み込み |
236 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
237 | set listReadData to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference)) |
238 | if (item 2 of listReadData) ≠ (missing value) then |
239 | log (item 2 of listReadStings)'s localizedDescription() as text |
240 | return display alert "【8】データの読み込みに失敗しました" |
241 | else |
242 | log "【8】データの読み込み" |
243 | set ocidReadData to (item 1 of listReadData) |
244 | end if |
245 | #NSIMAGEに読み込む |
246 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData)) |
247 | #BitMapRepに変換 |
248 | set ocidReadImageRepArray to ocidReadImage's representations() |
249 | set ocidReadImageRep to ocidReadImageRepArray's firstObject() |
250 | ##ピクセルサイズ取得 |
251 | set numPixelsWidth to ocidReadImageRep's pixelsWide() |
252 | set numPixelsHeight to ocidReadImageRep's pixelsHigh() |
253 | #半分サイズ(数値の丸めで1ピクセルの誤差を容認) |
254 | set numSmallWidth to (numPixelsWidth / 2) as integer |
255 | set numSmallHeight to (numPixelsHeight / 2) as integer |
256 | set ocidSmallSize to refMe's NSSize's NSMakeSize(numSmallWidth, numSmallHeight) |
257 | set ocidSmallRect to refMe's NSRect's NSMakeRect(0, 0, numSmallWidth, numSmallHeight) |
258 | #出力用イメージ NSBitmapImageRep |
259 | # samplesPerPixel |
260 | set intSPP to 4 as integer |
261 | # bitsPerSample |
262 | set intBPS to 8 as integer |
263 | # bytesPerRow |
264 | set intBPR to (numSmallWidth * intSPP) as integer |
265 | # bitsPerPixel |
266 | set intBPP to 32 as integer |
267 | # RGB系のカラースペース |
268 | set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace |
269 | # アルファあり |
270 | set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat |
271 | ##出力ピクセルサイズのブランクイメージ |
272 | set ocidArtBoardRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numSmallWidth) pixelsHigh:(numSmallHeight) bitsPerSample:(intBPS) samplesPerPixel:(intSPP) hasAlpha:(true) isPlanar:(false) colorSpaceName:(ocidColorSpaceName) bitmapFormat:(ocidBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(intBPP)) |
273 | #ArtBord |
274 | ### 初期化 CodeBase |
275 | refMe's NSGraphicsContext's saveGraphicsState() |
276 | ###Context |
277 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
278 | ###生成された画像でNSGraphicsContext初期化 |
279 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
280 | ##色を個別に指定する場合 値は0が暗 1が明 |
281 | set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0)) |
282 | ocidSetColor's |set|() |
283 | ###画像生成 |
284 | refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numSmallWidth), height:(numSmallHeight)}}) |
285 | #画像作成終了 |
286 | refMe's NSGraphicsContext's restoreGraphicsState() |
287 | #合成 リザイスでNSCompositeSourceOver |
288 | ### 初期化 |
289 | refMe's NSGraphicsContext's saveGraphicsState() |
290 | ###Context |
291 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep)) |
292 | ###NSGraphicsContext初期化 |
293 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
294 | ####NSCompositeSourceOver |
295 | set ocidDrawInRect to {origin:{x:(0), y:(0)}, |size|:{width:(numSmallWidth), Hight:(numSmallHeight)}} |
296 | set ocidFromRect to (refMe's NSZeroRect) |
297 | # set ocidOption to (refMe's NSCompositeSourceOver) |
298 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
299 | ## set ocidOption to (refMe's NSCompositeCopy) |
300 | (ocidReadImageRep's drawInRect:(ocidDrawInRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value)) |
301 | #画像作成終了 |
302 | refMe's NSGraphicsContext's restoreGraphicsState() |
303 | #保存オプション |
304 | set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
305 | (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
306 | (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
307 | ##保存 |
308 | set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
309 | #スモールの保存先 |
310 | set strSmallFileName to (strDateNo & "-" & numCntNo & "_" & numSmallWidth & "x" & numSmallHeight) as text |
311 | set ocidExportSmallBasePathURL to (ocidExportSmallDirPathURL's URLByAppendingPathComponent:(strSmallFileName) isDirectory:(false)) |
312 | set ocidExportSmallPathURL to (ocidExportSmallBasePathURL's URLByAppendingPathExtension:("png")) |
313 | |
314 | set ocidOption to (refMe's NSDataWritingAtomic) |
315 | set listDone to (ocidNSInlineData's writeToURL:(ocidExportSmallPathURL) options:(ocidOption) |error| :(reference)) |
316 | if (item 1 of listDone) is true then |
317 | log "【8】保存しました" |
318 | else if (item 2 of listDone) ≠ (missing value) then |
319 | log ((item 2 of listDone)'s localizedDescription()) as text |
320 | return display alert "Automator【8】保存に失敗しました" |
321 | end if |
322 | #カウントアップ |
323 | set numCntNo to numCntNo + 1 as integer |
324 | end repeat |
325 | |
326 | |
327 | ########################### |
328 | #【9】OCR作成して移動 |
329 | #カウンター |
330 | set numCntNo to 1 as integer |
331 | #収集したファイルを処理 |
332 | repeat with itemURL in ocidExportAutomatorURLArray |
333 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
334 | set listReadData to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference)) |
335 | if (item 2 of listReadData) ≠ (missing value) then |
336 | log (item 2 of listReadStings)'s localizedDescription() as text |
337 | return display alert "【9】データの読み込みに失敗しました" |
338 | else |
339 | log "【9】データの読み込み" |
340 | set ocidReadData to (item 1 of listReadData) |
341 | end if |
342 | #OCRを先に処理 |
343 | #最小フォントサイズ |
344 | set numMinPt to 8 as integer |
345 | #####ImageRep画像の高さを取得 |
346 | ##画像に対しての8Ptテキストの比率--> MinimumTextHeightにセットする |
347 | set ocidImageRep to (refMe's NSBitmapImageRep's imageRepWithData:(ocidReadData)) |
348 | ##ピクセルサイズ取得 |
349 | set numPixelsWidth to ocidReadImageRep's pixelsWide() |
350 | set numPixelsHeight to ocidReadImageRep's pixelsHigh() |
351 | set intTextHeight to (numMinPt / numPixelsHeight) as number |
352 | #####OCR |
353 | #VNImageRequestHandler's |
354 | set ocidHandler to (refMe's VNImageRequestHandler's alloc()'s initWithData:(ocidReadData) options:(missing value)) |
355 | #VNRecognizeTextRequest's |
356 | set ocidRequest to refMe's VNRecognizeTextRequest's alloc()'s init() |
357 | set ocidOption to (refMe's VNRequestTextRecognitionLevelAccurate) |
358 | (ocidRequest's setRecognitionLevel:(ocidOption)) |
359 | (ocidRequest's setMinimumTextHeight:(intTextHeight)) |
360 | (ocidRequest's setAutomaticallyDetectsLanguage:(true)) |
361 | (ocidRequest's setRecognitionLanguages:{"en", "ja"}) |
362 | (ocidRequest's setUsesLanguageCorrection:(false)) |
363 | #results |
364 | (ocidHandler's performRequests:(refMe's NSArray's arrayWithObject:(ocidRequest)) |error| :(reference)) |
365 | set ocidResponseArray to ocidRequest's results() |
366 | #戻り値を格納するテキスト |
367 | set ocidFirstOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
368 | set ocidSecondOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
369 | set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0)) |
370 | #missing value対策 |
371 | if ocidResponseArray = (missing value) then |
372 | (ocidFirstOpinionString's appendString:("テキストは認識されませんでした")) |
373 | (ocidSecondOpinionString's appendString:("テキストは認識されませんでした")) |
374 | else |
375 | #戻り値の数だけ々 |
376 | repeat with itemArray in ocidResponseArray |
377 | #候補数指定 1−10 ここでは2種の候補を戻す指定 |
378 | set ocidRecognizedTextArray to (itemArray's topCandidates:(2)) |
379 | set ocidFirstOpinion to ocidRecognizedTextArray's firstObject() |
380 | set ocidSecondOpinion to ocidRecognizedTextArray's lastObject() |
381 | (ocidFirstOpinionString's appendString:(ocidFirstOpinion's |string|())) |
382 | (ocidFirstOpinionString's appendString:("\r")) |
383 | (ocidSecondOpinionString's appendString:(ocidSecondOpinion's |string|())) |
384 | (ocidSecondOpinionString's appendString:("\r")) |
385 | end repeat |
386 | end if |
387 | ##比較して相違点を探すならここで |
388 | (ocidSaveString's appendString:("-----第1候補\r\r")) |
389 | (ocidSaveString's appendString:(ocidFirstOpinionString)) |
390 | (ocidSaveString's appendString:("\r\r-----第2候補\r\r")) |
391 | (ocidSaveString's appendString:(ocidSecondOpinionString)) |
392 | ## |
393 | set strOCRFileName to (strDateNo & "-" & numCntNo & "_" & numPixelsWidth & "x" & numPixelsHeight) as text |
394 | set ocidAutomatorExportBasePathURL to (ocidAutomatorExportDirPathURL's URLByAppendingPathComponent:(strOCRFileName) isDirectory:(false)) |
395 | set ocidOcrSaveFilePathURL to (ocidAutomatorExportBasePathURL's URLByAppendingPathExtension:("txt")) |
396 | #保存 |
397 | set listDone to (ocidSaveString's writeToURL:(ocidOcrSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)) |
398 | if (item 1 of listDone) is true then |
399 | log "【9】正常終了" |
400 | else if (item 1 of listDone) is false then |
401 | log (item 2 of listDone)'s localizedDescription() as text |
402 | return display alert " 【9】 OCR保存に失敗しました" |
403 | end if |
404 | set ocidImageRep to "" |
405 | set ocidReadData to "" |
406 | set ocidMoveFilePathURL to (ocidAutomatorExportBasePathURL's URLByAppendingPathExtension:("png")) |
407 | set appFileManager to refMe's NSFileManager's defaultManager() |
408 | set listDone to (appFileManager's moveItemAtURL:(itemURL) toURL:(ocidMoveFilePathURL) |error| :(reference)) |
409 | if (item 1 of listDone) is true then |
410 | log "【9】移動しました" |
411 | else if (item 2 of listDone) ≠ (missing value) then |
412 | log ((item 2 of listDone)'s localizedDescription()) as text |
413 | return display alert "Automator【9】移動に失敗しました" |
414 | end if |
415 | #カウントアップ |
416 | set numCntNo to numCntNo + 1 as integer |
417 | |
418 | |
419 | end repeat |
420 | |
421 | |
422 | end run |
423 | |
424 | |
425 | ################################ |
426 | # 日付 doGetDateNo() |
427 | ################################ |
428 | to doGetDateNo(strDateFormat) |
429 | ####日付情報の取得 |
430 | set ocidDate to current application's NSDate's |date|() |
431 | ###日付のフォーマットを定義 |
432 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
433 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
434 | ocidNSDateFormatter's setDateFormat:strDateFormat |
435 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
436 | set strDateAndTime to ocidDateAndTime as text |
437 | return strDateAndTime |
438 | end doGetDateNo |
AppleScriptで生成しました |
| 固定リンク