我想讓applescript一直向上滾動一個窗口。如何使用applescript滾動到窗口的頂部?
我已經嘗試了向上翻頁鍵,主鍵,我試過尋找一種使用內置窗口滾動功能的滾動方式,但是我至今無法移動滾動的位置。
我想讓applescript一直向上滾動一個窗口。如何使用applescript滾動到窗口的頂部?
我已經嘗試了向上翻頁鍵,主鍵,我試過尋找一種使用內置窗口滾動功能的滾動方式,但是我至今無法移動滾動的位置。
基本上,使用tell app "System Events"
語句發送擊鍵和鍵碼。 在理論上,你可以使用以下命令:
keystroke page up key
keystroke page down key
keystroke home key
但對我來說這doesn't工作。好消息是你可以使用關鍵代碼。我建議使用優秀的免費Full Key Codes應用程序來讀取它們,但讓它讀取同時按下兩個按鍵有點棘手。
爲FN + 箭頭鍵時,鍵代碼 -combos如下:
翻頁:FN + 向上鍵:key code 116
向下翻頁:FN + 向下鍵:key code 121
首頁:FN + 左鍵:key code 115
結束:FN + 右鍵:key code 119
因此,舉例來說,如果你有一個很長的網頁在Safari中打開,並且希望要滾動到其結尾處,請使用
tell application "System Events"
tell application "Safari" to activate
— to see the animation, we wait a moment:
delay 0.5
key code 119
end tell
發送鍵擊的替代方法是使用GUI scripting。
買者:雖然GUI腳本是比發送鍵擊的應用程序的給版本更強大的,在未來的版本變化的應用程序的佈局可以打破你的代碼。
另外:
GUI腳本要求輔助設備的訪問被啓用。使需要管理員權限:
tell application "System Events" to set UI elements enabled to true
(需要管理員權限)tell application "System Events" to get UI elements enabled
將訪問是否啓用報告或不。確定正確的UI元素的目標可以是不平凡的,繁瑣;使用Accessibility Inspector
實用程序附帶Xcode
幫助。該實用程序報告的類名稱對應於System Events
字典中包含的UI element
類;例如,AXSplitGroup
對應於splitter group
。
下滾動Safari 6.0.3
的前窗頂端(用於輔助設備的訪問必須啓用):
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell
更新:作爲一個提醒,這種類型的腳本運行良好對於給定版本的應用,代碼必須改爲Safari 8.0.4
:
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell
通過瀏覽器,您還可以使用JavaScript:
tell application "Safari" to tell document 1
do JavaScript "window.scroll(0,0)"
end tell
tell application "Google Chrome" to tell active tab of window 1
execute javascript "window.scroll(0,0)"
end tell
雖然這隻適用於所述的兩種瀏覽器應用程序,但它有兩個優點:它比鍵盤發送方法更強大,並且不需要授權進行輔助訪問,與GUI腳本方法不同。 – mklement0
太棒了。謝謝。我嘗試了很多東西,其中許多與您的解決方案類似,但它們並不完全正確。這像一個魅力。 –
不幸的是,引用的鍵盤實用程序已經過時(爲PowerPC平臺編寫)。 **關鍵代碼**(https://itunes.apple.com/cn/app/key-codes/id414568915?mt=12)是一個仍在維護(也是免費)的替代方案。 – mklement0