Pergunta

I can't seem to figure out how to select the correct buttons with Applescript. I am starting to learn vim and want to be able to toggle the Caps Lock Key between Caps Lock and Ctrl. I have marked the steps which I need to complete.

I found (this post) but it seems to be a little hacky. Maybe it's the way it supposed to be but it shows the system pref. window every time I use it, unlike (this code) that toggles the fn key and works seemlessly.

Could anyone offer some advice?

Here's my code:

tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell

    -- [STEP 1] set mod_keys to value of output from within "Modifier Keys..."
    set mod_keys to button "Modifier Keys..." of tab group 1 of window 1 of application process "System Preferences"

    -- I would prefer not to have to click the mod_keys because I don't want the window popping up but if it's necessary then okay
    click mod_keys

    -- [STEP 2] set cl_key to the second dropdown of mod_keys
    set cl_key to menu item 2 of menu 1 of pop up button 4

    set cl to value of cl_key
    if cl is menu item 2
        set q to menu item 2 of menu 1 of pop up button 4
    else
        set q to menu item 1 of menu 1 of pop up button 4
    end if

end tell

-- This is just to make sure it works, but may be unneccessary
if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

return q

Here is the Modifier Keys Screenshot:

enter image description here

enter link description here http://imageshack.us/a/img833/474/o5co.png

Foi útil?

Solução

This toggles the keys (you have to change the german labels):

    tell application "System Events"
tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell

tell window 1 of application process "System Preferences"
    click button "Sondertasten …" of tab group 1

    tell sheet 1
        tell pop up button "Feststelltaste (⇪):"

            set state to value
            click
            delay 0.2
            if "Feststelltaste" is in state then
                click menu item "⌃ ctrl-Taste" of menu 1
            else
                click menu item "⇪ Feststelltaste" of menu 1
            end if
            delay 0.2
        end tell
        click button "OK"
    end tell
end tell
    end tell

    if application "System Preferences" is running then
tell application "System Preferences" to quit
    end if

But as foo wrote, GUI-Scripts should be the last solution. Especially in Mavericks it's really annoying because you have to enable access for assistive devices for every single app (and again if you change your script).

Outras dicas

I have taken your codes into consideration and implemented it. So for those of you using English, you can use the code below:

tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"

    end tell
    tell window 1 of application process "System Preferences"
        --  click button "Modifier Keys…" of tab group 1
        set uiElems to entire contents

        click button "Modifier Keys…" of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events"
        delay 1
        click pop up button "Caps Lock (⇪) Key:" of sheet 1 of window "Keyboard" of application process "System Preferences" of application "System Events"
        delay 0.2
        keystroke "n"
        delay 0.2
        key code 36
        delay 0.2
        click button "OK" of sheet 1 of window "Keyboard" of application process "System Preferences" of application "System Events"

    --  set uiElems to entire contents

    end tell

end tell

if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top