David
David

Reputation: 14404

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?

Upvotes: 6

Views: 3144

Answers (1)

Clark
Clark

Reputation: 818

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

Upvotes: 1

Related Questions