Skip to content

[Mac] 如何使用 Apple Script 快速切換 Natural Scrolling 的滾動方向

Published: at 12:04 PM (3 min read)

Table of Contents

Open Table of Contents

問題

我自己平時工作外接滑鼠時,習慣將滾動方向設定為非自然滾動,但是當我使用觸控板時,我又習慣將滾動方向設定為自然滾動,但這樣的習慣導致我時常需要進入 系統偏好設定 -> 觸控板 -> 滾動與縮放 -> 自然滾動。然而 Apple Script 可以如何幫助我快速地切換滾動方向?

How to toggle scroll direction with Apple Script

編寫 Apple Script

  1. 新建一個 Apple Script 檔案: toggle_scroll_direction.applescript
-- Open the Trackpad settings in System Preferences
do shell script "open x-apple.systempreferences:com.apple.Trackpad-Settings.extension"

-- Delay to ensure System Events can interact with UI elements
delay 0.7

-- Begin interacting with System Events
tell application "System Events"
    -- Access the System Settings process
    tell process "System Settings"
        -- Click the second radio button to select a specific tab in the Trackpad settings
        click radio button 2 of tab group 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

        -- Toggle the "Natural scrolling" checkbox in the Trackpad settings
        click checkbox "Natural scrolling" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

        -- Quit the System Settings application
        tell application "System Settings" to quit
    end tell
end tell
  1. 執行 toggle_scroll_direction.applescript 測試
osascript toggle_scroll_direction.applescript

預期: 能夠自動開啟 系統偏好設定 -> 觸控板 -> 滾動與縮放 -> 自然滾動 並且切換滾動方向,這樣我們就可以透過一行指令來快速切換滾動方向 🎉

整合進 Raycast

如果你有用 Raycast 的話,你可以將 Apple Script 照以下程式碼整合進 Script Commands,這樣就直接透過快捷鍵或呼叫指令來快速切換滾動方向 🚀

Toggle Scroll Direction

#!/usr/bin/osascript

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Toggle Scroll Direction
# @raycast.mode compact

# Optional parameters:
# @raycast.icon 🤖

# Documentation:
# @raycast.description Toggle Scroll Direction - Quickly switch the natural scrolling setting on your Mac to easily adapt to different usage preferences.
# @raycast.author Camel2243
# @raycast.authorURL https://raycast.com/Camel2243

-- Open the Trackpad settings in System Preferences
do shell script "open x-apple.systempreferences:com.apple.Trackpad-Settings.extension"

-- Delay to ensure System Events can interact with UI elements
delay 0.7

-- Begin interacting with System Events
tell application "System Events"
    -- Access the System Settings process
    tell process "System Settings"
        -- Click the second radio button to select a specific tab in the Trackpad settings
        click radio button 2 of tab group 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

        -- Toggle the "Natural scrolling" checkbox in the Trackpad settings
        click checkbox "Natural scrolling" of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1

        -- Quit the System Settings application
        tell application "System Settings" to quit
    end tell
end tell

參考資料


Previous Post
[leetcode] 12. Roman to Integer
Next Post
[leetcode] 11. Container With Most Water