I want to write an AppleScript that

  1. runs (in a loop) until I press esc
  2. "listens" for keystrokes 1, 2, etc.
  3. executes a given command for a each keypress (running a shell command ddcctl to change secondary monitor brightness/contrast)

I've searched and everything I find relates to simulating a key press.

(If this can be done in bash that's fine but I don't know enough shell scripting to even start...)


Based on this tip, I wrote the shell script below, which works great, but only if I run it within Terminal. Is there any way to run this script 'invisibly', say, from Spotlight or via AppleScript do shell script, and have it respond until I exit?

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi
while [ "$k" != "^[" ]; do
    case "$k" in
        1) ddcctl -d 1 -b 10;;
        2) ddcctl -d 1 -b 20;;
        3) ddcctl -d 1 -b 30;;
        # ...etc...
        0) ddcctl -d 1 -b 100;;
    k="`cat -v`"
if [ -t 0 ]; then stty sane; fi
exit 0
  • You'll need a different way of capturing key-presses if you want to capture not only within your focused window but system wide. I would suggest that instead you bind the keys you want to use to each execute your apple script with a simple parameter (i.e. bind for example ctrl-1 to execute your script with the parameter 1).
    – jksoegaard
    Nov 8, 2016 at 12:08

2 Answers 2


The easiest way of accomplishing this is probably to use Automator to create a Service. The service should just execute your a bash script similar to this:

ddcctl -d 1 -b 10

Then in System Preference under Keyboard, add a keyboard shortcut to activate your Service.

This way your shortcut will work across all programs.

You'll probably need to do this for each of the 10 keyboard shortcuts you need.

  • Yeah, I know many ways to run a single command from a keystroke; but this is not what I want. I want to launch something, then be able to adjust brightness via 1-9.
    – d0g
    Nov 8, 2016 at 13:15
  • This can still be achieved using the same method. The bash script that is executed by the keyboard shortcut could just check if your "something" is launched - if it isn't, it doesn't do anything. Then create a dummy program that when launched just hangs around doing nothing until it is closed down.
    – jksoegaard
    Nov 8, 2016 at 15:17
  • Alternatively if you want to create a standalone program that does this is to use the addGlobalMonitorForEvents method in NSEvent in AppKit. However, I have no idea how easy that is to do from AppleScript. It is relatively easy to do so from Swift however.
    – jksoegaard
    Nov 8, 2016 at 15:19

I have just found an amazing solution. You can change the brightness with -b [+-]number. For example:

#!/bin/bash ddcctl -d 1 -b +10

Reference: How to get system volume control on Dell monitor .

  • Reread the OP. This does not in any way answer the question asked in the OP! Aug 19, 2017 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .