对付 Vitalsource 专用 AppleScript

Hacks Jan 14, 2017

对于 VitalSource 这种毒瘤,电子书 DRM 也就算了,一次只能打两页,简直是人类耻辱。正版体验太差,因此,用 AppleScript 把整本书打印出来(不知道算不算破解),for freedom。

关于这个脚本,最初出处已不可考,经过不知多少个人的修改了。我找到的时候,也是不能用的状态,应该是因为 macOS 和 Vitalsource Shelf 都更新过了的关系。我不懂 AppleScript,不过语法和英文差不多,改了一下可用了。

# Saves current document open in VitalSource Bookshelf as PDF

set totalPages to 278
set startPage to 1

activate application "VitalSource Bookshelf"
tell application "System Events"
	tell process "VitalSource Bookshelf"
		repeat with theIncrementValue from startPage to totalPages - 1 by 2
			set thisValue to theIncrementValue
			
			# Wait until the first Print dialog opens before proceeding
			repeat until exists window "Print"
				# Open the print dialog
				keystroke "p" using command down
			end repeat
			
			# First print dialoge. Print pages ten at a time.
			keystroke thisValue as string
			delay 1
			keystroke tab
			delay 1
			keystroke thisValue + 1 as string
			delay 1
			keystroke tab
			# delay 2
			click button "继续" of window "Print"
			
			# Wait until the second Print dialog opens before proceeding
			repeat until exists window "打印"
			end repeat
			
			# Expand the "PDF" menu button (must be expanded before the menu is referencable)
			click menu button "PDF" of window "打印"
			
			# Wait until the Menu button menu is created before proceeding
			repeat until exists menu item "存储为 PDF…" of menu 1 of menu button "PDF" of window "打印"
			end repeat
			
			# Select the "Save as PDF" menu item
			click menu item "存储为 PDF…" of menu 1 of menu button "PDF" of window "打印"
			
			# Wait until the Save dialog opens before proceeding
			# repeat until sheet 1 exists
			# end repeat
			delay 3
			# Save. Set name to page numbers saved.
			keystroke thisValue as string
			keystroke "_"
			keystroke thisValue + 1 as string
			# click button "存储"
			keystroke return
			
			# Wait for dialog to close before proceeding.
			# repeat until not (exists window "Save")
			# end repeat
			delay 3
		end repeat
	end tell
end tell

一定要小心脚本跑飞,我改代码的时候不知道飞了几次,只能强制重启。如果不确定的话,多加 delay 参数。

截至目前,2017.01.05,该脚本正常运行。

aLPHAtOAD

太年轻,太简单,有时候幼稚。