0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kindleの自動スクショ用AppleScript

Posted at

用途

  • Macbook上で開いたKindle(固定レイアウト)を自動でページめくりながらスクショする。あくまで、自分で購入したkindleを自分しか使用しないpdfとして使うための、下ごしらえ用のツール。

書いてないこと*参考サイトに書いてある。

  • 標準アプリ「プレビュー」でのPDF化
  • pngのトリミング(XnConvertが良いらしい)

使用方法

  • Mackbook「システム環境設定」から、「スクリプトエディタ」のコード実行に関わる設定を修正。
  • Kindle開いて、1ページが適度な大きさで表示されるようにウィンドウの大きさを調整。(フルスクリーンだと、1ページずつの表示にできない。)
  • Kindle上で、スクショ開始したいページに移動。
  • 自動スクショのpngファイルの保存先(スクリプト実行後に指定)のフォルダを好きな場所に作成。
  • スクリプトエディタで下記コードを実行。

参考サイト

差異

  • Kindleデスクトップアプリの仕様変更で、フルスクリーンだと見開きになってしまう。フルスクリーンじゃなくても、Kindleのウィンドウの座標とサイズ取得してスクショするようにした。
  • なるべく、コードを編集しなくて良いように、下記は入力画面を表示させるようにした。
    • pngの出力先フォルダ
    • スクショしたいページ数
    • ページ送りの方向
-- ===== Settings =====
-- target app
set target to "Kindle"
-- page index to the current page of kindle
set startIndex to 1
-- time to wait after actions (tune if needed)
set pauseSeconds to 0.8
-- If you want to capture only the Kindle window (not full screen), set this to true
set captureWindowOnly to true
-- ====================

-- get which directory to save the png
tell application "Finder"
	set theFolder to choose folder with prompt "which directory to save the png:"
	set saveFolder to the POSIX path of theFolder
end tell

-- how many pages to save from and including current page
set user_input to display dialog "how many pages to save from and including current page:" default answer "" buttons {"キャンセル", "OK"} default button "OK"
set user_value to text returned of user_input
try
	set pages to (user_value as integer)
on error
	error "number of page must be integer"
end try

-- get paging direction
set user_input to display dialog "paging direction :" buttons {"to left", "to right"} default button "to right"
set theChoice to button returned of user_input
if theChoice is "to left" then
	display alert "paging direction: left"
	-- pagedir: 1 = left, 2 = right
	set pagedir to 1
else
	display alert "paging direction: right"
	set pagedir to 2
end if



-- Decide arrow key code
set pageKeyCode to 124 -- default right arrow
if pagedir = 1 then set pageKeyCode to 123 -- left arrow

-- Bring Kindle frontmost
tell application target to activate
delay pauseSeconds
tell application "System Events"
	if not (exists process target) then error "Process '" & target & "' not found."
	tell process target to set frontmost to true
end tell
delay pauseSeconds

-- get Kindle window bounds for -R capturing
set rectArg to ""
if captureWindowOnly then
	tell application "System Events"
		tell process target
			if (count of windows) = 0 then error "No Kindle window is open."
			set {wx, wy} to position of window 1
			set {ww, wh} to size of window 1
		end tell
	end tell
	set rectArg to " -R " & wx & "," & wy & "," & ww & "," & wh
end if

repeat with i from startIndex to pages
	-- zero-padding like p001, p002...
	set dp to text -3 thru -1 of ("00" & i)
	set savePath to saveFolder & "p" & dp & ".png"
	
	-- Capture current page
	do shell script "screencapture -x" & rectArg & " " & quoted form of savePath
	
	-- Turn the page (so next iteration captures the NEW page)
	if i < pages then
		tell application "System Events"
			key code pageKeyCode -- send arrow key
		end tell
		delay pauseSeconds
	end if
end repeat

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?