使用AppleScript自动滚动预览

天冷了,在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值