• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Low level MacOS management in JavaScript via BetterTouchTool

btt

Package logo

Package version Vunerabilities

Vunerabilitiesn

Manage your BetterTouchTool in JavaScript, easily.

Get started

See the guide and api for btt.js.

About

This package is a handy wrapper over BetterTouchTool built in webserver API. (by @Andreas Hegenberg)

This package will allow you to automate you MacOS-running machine using JavaScript. You'll be able to:

  • Create event listeners that'll run within an operating system, outside the browser!
  • Toggle your do-not-disturb state
  • Show a system notification
  • Toggle Night Shift
  • Sleep your computer after timeout
  • Create your own touchbar widgets
  • Feel a notification via haptic engine
  • Control the brightness of the screen and keyboard
  • Control the volume levels
  • Use the content of your clipboard to be opened in a specific url or application
  • Create your own UIs "within system" using web view
  • Trigger a system-wide keyboard shortcut
  • Send a shortcut to a specific application
  • Show / Hide / Open / Quit a specific applcation
  • Move your mouse to a specific position and click
  • Hide your cursor
  • Lock / Unlock your MacOS machine
  • Integrate your flow / touchbar with various APIs ...

and anything else that BetterTouchTool or the JavaScript specification will allow you to do!

Typed, browser/server side library

This package provides its own type definitions and can be run both on browser (using module bundlers) and in a nodejs environment.

Requirements

This package depends on the application BetterTouchTool in at least version v2.0.0, you need to have it installed and running before going further.

Then, please enable and configure the webserver in the BetterTouchTool preferences. You're now ready to go!

Installation

npm install btt

Example usage

First, create a btt instance passing the required data for BTT webserver.

// import Btt class from the package
import { Btt } from 'btt';

// create an instance representing btt webserver
// can be remote or local
const btt = new Btt({
  domain: '127.0.0.1',
  port: 8000,
  protocol: 'http',
  version: '2.525',
});

Now you can invoke the actions - there are plenty of ways to do it, and all are promise-based.

// sequentially run three actions - spotlight, type text and night shift
// as all actions are promise-based, you can use async/await notation without hassle
btt
  .triggerShortcut('cmd+space').invoke()
  .then(() => btt.sendText({ text: 'Hello world!'}).invoke())
  .then(() => btt.toggleNightShift().invoke());

Response structure for every action

// every single action returns a CallResult object containing information about the Call

interface CallResult {
  time: number;     // contains time in MS that this action took to perform (including fetch time)
  status: number;   // contains an HTTP status / string
  value: any;       // depending on the method used, may return an array, object or fetch result
  note?: string;    // an additional note for the user
}

Chaining methods

// you can also use a custom chain method to simplify even more and avoid using async/await
btt
  .invokeChain()                      // 1)
  .triggerShortcut('cmd+space')       // 2)
  .sendText({text: 'Hello world!'})   // 3)
  .wait(1000)                         // 4)
  .toggleNightShift()                 // 5)
  .call()                             // 6)
  .then(v => console.info(v))         // 7)

// Explanation:
// 1) Starts method chaining
// 2) Action that a user wants to perform
// 3) Action that a user wants to perform
// 4) Additional method available in chain only - wait before triggering next action
// 5) Action that a user wants to perform
// 6) Invokes all previously-defined actions, ensuring the execution order
// 7) Returns a promise that resolves once all of the actions are fulfilled. 
//    Contains information about the status of the chain (time, value, status)

Event listeners

You can even register a system-wide event listener within BTT that'll trigger particular actions

// creates a trigger in BetterTouchTool. Keep in mind that this is persistent until you manually delete it!
btt.addTriggerAction('oneFingerForceClick', (ev) => {

  // create a list of actions that you want to perform on single finger force click
  const actionsToInvoke = [
    btt.showHUD({
      title: 'Wow!',
      details: 'I triggered! 😍',
    }),
  ];
  
  // and push them to `actions` property in the event object.
  ev.actions.push(...actionsToInvoke);
});

// you can also delete an event listener - trigger: 
btt.removeTriggerAction('oneFingerForceClick', callbackFuntion);

The above method will trigger the callback upon running your script, not when a particular event really occurs. If you need to call a function upon event recognition, you need to use btt-node-server and use the addEventListener and removeEventListener methods on the btt instance. The callback you provide will run in the nodejs environment, within vm.

const btt = new Btt({
  domain: '127.0.0.1',
  port: 8000,
  protocol: 'http',
  version: '2.525',
  // pass eventServer to use this part of the lib
  eventServer: {
    domain: 'localhost',
    port: 8888,
  },
});

// adds real event listener, that'll run once event occurs
btt.addEventListener('cmd+ctrl+alt+u', async (ev) => {
  // write the code as you'd normally do -> trigger the action for some interval
  const intervalID = setInterval(() => {
    btt.showHUD({ title: 'It works!'}).invoke();
  }, 1000);

  // you can use fetch API here or anything that your node version will support

  // stops the interval after 10 seconds
  await new Promise((res, rej) => {
    setTimeout(() => {
      clearTimeout(intervalID);
      res();
    }, 10000);
  });
  
  // the value you return from the callback will be the response of the btt-node-server 
  return { messsage: 'Hello world!' };
});

To get all available events, you have to look in the enums (list of all valid events will be available soon). Most of the time you can just guess because all event names are the lowercased equivalent of the triggers from within BetterTouchTool.

Additional action information

For use within the browser, you can get the url that lies behind all actions and assign it to some <a href="${link}">Link</a>. To get link you simply need to read the .url property of any action:

console.log(
  btt.triggerShortcut('cmd+space').url
);

If you want to have a peak at the generated action JSON, or want to share it with others who use BetterTouchTool you can read the .json property of any action.

console.log(
  btt.showHUD({ title: 'Hello!' }).json
);

More examples

For more advanced examples visit the example section

Notice

Keep in mind that this module only provides handy utility functions that send requests to BTT built in webserver. So depending on your BTT version, some actions may be glitchy. Do not hestitate to report those issues here or in the official BTT community forum.

Also, keep in mind that accessing any kind of low level APIs from JS may be dangerous, make sure to stay secure

Related projects:

  • btt - BetterTouchTool management in JS
  • btt-node-server - Simple express server, required for advanced event listener handling
  • btt-node Premature version of this package (btt) - deprecated

License

MIT

More Repositories

1

brackets-pastetobin

Brackets extension that allows upload snippets to pastebin quickly
JavaScript
27
star
2

blindify

Web extension that simulates being blind, forcing you to use screen reader to navigate through web
TypeScript
5
star
3

btt-node

Easier management over BetterTouchTool from within nodejs environment
TypeScript
5
star
4

docker-glyphhanger

A glyphhanger inside docker
Shell
4
star
5

vue-carbon

Carbon Design System components - in vue.js!
JavaScript
2
star
6

cships

My first "real game" ever with highscores and progress save. Bunch of ifology and not DRY code - but it worked! Made in 2012
C++
2
star
7

btt-node-server

Node client for btt, using websockets to recieve custom events
TypeScript
2
star
8

react-recruitment-task

A recruitment task to one of the software companies
JavaScript
2
star
9

gamepad-game

TypeScript
1
star
10

ola-node

JavaScript
1
star
11

btt-presets

My presets I've decided to made public
1
star
12

kie-node

Backend API for KIE project
JavaScript
1
star
13

kie-static-builder

Static site builder for KIE project
JavaScript
1
star
14

scss-demo-dominika

CSS
1
star
15

btt-read-selection

JavaScript
1
star
16

kie-editor

Snippet editor of KIE project
1
star
17

kie-example-content

Example content to work on for KIE project
HTML
1
star
18

grzegorz-demo

demo do edu-cli
1
star
19

Dragon-Dash

Tap 3 Dragon Ball game (FRONT END)
JavaScript
1
star
20

btt-touchbar-widgets

Widgets for my personal setup
1
star
21

btt-clipboard-share-client

JavaScript
1
star
22

js-image-finder

Find and review images in your OS
JavaScript
1
star
23

dominika-demo

Inidividual lessons of git
JavaScript
1
star
24

dominika-npm

Example repo for lessons with Dominika
JavaScript
1
star
25

simple-http-hosting-dominika

Inidividual lessons
HTML
1
star
26

kie-creator

Brackets extension for creating data in KIE project
JavaScript
1
star
27

teachmeweb

Otwarte repozytorium w którym przechowywane będą przykłady z lokalnego projektu o tej samej nazwie, TeachMeWeb
HTML
1
star
28

brackets-schema-preview

Preview your schema from inside brackets
JavaScript
1
star
29

kie

Core repository for KIE project
JavaScript
1
star
30

kie-front

Front-end part of KIE project
JavaScript
1
star
31

pbc-api

api for pbc project
TypeScript
1
star