Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Apple Script to control backlit keyboard?

Is there a way to use an Apple Script to control the brightness of the backlit keyboard on a Macbook?

The backlit keys are the F5 and F6.

Edit:

Based on the suggestion of @Clark I tried the follow, but it does not work.

    NSAppleScript *run = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to key code 96"];
    [run executeAndReturnError:nil];

Any suggestions?

like image 692
David Avatar asked Sep 25 '11 00:09

David


People also ask

How do I control the lights on my Mac keyboard?

Manually adjust keyboard backlighting or turn it off . In Control Center, click Keyboard Brightness, then drag the slider. In the Touch Bar, expand the Control Strip, then tap the increase brightness button or the decrease brightness button . To turn backlighting off, touch and hold.


1 Answers

Amit Singh has a chapter on how to do this from C.

https://web.archive.org/web/20200103164052/https://osxbook.com/book/bonus/chapter10/light/

It'd be easy to compile the sample code on that page and call it from Applescript.

To make Applescript type a function key you have to use the key code. The key codes fore the function keys are:

  • F1 = 122
  • F2 = 120
  • F3 = 99
  • F4 = 118
  • F5 = 96
  • F6 = 97
  • F7 = 8
  • F8 = 100
  • F9 = 101
  • F10 = 109
  • F11 = 103

To type one do something like this:

tell application "System Events" to key code 96

like image 161
Clark Avatar answered Oct 05 '22 02:10

Clark