macosapplescript

AppleScript - cycle through desktops


I am writing a script that will cycle through desktops on macOS

I try to move between desktops using shortcuts "ctrl+right" and "ctrl+left", which I set up on my machine to move to a right and a left desktop respectively.

# move 4 desktops to the right and back
tell application "System Events"
    repeat 4 times
        key code 124 using (control down)
        key code 123 using (control down)
    end repeat
end tell

Once I get to a desktop with a fullscreen RDP session, the RDP session starts receiving the ctrl+-> instead of the macOS.

How can I move focus from the RDP session back to macOS (or cycle through desktops in a different way)?


Solution

  • I have this AppleScript which switches to desktop 2 :

    tell application "Mission Control" to launch
    tell application "System Events"
        delay .1
        -- button 2 means desktop 2
        click button 2 of list 1 of group 2 of group 1 of group 1 of process "Dock"
        key code 53 --  # Esc key on US English Keyboard
    end tell
    

    I have not found what's the logic of those groups and I'm not sure how/if you can adapt it to suit your requirements.