How can I toggle Automatic Graphics Switching using terminal?

Solution 1:

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.

Solution 2:

The command sudo pmset -a gpuswitch n will set the graphics switching, with the following values for n:

  • 2 is integrated gpu both on cable and battery

  • 1 is dedicated gpu only with power connected; integrated gpu on battery power

  • 0 is dedicated gpu on both ac and battery

pmset -g will show you the current configuration.

Solution 3:

I find this https://discussions.apple.com/thread/8160651 but it not match my 2018 MacBook with macOS Mojave 10.14

0 = Does not use dedicated graphics
1 = Use dedicated graphics
2 = Switch automaticly is default value when "Automatic Graphics Switching" selected in energy in preference.

The -a -b -c -u flags determine whether the settings apply to battery -b , charger (wall power) -c , UPS -u or all -a

Check settings depended on charger/battery pmset -g custom

Let system use dedicated graphics card when charger plugin sudo pmset -c gpuswitch 1

Let system use graphics card auto switch when using battery sudo pmset -b gpuswitch 2

Let system use auto switch on charger & battery sudo pmset -a gpuswitch 2