天冷了,在Mac预览里看PDF时,滚动页面非常冻手。预览虽然能够实现幻灯片播放,但是不支持逐行滚动。这里我们使用AppleScript来控制页面的滚动。
我们先将页面分成指定行数linesOfPage,根据自己的阅读速度设定滚动时间间隔intval。然后读取PDF的页面数量pageNum,这样我们就能计算出每次的滚动量dy= 1.0/pageNum/linesOfPage。每次滚动的时候,我们先获取当前的滚动位置,然后加上滚动量,将其设置为滚动条的新位置值。
注:获取UI元素要用到UI Browser。
完整代码如下:
set linesOfPage to 20
set intval to 2
try
set theDuration to (text returned of (display dialog "Enter the scroll times" default answer "10"))
set theDuration to theDuration as integer
end try
activate application "Preview"
tell application "System Events"
tell process "Preview"
set pageText to (get value of the last static text of front window)
-- get page number
set {currentPage, totalPage} to my splitString(pageText)
set pageNum to totalPage as number
set dy to 1.0 / pageNum / linesOfPage
-- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
set scrollbarValue to a reference to scroll bar 1 of scroll area 2 of splitter group 1 of front window
repeat with i from theDuration to 1 by -1
set inputValue to (get value of scrollbarValue)
set value of scrollbarValue to inputValue + dy
delay intval
end repeat
end tell
end tell
on splitString(someString)
try
set tempTID to AppleScript's text item delimiters -- save current delimiters
set AppleScript's text item delimiters to "/"
set pieces to text items of someString -- split the string
set AppleScript's text item delimiters to tempTID -- restore old delimiters
set firstPart to item 1 of pieces
set secondPart to item 2 of pieces
on error errmess -- delimiter not found
log errmess
return {firstPart, ""} -- empty string for missing item
end try
return {firstPart, secondPart}
end splitString