天冷了,在Mac预览里看PDF时,滚动页面非常冻手。预览虽然能够实现幻灯片播放,但是不支持逐行滚动。这里我们使用AppleScript来控制页面的滚动。

我们先将页面分成指定行数linesOfPage,根据自己的阅读速度设定滚动时间间隔intval。然后读取PDF的页面数量pageNum,这样我们就能计算出每次的滚动量dy= 1.0/pageNum/linesOfPage。每次滚动的时候,我们先获取当前的滚动位置,然后加上滚动量,将其设置为滚动条的新位置值。

注:获取UI元素要用到UI Browser。

完整代码如下:

set linesOfPage to 20set intval to 2tryset theDuration to (text returned of (display dialog "Enter the scroll times" default answer "10"))set theDuration to theDuration as integerend tryactivate application "Preview"tell application "System Events"tell process "Preview"set pageText to (get value of the last static text of front window)-- get page numberset {currentPage, totalPage} to my splitString(pageText)set pageNum to totalPage as numberset dy to 1.0 / pageNum / linesOfPage-- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5set scrollbarValue to a reference to scroll bar 1 of scroll area 2 of splitter group 1 of front windowrepeat with i from theDuration to 1 by -1set inputValue to (get value of scrollbarValue)set value of scrollbarValue to inputValue + dydelay intvalend repeatend tellend tellon splitString(someString)tryset tempTID to AppleScript's text item delimiters -- save current delimitersset AppleScript's text item delimiters to "/"set pieces to text items of someString -- split the stringset AppleScript's text item delimiters to tempTID -- restore old delimitersset firstPart to item 1 of piecesset secondPart to item 2 of pieceson error errmess -- delimiter not foundlog errmessreturn {firstPart, ""} -- empty string for missing itemend tryreturn {firstPart, secondPart}end splitString