20260527

[AppleScript]Kindleページ送り + キャプチャー見開き

[AppleScript]Kindleページ送り + キャプチャー見開き

NOTE記事一覧ですnote.com
 

【スクリプトエディタで開く】 |

Kindleキャプチャー見開きapplescript.scpt
ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004
005Kindleのページ送り
006日本語版用 見開き表示分ページ送り
007
008キャプチャー(自炊用)
009
010com.cocolog-nifty.quicktimer.icefloe *)
011----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
012use AppleScript version "2.8"
013use framework "Foundation"
014use framework "AppKit"
015use framework "CoreGraphics"
016use scripting additions
017property refMe : a reference to current application
018property ocidNotFound : a reference to 9.22337203685477E+18 + 5807
019
020#################################
021#総ページ数
022set numCntPage to (183) as integer
023## 1ページ(見開き)での滞留時間
024set numInterval to 4 as integer
025## ファイル名定義
026set strKeyName to ("ScreenCapture") as text
027## 1ページ(見開き)での滞留時間
028set numInterval to 4 as integer
029##   メニューバーPXサイズ
030set numMenuBarHpx to 33 as integer
031#################################
032#本処理
033########################
034# ダイアログ
035set strTitle to ("入力してください") as text
036set strMsg to ("メイン画面下に出る総ページ数") as text
037set strDefaultAnswer to (numCntPage) as text
038set strOK to ("OK") as text
039set strCancel to ("キャンセル") as text
040set listButtons to {strOK, strCancel} as list
041set strIconFilePath to ("/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns") as text
042set aliasIconFilePath to (POSIX file strIconFilePath) as alias
043try
044   tell application "System Events"
045      activate
046      set recordResponse to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listButtons default button strOK cancel button strCancel with icon aliasIconFilePath giving up after 60 without hidden answer) as record
047   end tell
048on error strErrorMes number numErrorNO
049   log strErrorMes & numErrorNO
050   return false
051end try
052set boolGaveUP to (gave up of recordResponse) as boolean
053set strButtonReturned to (button returned of recordResponse) as text
054try
055   tell application "System Events" to quit
056end try
057if recordResponse is false then
058   log "キャンセル終了"
059   return false
060else if true is boolGaveUP then
061   log "時間切れですやりなおしてください"
062   return false
063else if strCancel is strButtonReturned then
064   log "キャンセル終了"
065   return false
066end if
067
068set strResponse to (text returned of recordResponse) as text
069set ocidOrgStrings to (refMe's NSString's stringWithString:(strResponse))
070set ocidResponseString to (ocidOrgStrings's stringByReplacingOccurrencesOfString:(return) withString:(""))
071set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(linefeed) withString:(""))
072set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(tab) withString:(""))
073set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(space) withString:(""))
074##半角にして
075set ocidNumberString to ocidResponseString's stringByApplyingTransform:(refMe's NSStringTransformFullwidthToHalfwidth) |reverse|:(false)
076###数字以外の文字が
077set ocidDigitsCharSet to (refMe's NSCharacterSet's decimalDigitCharacterSet)
078set ocidNotDigitsCharSet to ocidDigitsCharSet's invertedSet()
079set ocidMatch to ocidNumberString's rangeOfCharacterFromSet:(ocidNotDigitsCharSet)
080set ocidLocation to ocidMatch's location()
081#NotFoundの反対=数字以外の文字があったら
082if ocidLocationocidNotFound then
083   log "数字以外の文字がありました"
084   return false
085end if
086set ocidReplacedStrings to ocidNumberString as string
087set numCntPage to ocidReplacedStrings as integer
088#ページ送りは1ページ少なく
089set num2upPage to (round (numCntPage / 2) rounding down) as integer
090
091
092###メインモニタのサイズ PT
093set ocidScreensArray to refMe's NSScreen's screens()
094set appMainScreen to ocidScreensArray's firstObject()
095set rectMailScreen to appMainScreen's frame()
096set originMainScreen to (first item of rectMailScreen) as list
097set sizeMainScreen to (last item of rectMailScreen) as list
098set numWpt to (first item of sizeMainScreen) as integer
099set numHpt to (last item of sizeMainScreen) as integer
100
101###メインモニタのサイズ PX
102set numMainDisplayID to refMe's CGMainDisplayID()
103set numWpx to refMe's CGDisplayPixelsWide(numMainDisplayID)
104set numHpx to refMe's CGDisplayPixelsHigh(numMainDisplayID)
105
106##SYSTEM EVENTS用のマルチリンガル対応用
107tell application id "com.apple.finder"
108   set strView to (localized string ("206.title") from table ("MenuBar")) as text
109   set strFullScreen to (localized string ("300944.title") from table ("MenuBar")) as text
110   set strExitFullScreen to (localized string ("FV20") from table ("LocalizableMerged")) as text
111end tell
112
113## 時間取得
114set strDateAndTimt to doGetDateNo({"yyyyMMdd-hhmmss", 1})
115
116#ユーザー名
117set recordSystemInfo to (system info) as record
118set strUID to (short user name of recordSystemInfo) as text
119##保存先
120set strSaveDirPath to ("/Users/" & strUID & "/Desktop/ScreenCapture/" & strKeyName & "/") as text
121set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath)
122set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
123set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true)
124
125##保存先確保
126set appFileManager to refMe's NSFileManager's defaultManager()
127set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init()
128ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
129set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
130
131
132###フルスクリーンにする
133tell application id "com.amazon.Lassen"
134   activate
135   tell application "System Events"
136      tell application process "Kindle"
137         try
138            click menu item strFullScreen of menu 1 of menu bar item strView of menu bar 1
139         end try
140      end tell
141   end tell
142end tell
143#ここはフルスクリーン移行の待ち時間
144delay 2
145
146repeat with itemNo from 1 to num2upPage by 1
147   #保存ファイル名
148   set strFileName to ("" & itemNo & "-" & strKeyName & "_" & strDateAndTimt & ".png") as text
149   ###保存先 ファイルパス
150   set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false))
151   set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
152   
153   
154   #キャプチャーとって
155   set strCommandText to ("/usr/sbin/screencapture   -R0," & numMenuBarHpx & "," & numWpx & "," & (numHpx - numMenuBarHpx) & " -x   -t png -o \"" & strSaveFilePath & "\"") as text
156   do shell script strCommandText
157   delay numInterval
158   
159   #次のページへ
160   tell application id "com.amazon.Lassen" to activate
161   tell application "System Events"
162      tell application process "Kindle"
163         key code 123
164      end tell
165   end tell
166   delay 2
167   if itemNo ≥ (num2upPage - 1) then
168      #保存ファイル名
169      set strFileName to ("" & (itemNo + 1) & "-" & strKeyName & "_" & strDateAndTimt & ".png") as text
170      ###保存先 ファイルパス
171      set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false))
172      set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
173      #キャプチャーとって
174      set strCommandText to ("/usr/sbin/screencapture   -R0," & numMenuBarHpx & "," & numWpx & "," & (numHpx - numMenuBarHpx) & " -x   -t png -o \"" & strSaveFilePath & "\"") as text
175      do shell script strCommandText
176   end if
177   
178end repeat
179
180delay 2
181#フルスクリーンを終了させる
182tell application id "com.amazon.Lassen"
183   activate
184   tell application "System Events"
185      tell application process "Kindle"
186         try
187            click menu item strExitFullScreen of menu 1 of menu bar item strView of menu bar 1
188         end try
189      end tell
190   end tell
191end tell
192
193return
194
195
196
197################################
198# 日付 doGetDateNo(argDateFormat,argCalendarNO)
199# argCalendarNO 1 NSCalendarIdentifierGregorian 西暦
200# argCalendarNO 2 NSCalendarIdentifierJapanese 和暦
201################################
202to doGetDateNo({argDateFormat, argCalendarNO})
203   ##渡された値をテキストで確定させて
204   set strDateFormat to argDateFormat as text
205   set intCalendarNO to argCalendarNO as integer
206   ###日付情報の取得
207   set ocidDate to current application's NSDate's |date|()
208   ###日付のフォーマットを定義(日本語)
209   set ocidFormatterJP to current application's NSDateFormatter's alloc()'s init()
210   ###和暦 西暦 カレンダー分岐
211   if intCalendarNO = 1 then
212      set ocidCalendarID to (current application's NSCalendarIdentifierGregorian)
213   else if intCalendarNO = 2 then
214      set ocidCalendarID to (current application's NSCalendarIdentifierJapanese)
215   else
216      set ocidCalendarID to (current application's NSCalendarIdentifierISO8601)
217   end if
218   set ocidCalendarJP to current application's NSCalendar's alloc()'s initWithCalendarIdentifier:(ocidCalendarID)
219   set ocidTimezoneJP to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo")
220   set ocidLocaleJP to current application's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP_POSIX")
221   ###設定
222   ocidFormatterJP's setTimeZone:(ocidTimezoneJP)
223   ocidFormatterJP's setLocale:(ocidLocaleJP)
224   ocidFormatterJP's setCalendar:(ocidCalendarJP)
225   # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterNoStyle)
226   # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterShortStyle)
227   # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterMediumStyle)
228   # ocidFormatterJP's setDateStyle:(current application's NSDateFormatterLongStyle)
229   ocidFormatterJP's setDateStyle:(current application's NSDateFormatterFullStyle)
230   ###渡された値でフォーマット定義
231   ocidFormatterJP's setDateFormat:(strDateFormat)
232   ###フォーマット適応
233   set ocidDateAndTime to ocidFormatterJP's stringFromDate:(ocidDate)
234   ###テキストで戻す
235   set strDateAndTime to ocidDateAndTime as text
236   return strDateAndTime
237end doGetDateNo
AppleScriptで生成しました