How to flash screen from command line

Is there a way to flash the screen from the command line? I am looking for the same effect that Accessibility does for "Flash the screen when an alert sound occurs", but I don't want it for every alert.


Solution 1:

As far as I know, you can't replicate this alert without enabling that option. If you do enable that option, you can cause a flash from Terminal using:

osascript -e "beep"

However, if you don't want to enable that option, you can use invert colours. Using the invert colours Accessibility option twice causes the screen to flash, therefore pressing the keyboard shortcut ⌃⌥⌘8 twice will cause a flash:

#!/usr/bin/osascript
tell application "System Events" to repeat 2 times
key code 28 using {command down, control down, option down}
delay 0
end repeat

Solution 2:

It doesn't flash the whole screen (unless you have terminal set to full screen) but if you go to Preferences > Settings > Advanced and select "Visual Bell" you can then type:

echo ^G

(The keystrokes to get the correct characters are: echo control + v control + g because the control character has to be quoted)

to flash the terminal window.

Solution 3:

This works for me in Sierra via applescript:

tell application "System Preferences"
    run
    reveal anchor "Hearing" of pane id "com.apple.preference.universalaccess"
end tell
tell application "System Events" to tell process "System Preferences" to click button "Test Screen Flash" of window "Accessibility" of application process "System Preferences" of application "System Events"

Solution 4:

For others, one alternative I have found is to use the brightness command line tool, available via homebrew ( You can find instructions on how to install it from http://brew.sh ). Link to discussion: https://apple.stackexchange.com/a/127258/23876

The first line installs Brightness, the second shows you what brightness looks like in action.

brew install brightness
brightness 0; sleep 1; brightness 1

It only works on the main display though. If this form of brightness switching is too slow for you, experiment with sleep 0.3 or even sleep 0.1.

You can also get the current brightness so that you won't reset to 1:

setopt rematchpcre # needs zsh
if [[ "$(brightness -l)" =~ 'display 0: brightness (\S+)' ]]; then
    print -r -- "$match[1]"
else
    return 1
fi