Toggle Automatic Graphics Switching via Terminal – How to

gpu

I have a 15" MacBook Pro from May 2019 with a Radeon Pro 555X 4 GB graphics card. I would like to change the Automatic Graphics Switching setting with a terminal command.

Best Answer

I did some googling, and I do not know of a native command line utility, or a third-party command line utility, that can toggle the state of automatic graphics switching; however it can be done from the command line by utilizing AppleScript to toggle the [√] Automatic graphics switching checkbox on the Energy Saver pane in System Preferences.

In lieu of finding a native command line utility, or a third-party command line utility, or until a better answer is posted, the following will allow you to toggle it from the command line in e.g. Terminal.

  1. In Terminal, use the following compound command to create the file and open it:

    touch togags; open togags
    
  2. Copy and paste the example AppleScript code, shown further below, into the opened togags file.

  3. Save and close the file.

  4. Make the file executable:

    chmod u+x togags
    

I used togags for: [tog]gle [a]utomatic [g]raphics [s]witching

You can now use it from the directory it's in using ./togags otherwise /path/to/togags; however, it's best if you place in into a directory that's within your PATH statement. Then it can be used from anywhere by just togags, (or whatever you actually named the executable).

NOTE: This will also require giving Terminal accessibility privileges for this to work properly.

Running the command twice, to show its output:

$ togags
   Automatic Graphics Switching is: OFF
$ togags
   Automatic Graphics Switching is: ON
$ 

The following example AppleScript code, was tested and works me as coded on macOS High Sierra. A minor change may be needed for macOS Mojave; however, I'm not able to test at the present time. The same goes for older versions of OS X/macOS.

Example AppleScript code:

#!/usr/bin/osascript

if running of application "System Preferences" then
    try
        quit application "System Preferences"
    on error
        do shell script "killall 'System Preferences'"
    end try
end if

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

tell application "System Preferences"
    reveal pane id "com.apple.preference.energysaver"
    repeat until exists window "Energy Saver"
        delay 0.1
    end repeat
end tell

tell application "System Events" to tell ¬ 
    group 1 of window "Energy Saver" of application process "System Preferences"
    repeat until exists checkbox "Automatic graphics switching"
        delay 0.1
    end repeat
    click checkbox "Automatic graphics switching"
    set cbAGS to (value of checkbox "Automatic graphics switching") as boolean
end tell

quit application "System Preferences"

if cbAGS then
    return "   Automatic Graphics Switching is: ON"
else
    return "   Automatic Graphics Switching is: OFF"
end if

Note: The example AppleScript code is just that and other then what's already coded, it 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.