iOS Simulators – Toggle Between Portrait and Landscape Right via AppleScript

applescriptiosios-simulatormacos

How do I toggle between "Portrait" and "Landscape Right" orientation in the iOS simulators via AppleScript?

Best Answer

On my system, using Script Editor, the following example AppleScript code works for me to toggle between Portrait and Landscape Right of the Simulator window:

tell application "Simulator" to activate

tell application "System Events" to ¬
    tell application process "Simulator" to ¬
        tell menu 1 of ¬
            menu item "Orientation" of ¬
            menu 1 of ¬
            menu bar item "Device" of ¬
            menu bar 1
            if value of ¬
                attribute "AXMenuItemMarkChar" of ¬
                menu item "Portrait" is "✓" then
                click menu item "Landscape Right"
            else if value of ¬
                attribute "AXMenuItemMarkChar" of ¬
                menu item "Landscape Right" is "✓" then
                click menu item "Portrait"
            end if
        end tell

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.