首頁>Apple>source

我正在尝試建立一个快捷方式,以使用Automator切換Keyboard Viewer.我已经尝試了這两種方法:這是针對Sierra的
两種方式都存在語法錯誤"無法获取應用程式KeyboardViewer"
如果我按分配的快捷方式,它將打開一个視窗("選擇應用程式"),其中包含應用程式列表

我尝試過的指令碼:

if application "KeyboardViewer" is running then
    quit application "KeyboardViewer"
end if
activate application "KeyboardViewer"
-- wait until the window has been closed, then end the KeyboardViewer process
set numberOfWindows to 1
repeat until numberOfWindows = 0
    delay 5
    tell application "System Events"
        tell process "KeyboardViewer"
            set numberOfWindows to count windows
        end tell
    end tell
end repeat
quit application "KeyboardViewer"

on run
    tell application "System Events"
        set activeApp to name of first application process whose frontmost is true and visible is true
    end tell
    if application "KeyboardViewer" is running then
        quit application "KeyboardViewer"
    else
        activate application "KeyboardViewer"
    end if
    tell application activeApp to activate
end run
最新回復
  • 5月前
    1 #

    示例 AppleScript code(如下所示)將切換 Accessibility Keyboard的狀態 並在 macOS Catalina下进行了測試 和 macOS Big Sur

    该代碼對我有用,但是,它使用第三方實用程式来避免關闭 Accessibility Keyboard 通過取消選中 Enable Accessibility Keyboard System Preferences中的複選框 為了不必去看 UI Scripting System Preferences的 開幕和 OK 單击按钮,然後按 System Preferences 關闭,就像您問题的另一个答案中的code一樣。

    必须使用 UI Scripting 够糟糕的,但是却不得不去看 System Preferences 打開和 OK 單击按钮,然後按 System Preferences 使用我的方法完全不需要關闭。

    第三方實用程式是cliclick,我將其放在了 /usr/local/bin/中 並單击 UI上的Close按钮 威兹維兹 . (我確實尝試過使用其他方法關闭它,但這是問题最少的原因,也是為什麼我需要使用它。)

    使用示例 Accessibility Keyboard code我將其儲存為 AppleScriptToggle Accessibility Keyboard.scpt 並使用 ~/Library/Scripts/鍵盤快捷键觸發它 使用FastScripts,但是,使用 ⌃⌥⌘K 可以使用 Automator建立服務/快速操作 actioncode,以及分配在以下位置的鍵盤快捷键Run AppleScript > System Preferences > Keyboard > Shortcuts

    示例 Services是什麼 code可以:

    AppleScript

      Checks to see if the 通過阅讀 Accessibility Keyboard顯示 plist檔案看看是否 com.apple.HIToolbox 存在,就像它在顯示時一樣,而不是在不顯示時一樣。

      如果顯示,則稱為 com.apple.inputmethod.AssistiveControl handler,以程式設計方式获取 closeVirtualKeyboardClose按钮的位置和大小 為了點击它,使用 Accessibility Keyboard ,無論它在顯示器上的什麼位置。

      如果未顯示,則呼叫 cliclick 顯示處理程式

      示例 openVirtualKeyboard 代碼

      AppleScript
      

  • 5月前
    2 #

    這在macOS Big Sur上對我有用。

    set shellCMD to {¬
        "defaults read com.apple.HIToolbox", ¬
        " | ", ¬
        "grep -o 'com.apple.inputmethod.AssistiveControl'", ¬
        "; exit 0"} as string
    set foo to (do shell script shellCMD)
    
    if not foo is equal to "" then
        closeVirtualKeyboard()
    else
        my openVirtualKeyboard()
    end if
    on closeVirtualKeyboard()
        tell application "System Events"
            set ps to {position, size} of ¬
                button "Close" of window "Panel" of ¬
                application process "Assistive Control"
            set x to (item 1 of item 1 of ps) + ¬
                (item 1 of item 2 of ps) / 2 as integer
            set y to (item 2 of item 1 of ps) + ¬
                (item 2 of item 2 of ps) / 2 as integer
            set shellCMD to ¬
                {"/usr/local/bin/cliclick -r c:", x, ",", y} ¬
                    as string
            tell me to do shell script shellCMD
        end tell
    end closeVirtualKeyboard
    on openVirtualKeyboard()
        if running of application "System Preferences" then
            try
                tell application "System Preferences" to quit
            on error
                do shell script "killall 'System Preferences'"
            end try
        end if
        delay 0.1
        repeat while running of application "System Preferences" is true
            delay 0.1
        end repeat
        tell application "System Preferences" to ¬
            reveal anchor "Virtual_Keyboard" in ¬
                pane id "com.apple.preference.universalaccess"
        tell application "System Events"
            repeat until (exists checkbox "Enable Accessibility Keyboard" of ¬
                tab group 1 of group 1 of window "Accessibility" of ¬
                application process "System Preferences")
                delay 0.01
            end repeat
            click checkbox "Enable Accessibility Keyboard" of ¬
                tab group 1 of group 1 of window "Accessibility" of ¬
                application process "System Preferences"
        end tell
        tell application "System Preferences" to quit
    end openVirtualKeyboard
    

    旁白:我使用do shell指令碼" killall'System Preferences'"命令而不是告诉應用程式" System Preferences"退出的原因是...让我们說,無論出於何種原因,System Preferences.app已经 啟用(例如,打開"共享/遠端管理/計算機設置"視窗),但不可见或顯示在最前面或其他位置.在指令碼編輯器中,您尝試執行告诉應用程式"系統偏好設置"以顯示窗格ID為" com.apple.preference.universalaccess"的锚點" SpeakableItems"。该命令没有明顯的錯誤,但是当您切換到系統偏好設置時," 顯示的锚"未顯示.好吧,没什麼大不了的,容易修複.我將在顯示锚命令之前插入一个Tell應用程式"系統偏好設置"以退出命令.這次執行更新的代碼,您会收到(userCanceledErr:-128)錯誤.所有這些麻煩是因為打開了"系統偏好設置"中的輔助下拉視窗.我认為do shell指令碼" killall'System Preferences'"命令似乎是最好的解決方案。


    如果在系統偏好設置/輔助功能中選中了"在選單欄中顯示輔助功能狀態",則這裏是另一个選項.簡短,很可爱,但是它確實使用了UI指令碼。

    if application "System Preferences" is running then do shell script "killall 'System Preferences'"
    repeat until application "System Preferences" is not running
        delay 0.1
    end repeat
    tell application "System Preferences"
        reveal anchor "Virtual_Keyboard" of pane "com.apple.preference.universalaccess"
    end tell
    tell application "System Events" to tell application process "System Preferences"
        repeat until checkbox "Enable Accessibility Keyboard" of tab group 1 ¬
            of group 1 of window "Accessibility" exists
            delay 0.1
        end repeat
        set theValue to value of checkbox "Enable Accessibility Keyboard" of tab group ¬
            1 of group 1 of window "Accessibility"
        delay 0.1
        if theValue = 1 then
            set frontmost to true
            repeat until frontmost
                delay 0.1
            end repeat
            click checkbox "Enable Accessibility Keyboard" of tab group 1 of ¬
                group 1 of window "Accessibility"
            delay 0.1
            if UI element "OK" of sheet 1 of window "Accessibility" exists then
                click UI element "OK" of sheet 1 of window "Accessibility"
            end if
        else
            click checkbox "Enable Accessibility Keyboard" of tab group 1 of ¬
                group 1 of window "Accessibility"
        end if
    end tell
    do shell script "killall 'System Preferences'"
    
    tell application "System Events" click menu bar item "Accessibility Shortcuts" of menu bar 1 ¬ of application process "ControlCenter" repeat until checkbox "Accessibility Keyboard" of group 1 of ¬ window "Control Center" of application process "ControlCenter" exists delay 0.1 end repeat click checkbox "Accessibility Keyboard" of group 1 of ¬ window "Control Center" of application process "ControlCenter" delay 0.1 key code 53 -- escape key end tell

相似問題

  • macos:Big Sur切換到zsh终端,但仍使用bash_profile
  • macos:如何檢索存档檔案