| 001 | #!/usr/bin/env osascript |
| 002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 003 | (* |
| 004 |
|
| 005 | Kindleのページ送り |
| 006 | 日本語版用 見開き表示分ページ送り |
| 007 |
|
| 008 | キャプチャー(自炊用) |
| 009 |
|
| 010 | com.cocolog-nifty.quicktimer.icefloe *) |
| 011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
| 012 | use AppleScript version "2.8" |
| 013 | use framework "Foundation" |
| 014 | use framework "AppKit" |
| 015 | use framework "CoreGraphics" |
| 016 | use scripting additions |
| 017 | property refMe : a reference to current application |
| 018 | property ocidNotFound : a reference to 9.22337203685477E+18 + 5807 |
| 019 |
|
| 020 | ################################# |
| 021 | #総ページ数 |
| 022 | set numCntPage to (183) as integer |
| 023 | ## 1ページ(見開き)での滞留時間 |
| 024 | set numInterval to 4 as integer |
| 025 | ## ファイル名定義 |
| 026 | set strKeyName to ("ScreenCapture") as text |
| 027 | ## 1ページ(見開き)での滞留時間 |
| 028 | set numInterval to 4 as integer |
| 029 | ## メニューバーPXサイズ |
| 030 | set numMenuBarHpx to 33 as integer |
| 031 | ################################# |
| 032 | #本処理 |
| 033 | ######################## |
| 034 | # ダイアログ |
| 035 | set strTitle to ("入力してください") as text |
| 036 | set strMsg to ("メイン画面下に出る総ページ数") as text |
| 037 | set strDefaultAnswer to (numCntPage) as text |
| 038 | set strOK to ("OK") as text |
| 039 | set strCancel to ("キャンセル") as text |
| 040 | set listButtons to {strOK, strCancel} as list |
| 041 | set strIconFilePath to ("/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns") as text |
| 042 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 043 | try |
| 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 |
| 048 | on error strErrorMes number numErrorNO |
| 049 | log strErrorMes & numErrorNO |
| 050 | return false |
| 051 | end try |
| 052 | set boolGaveUP to (gave up of recordResponse) as boolean |
| 053 | set strButtonReturned to (button returned of recordResponse) as text |
| 054 | try |
| 055 | tell application "System Events" to quit |
| 056 | end try |
| 057 | if recordResponse is false then |
| 058 | log "キャンセル終了" |
| 059 | return false |
| 060 | else if true is boolGaveUP then |
| 061 | log "時間切れですやりなおしてください" |
| 062 | return false |
| 063 | else if strCancel is strButtonReturned then |
| 064 | log "キャンセル終了" |
| 065 | return false |
| 066 | end if |
| 067 |
|
| 068 | set strResponse to (text returned of recordResponse) as text |
| 069 | set ocidOrgStrings to (refMe's NSString's stringWithString:(strResponse)) |
| 070 | set ocidResponseString to (ocidOrgStrings's stringByReplacingOccurrencesOfString:(return) withString:("")) |
| 071 | set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(linefeed) withString:("")) |
| 072 | set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(tab) withString:("")) |
| 073 | set ocidResponseString to (ocidResponseString's stringByReplacingOccurrencesOfString:(space) withString:("")) |
| 074 | ##半角にして |
| 075 | set ocidNumberString to ocidResponseString's stringByApplyingTransform:(refMe's NSStringTransformFullwidthToHalfwidth) |reverse|:(false) |
| 076 | ###数字以外の文字が |
| 077 | set ocidDigitsCharSet to (refMe's NSCharacterSet's decimalDigitCharacterSet) |
| 078 | set ocidNotDigitsCharSet to ocidDigitsCharSet's invertedSet() |
| 079 | set ocidMatch to ocidNumberString's rangeOfCharacterFromSet:(ocidNotDigitsCharSet) |
| 080 | set ocidLocation to ocidMatch's location() |
| 081 | #NotFoundの反対=数字以外の文字があったら |
| 082 | if ocidLocation ≠ ocidNotFound then |
| 083 | log "数字以外の文字がありました" |
| 084 | return false |
| 085 | end if |
| 086 | set ocidReplacedStrings to ocidNumberString as string |
| 087 | set numCntPage to ocidReplacedStrings as integer |
| 088 | #ページ送りは1ページ少なく |
| 089 | set num2upPage to (round (numCntPage / 2) rounding down) as integer |
| 090 |
|
| 091 |
|
| 092 | ###メインモニタのサイズ PT |
| 093 | set ocidScreensArray to refMe's NSScreen's screens() |
| 094 | set appMainScreen to ocidScreensArray's firstObject() |
| 095 | set rectMailScreen to appMainScreen's frame() |
| 096 | set originMainScreen to (first item of rectMailScreen) as list |
| 097 | set sizeMainScreen to (last item of rectMailScreen) as list |
| 098 | set numWpt to (first item of sizeMainScreen) as integer |
| 099 | set numHpt to (last item of sizeMainScreen) as integer |
| 100 |
|
| 101 | ###メインモニタのサイズ PX |
| 102 | set numMainDisplayID to refMe's CGMainDisplayID() |
| 103 | set numWpx to refMe's CGDisplayPixelsWide(numMainDisplayID) |
| 104 | set numHpx to refMe's CGDisplayPixelsHigh(numMainDisplayID) |
| 105 |
|
| 106 | ##SYSTEM EVENTS用のマルチリンガル対応用 |
| 107 | tell 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 |
| 111 | end tell |
| 112 |
|
| 113 | ## 時間取得 |
| 114 | set strDateAndTimt to doGetDateNo({"yyyyMMdd-hhmmss", 1}) |
| 115 |
|
| 116 | #ユーザー名 |
| 117 | set recordSystemInfo to (system info) as record |
| 118 | set strUID to (short user name of recordSystemInfo) as text |
| 119 | ##保存先 |
| 120 | set strSaveDirPath to ("/Users/" & strUID & "/Desktop/ScreenCapture/" & strKeyName & "/") as text |
| 121 | set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDirPath) |
| 122 | set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath() |
| 123 | set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:true) |
| 124 |
|
| 125 | ##保存先確保 |
| 126 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 127 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 128 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 129 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference) |
| 130 |
|
| 131 |
|
| 132 | ###フルスクリーンにする |
| 133 | tell 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 |
| 142 | end tell |
| 143 | #ここはフルスクリーン移行の待ち時間 |
| 144 | delay 2 |
| 145 |
|
| 146 | repeat 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 | |
| 178 | end repeat |
| 179 |
|
| 180 | delay 2 |
| 181 | #フルスクリーンを終了させる |
| 182 | tell 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 |
| 191 | end tell |
| 192 |
|
| 193 | return |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 | ################################ |
| 198 | # 日付 doGetDateNo(argDateFormat,argCalendarNO) |
| 199 | # argCalendarNO 1 NSCalendarIdentifierGregorian 西暦 |
| 200 | # argCalendarNO 2 NSCalendarIdentifierJapanese 和暦 |
| 201 | ################################ |
| 202 | to 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 |
| 237 | end doGetDateNo |