MrSimonC / Toggle-Mac-Function-Keys

This AppleScript allows you to automate the toggling of using Function Keys vs Media Keys on MacOS
MIT License
86 stars 15 forks source link

Help for another Box #19

Closed chris1111 closed 1 month ago

chris1111 commented 1 month ago

Ive test your script in Sequoia 15.0.1 then it work well, thanks for that. You can update your Readme

But I need some help to change the keys F14, F15 for F9, F10 In Keyboard ➥ Display

Screenshot 2024-10-27 at 8 47 31 AM

I can't figure out how to open the Display pane with AppleScript which is totally different from the one in System Settings

Thanks for Help

MrSimonC commented 1 month ago

Hey. Thanks for the confirmation about the readme and for letting me know that the new macOS version has dropped!

The efficient code on master branch that you've probably seen won't work as it directly invokes an "anchor", and they just isn't an anchor for the display screen that you require, but there is a slightly more (older, cruder – but then all AppleScript is crude in my opinion) which does work:

open location "x-apple.systempreferences:com.apple.Keyboard-Settings.extension"

tell application "System Events" to tell process "System Settings"
    # example window title: "Keyboard – 86%", so "begins with" is needed
    repeat until window begins with "Keyboard" exists
    end repeat

    # wait until Keyboard window is the main window of the application and is accessible
    repeat until exists of (1st window whose value of attribute "AXMain" is true)
    end repeat

    # wait until the group is displayed (needed else fails on Apple M2 Pro)
    repeat until exists group 1 of group 2 of splitter group 1 of group 1 of window 1
    end repeat

    # "Keyboard Shortcuts..." Button (which is button 3 for some macs, and button 1 for others due to keyboard brightness button)
    set keyboardButton to 3
    try
        click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
    on error errorMessage number errorNumber
        -- 1719 being a "reference error" code
        if errorNumber is -1719 then
            set keyboardButton to 1
            click button keyboardButton of group 2 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window 1
        end if
    end try

    repeat until sheet 1 of window 1 exists
    end repeat

    # Click Function Keys
    keystroke "d"
end tell
chris1111 commented 1 month ago

Alright it work thanks again 👍🏻 The only thing needs to fix it is to change the key at the end

MrSimonC commented 1 month ago

@chris1111, I'm glad the updated script worked for you. However, I have to warn you that what you're asking for now is a pretty challenging task. As you've noticed, the screen itself indicates that we need to find the controls, double-click the mouse, and then type in the keys. Unfortunately, AppleScript isn't great at simulating mouse clicks, and we're starting to venture into uncharted territory. While it's not impossible to achieve, it's definitely not a common use case for AppleScript. I'm happy to have helped you get this far, but I think it's time to seek additional help from a community that's more focused on advanced AppleScripting, such as the r/AppleScript community on Reddit. They may be able to provide more guidance on how to tackle this specific challenge. Good luck, and thanks for understanding!

chris1111 commented 1 month ago

Thanks for help and support , I totally understand, I also have very good knowledge of Applescript but not at all of keyboard. I will also look on stackoverflow and macscripter.