Skip to content
jigish edited this page Jan 24, 2013 · 60 revisions

This page documents the JavaScript config feature of Slate. This feature is available in Slate version 1.0.19 and above.

Many thanks to mgax for the initial code.

Appetizer

Normally, the way Slate is configured, a truly dynamic operation is impossible.

For example, lets say I'm a nutjob and I wanted the keystroke ctrl+1 to:

  • if the window's title is "OMG I WANT TO BE FULLSCREEN" then fullscreen the window regardless of the application.
  • push the window to the right if the application of the current window is iTerm
  • push the window to the left if the application is Google Chrome
  • push the window to the top if the application is anything else

And again, I want the keystroke ctrl+1 to handle all of this. In the typical Slate config, this is impossible.

Enter JavaScript Configs:

// Create Operations
var pushRight = slate.operation("push", {
  "direction" : "right",
  "style" : "bar-resize:screenSizeX/3"
});
var pushLeft = slate.operation("push", {
  "direction" : "left",
  "style" : "bar-resize:screenSizeX/3"
});
var pushTop = slate.operation("push", {
  "direction" : "top",
  "style" : "bar-resize:screenSizeX/2"
});
var fullscreen = slate.operation("move", {
  "x" : "screenOriginX",
  "y" : "screenOriginY",
  "width" : "screenSizeX",
  "height" : "screenSizeY"
});
// Bind A Crazy Function to 1+ctrl
slate.bind("1:ctrl", function(win) {
  // here win is a reference to the currently focused window
  if (win.title() === "OMG I WANT TO BE FULLSCREEN") {
    win.doOperation(fullscreen);
    return;
  }
  var appName = win.app().name();
  if (appName === "iTerm") {
    win.doOperation(pushRight);
  } else if (appName === "Google Chrome") {
    win.doOperation(pushLeft);
  } else {
    win.doOperation(pushTop);
  }
});

Definitely verbose, but they can do much more than normal configs. Yum.

Main Course

Disclaimer: I have only done basic smoke screen testing on this so far. It is definitely possible that some things don't work as expected. If that is the case please create an issue and I'll take a look.

To use JavaScript configs, create the file .slate.js in your home directory. You can use .slate.js alongside your .slate file if you like and Slate will load both (.slate first, then .slate.js). All JavaScript configs should go into the .slate.js file.

In the .slate.js file you will have access to the following global objects:

The slate JavaScript Object

The slate object serves two purposes: configuring Slate and accessing information about the your environment and windows.

The slate object is aliased to S. For example, instead of calling slate.log("hi"); you can simply call S.log("hi");. Most of the config APIs are aliased as well. See their sections below for their aliases.

slate Config APIs

slate.config

Alias: slate.cfg or S.cfg

slate.config(name, value);
  • name is the name of the Slate config. Must be a String.
  • value is the value of the Slate config. Depending on the config it can be a String, Number, Boolean, Array, or even a Function that returns one of them.

Here is a list of possible configs.

slate.configAll

Alias: slate.cfga or S.cfga

slate.configAll({
  name1 : value1,
  name2 : value2,
  ...
});
  • names and values are the same as slate.config.

Here is a list of possible configs.

slate.operation

Alias: slate.op or S.op

var object = slate.operation(name, params);
  • object - the created operation. can be used to reference this operation in other APIs.
  • name - the operation name. must be a String. e.g. "move".
  • params - the operation's parameters. must be a Hash. more information on the parameters that operations take can be found here.

Here is list of possible operations.

slate.operationFromString

Alias: slate.opstr or S.opstr

var object = slate.operationFromString(operationString);
  • object - the created operation. can be used to reference this operation in other APIs.
  • operationString - the original-style Slate config for the operation. must be a String. e.g. "push right".

Here is the original-style config reference.

slate.bind

Alias: slate.bnd or S.bnd

slate.bind(keystroke, action, repeat)
  • keystroke - the keystroke used to activate this binding. must be a String. more information about how to set keystrokes can be found here. e.g. "1:ctrl".
  • action - the action to perform when this binding is activated. must be an Operation object or a JavaScript function.
  • repeat - whether to repeat this binding if the keystroke is held. if undefined, the config repeatOnHoldOps will be used to determine if this operation should be repeated. must be undefined or a Boolean.

slate.bindAll

slate.layout

slate.default

slate.source

slate Info APIs

slate.shell

slate.window

slate.windowUnderPoint

slate.app

slate.eachApp

slate.screen

slate.screenCount

slate.screenForRef

slate.screenUnderPoint

slate.isPointOffScreen

slate.isRectOffScreen

slate.eachScreen

slate.log

Clone this wiki locally