-- ===== 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