#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
#Automater用です
#保存先フォルダ等に指定があります(このままでは動作しないよ的な)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "CoreImage"
use framework "VisionKit"
use framework "Vision"
use scripting additions
property refMe : a reference to current application
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807
################
##フォルダも作るか?
property boolMakeDir : true
#on run {argListFilePath}
on run
#必要なフォルダがあれば作る
set appFileManager to refMe's NSFileManager's defaultManager()
#Picturesフォルダ
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidPicturesDirPathURL to ocidURLsArray's firstObject()
#テンポラリフォルダ
set ocidTempDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/TemporaryItems") isDirectory:(true))
#実寸保存先
set ocidSaveDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/AutomatorExport") isDirectory:(true))
#スモールの保存先
set ocidSmallDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/SmallExport") isDirectory:(true))
#TIFFの保存先
set ocidPostDirPathURL to (ocidPicturesDirPathURL's URLByAppendingPathComponent:("ScreenCapture/PostImage") isDirectory:(true))
if boolMakeDir is true then
set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions))
#
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidTempDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
#
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
#
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidSmallDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
#
set listBoolMakeDir to (appFileManager's createDirectoryAtURL:(ocidPostDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
end if
############################
#本処理
#【1】リネーム
#【2】72ppiでAutomatorExportに
#【3】半分のサイズをSmallExportに
#【4】OCRテキストをAutomatorExport
############################
# repeat with argAliasFilePath in argListFilePath
##入力ファイル
# set strFilePath to (POSIX path of argAliasFilePath) as text
set strFilePath to ("~/Pictures/ScreenCapture/ScreenCapture/screen.png") as text
set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath))
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
set ocidFileName to ocidFilePath's lastPathComponent()
set ocidExtensionName to ocidFilePathURL's pathExtension()
###########################
#【1】リネーム
###########################
#日付時間ファイル名
set strDateNo to doGetDateNo("yyyyMMdd-hhmmss")
set ocidMoveTmpBaseFilePathURL to (ocidTempDirPathURL's URLByAppendingPathComponent:(strDateNo) isDirectory:(false))
#拡張子
set ocidTmpFilePathURL to (ocidMoveTmpBaseFilePathURL's URLByAppendingPathExtension:(ocidExtensionName))
#移動
set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidTmpFilePathURL) |error|:(reference))
if (item 1 of listDone) is true then
log "【1】移動しました"
else if (item 2 of listDone) ≠ (missing value) then
log ((item 2 of listDone)'s localizedDescription()) as text
return display alert "Automatorファイルの移動に失敗しました"
end if
###########################
#【2】72ppiでAutomatorExportに
###########################
#####データ読み込み
set ocidOption to (refMe's NSDataReadingMappedIfSafe)
set listReadData to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidTmpFilePathURL) options:(ocidOption) |error|:(reference))
if (item 2 of listReadData) ≠ (missing value) then
log (item 2 of listReadStings)'s localizedDescription() as text
return "【4】データの読み込みに失敗しました"
else
set ocidReadData to (item 1 of listReadData)
end if
##NSIMAGEに読み込む
set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData))
#サイズPT
set recordSize to ocidReadImage's |size|()
set numWidthPtSize to (recordSize's width) as number
set numHeightPtSize to (recordSize's height) as number
##BitMapRepに変換
set ocidReadImageRepArray to ocidReadImage's representations()
set ocidReadImageRep to ocidReadImageRepArray's firstObject()
##ピクセルサイズ取得
set numPixelsWidth to ocidReadImageRep's pixelsWide()
set numPixelsHeight to ocidReadImageRep's pixelsHigh()
#解像度の比率
set numMag to (numPixelsWidth / numWidthPtSize) as integer
#解像度と同じなら
if numMag = 1 then
set strSaveFileName to (strDateNo & "-" & numPixelsWidth & "x" & numPixelsHeight) as text
else
set numWidthPtSize to (numWidthPtSize) as integer
set numHeightPtSize to (height of recordSize) as integer
set strSaveFileName to (strDateNo & "-" & numWidthPtSize & "x" & numHeightPtSize & "@" & numMag) as text
end if
#原寸として移動する
set ocidSaveBasePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
set ocidSaveFilePathURL to (ocidSaveBasePathURL's URLByAppendingPathExtension:(ocidExtensionName))
set listDone to (appFileManager's moveItemAtURL:(ocidTmpFilePathURL) toURL:(ocidSaveFilePathURL) |error|:(reference))
if (item 1 of listDone) is true then
log "【2】移動しました"
else if (item 2 of listDone) ≠ (missing value) then
log ((item 2 of listDone)'s localizedDescription()) as text
return display alert "Automator【2】移動に失敗しました"
end if
###########################
#【3】半分のサイズをSmallExportに
###########################
#まず72ppiにする
set ocidPixelsSize to refMe's NSMakeSize(numPixelsWidth, numPixelsHeight)
(ocidReadImageRep's setSize:(ocidPixelsSize))
#半分サイズ(数値の丸めで1ピクセルの誤差を容認)
set numSmallWidth to (numPixelsWidth / 2) as integer
set numSmallHeight to (numPixelsHeight / 2) as integer
set ocidSmallSize to refMe's NSMakeSize(numSmallWidth, numSmallHeight)
set ocidSmallRect to refMe's NSMakeRect(0, 0, numSmallWidth, numSmallHeight)
#出力用イメージ NSBitmapImageRep
# samplesPerPixel
set intSPP to 4 as integer
# bitsPerSample
set intBPS to 8 as integer
# bytesPerRow
set intBPR to (numSmallWidth * intSPP) as integer
# bitsPerPixel
set intBPP to 32 as integer
# RGB系のカラースペース
set ocidColorSpaceName to refMe's NSCalibratedRGBColorSpace
# アルファあり
set ocidBitmapFormat to refMe's NSAlphaFirstBitmapFormat
##出力ピクセルサイズのブランクイメージ
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))
###########################
#ArtBord
### 初期化 CodeBase
refMe's NSGraphicsContext's saveGraphicsState()
###Context
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
###生成された画像でNSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
##色を個別に指定する場合 値は0が暗 1が明
set ocidSetColor to (refMe's NSColor's colorWithSRGBRed:(1.0) green:(1.0) blue:(1.0) alpha:(0.0))
ocidSetColor's |set|()
###画像生成
refMe's NSRectFill({origin:{x:0, y:0}, |size|:{width:(numSmallWidth), height:(numSmallHeight)}})
#画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
###########################
#合成 リザイスでNSCompositeSourceOver
### 初期化
refMe's NSGraphicsContext's saveGraphicsState()
###Context
set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidArtBoardRep))
###NSGraphicsContext初期化
(refMe's NSGraphicsContext's setCurrentContext:(ocidContext))
####NSCompositeSourceOver
set ocidDrawInRect to {origin:{x:(0), y:(0)}, |size|:{width:(numSmallWidth), Hight:(numSmallHeight)}}
set ocidFromRect to (refMe's NSZeroRect)
# set ocidOption to (refMe's NSCompositeSourceOver)
set ocidOption to (refMe's NSCompositingOperationSourceOver)
## set ocidOption to (refMe's NSCompositeCopy)
(ocidReadImageRep's drawInRect:(ocidDrawInRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:1.0 respectFlipped:true hints:(missing value))
#画像作成終了
refMe's NSGraphicsContext's restoreGraphicsState()
###########################
#保存オプション
set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
(ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced))
(ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma))
##保存
set ocidNSInlineData to (ocidArtBoardRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty))
#スモールの保存先
set strSmallFileName to (strDateNo & "-" & numPixelsWidth & "x" & numSmallHeight) as text
set ocidSmallBasePathURL to (ocidSmallDirPathURL's URLByAppendingPathComponent:(strSmallFileName))
set ocidSmallFilePathURL to (ocidSmallBasePathURL's URLByAppendingPathExtension:(ocidExtensionName))
# set boolDone to (ocidNSInlineData's writeToURL:(ocidSmallFilePathURL) atomically:true)
set ocidOption to (refMe's NSDataWritingAtomic)
set listDone to (ocidNSInlineData's writeToURL:(ocidSmallFilePathURL) options:(ocidOption) |error|:(reference))
if (item 1 of listDone) is true then
log "【3】保存しました"
else if (item 2 of listDone) ≠ (missing value) then
log ((item 2 of listDone)'s localizedDescription()) as text
return display alert "Automator【3】保存に失敗しました"
end if
###########################
#【4】OCRテキストをAutomatorExport
###########################
set numMinPt to 8 as integer
#####ImageRep画像の高さを取得
##画像に対しての8Ptテキストの比率--> MinimumTextHeightにセットする
set ocidImageRep to (refMe's NSBitmapImageRep's imageRepWithData:(ocidReadData))
set numImageHeight to ocidImageRep's pixelsHigh()
set intTextHeight to (numMinPt / numImageHeight) as number
#####OCR
#VNImageRequestHandler's
set ocidHandler to (refMe's VNImageRequestHandler's alloc()'s initWithData:(ocidReadData) options:(missing value))
#VNRecognizeTextRequest's
set ocidRequest to refMe's VNRecognizeTextRequest's alloc()'s init()
set ocidOption to (refMe's VNRequestTextRecognitionLevelAccurate)
(ocidRequest's setRecognitionLevel:(ocidOption))
(ocidRequest's setMinimumTextHeight:(intTextHeight))
(ocidRequest's setAutomaticallyDetectsLanguage:(true))
(ocidRequest's setRecognitionLanguages:{"en", "ja"})
(ocidRequest's setUsesLanguageCorrection:(false))
#results
(ocidHandler's performRequests:(refMe's NSArray's arrayWithObject:(ocidRequest)) |error|:(reference))
set ocidResponseArray to ocidRequest's results()
#戻り値を格納するテキスト
set ocidFirstOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
set ocidSecondOpinionString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
set ocidSaveString to (refMe's NSMutableString's alloc()'s initWithCapacity:(0))
#戻り値の数だけ々
repeat with itemArray in ocidResponseArray
#候補数指定 1−10 ここでは2種の候補を戻す指定
set ocidRecognizedTextArray to (itemArray's topCandidates:(2))
set ocidFirstOpinion to ocidRecognizedTextArray's firstObject()
set ocidSecondOpinion to ocidRecognizedTextArray's lastObject()
(ocidFirstOpinionString's appendString:(ocidFirstOpinion's |string|()))
(ocidFirstOpinionString's appendString:("\n"))
(ocidSecondOpinionString's appendString:(ocidSecondOpinion's |string|()))
(ocidSecondOpinionString's appendString:("\n"))
end repeat
##比較して相違点を探すならここで
(ocidSaveString's appendString:("-----第1候補\n\n"))
(ocidSaveString's appendString:(ocidFirstOpinionString))
(ocidSaveString's appendString:("\n\n-----第2候補\n\n"))
(ocidSaveString's appendString:(ocidSecondOpinionString))
###保存
set ocidOcrBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false))
#拡張子
set ocidOcrFilePathURL to (ocidOcrBaseFilePathURL's URLByAppendingPathExtension:("txt"))
#保存
set listDone to (ocidSaveString's writeToURL:(ocidOcrFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
if (item 1 of listDone) is true then
log "【4】正常終了"
else if (item 1 of listDone) is false then
log (item 2 of listDone)'s localizedDescription() as text
return display alert " 【4】 保存に失敗しました"
end if
set ocidSaveString to ""
set ocidReadData to ""
set ocidNSInlineData to ""
set ocidReadImageRepArray to ""
set ocidReadImageRep to ""
# end repeat
end run
################################
# 日付 doGetDateNo()
################################
to doGetDateNo(strDateFormat)
####日付情報の取得
set ocidDate to current application's NSDate's |date|()
###日付のフォーマットを定義
set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidNSDateFormatter's setDateFormat:strDateFormat
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo