Skip to content

Instantly share code, notes, and snippets.

@andyHa
Last active November 25, 2015 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyHa/2332fce1126f032b5294 to your computer and use it in GitHub Desktop.
Save andyHa/2332fce1126f032b5294 to your computer and use it in GitHub Desktop.
/**
* Use with: https://github.com/kasper/phoenix
*
* I start my apps using F-keys provided by the apple keyboard which are
* otherwise rigorously ignored by other applications.
*
* ^ + CMD + <- and -> move windows from one screen to another
* ^ + CMD + UP maximizes the current window
* CMD + < cycles through windows of the current application
**/
App.allWithTitle = function( title ) {
return _(this.runningApps()).filter( function( app ) {
if (app.title() === title) {
return true;
}
});
};
App.focusOrStart = function ( title, offset, windowFilter ) {
var apps = App.allWithTitle( title );
if (_.isEmpty(apps)) {
api.launch(title);
return;
}
if (windowFilter == null) {
windowFilter = function(x) { return true; };
}
var windows = _.chain(apps)
.map(function(x) { return x.allWindows(); })
.flatten()
.filter(windowFilter)
.value();
if (_.isEmpty(windows)) {
return;
}
var wnd = _.toArray(windows)[0];
if (wnd.isWindowMinimized()) {
wnd.unMinimize();
}
wnd.setFrame(getScreenRect(offset));
wnd.focusWindow();
};
function getScreenRect(offset) {
var win = Window.focusedWindow();
var currentScreen = win.screen();
var screens = [currentScreen];
for (var x = currentScreen.previousScreen(); x != win.screen(); x = x.previousScreen()) {
screens.push(x);
}
screens = _(screens).sortBy(function(s) { return s.frameIncludingDockAndMenu().x; });
offset = Math.min(offset, screens.length - 1);
return screens[offset].frameIncludingDockAndMenu();
}
String.prototype.startsWith = function (str) {
return !this.indexOf(str);
}
api.bind('F13', [ ], function () {
App.focusOrStart('Firefox', 1, function(wnd) { return !wnd.title().startsWith('Firebug'); });
});
api.bind('F14', [ ], function () {
App.focusOrStart('Google Chrome', 2);
});
api.bind('F15', [ ], function () {
App.focusOrStart('Slack', 2);
});
api.bind('F16', [ ], function () {
App.focusOrStart('Sublime Text 2', 1);
});
api.bind('F17', [ ], function () {
App.focusOrStart('IntelliJ IDEA', 0);
});
api.bind('F18', [ ], function () {
App.focusOrStart('Terminal', 1);
});
api.bind('F19', [ ], function () {
App.focusOrStart('Finder', 1);
});
api.bind('LEFT', [ 'ctrl', 'cmd' ], function () {
var wnd = Window.focusedWindow();
wnd.setFrame(wnd.screen().nextScreen().frameIncludingDockAndMenu());
});
api.bind('RIGHT', [ 'ctrl', 'cmd' ], function () {
var wnd = Window.focusedWindow();
wnd.setFrame(wnd.screen().previousScreen().frameIncludingDockAndMenu());
});
api.bind('UP', [ 'ctrl', 'cmd' ], function () {
var wnd = Window.focusedWindow();
wnd.maximize();
});
function findWindow(window, windows) {
index = 0;
while(windows.length > index && windows[index].title() != window.title()) {
index++;
}
return index < windows.length ? index : -1;
}
api.bind('<', [ 'cmd' ], function () {
wnds = _.filter(Window.visibleWindowsMostRecentFirst(), function(wnd) { return wnd.app().title() == Window.focusedWindow().app().title(); });
index = findWindow(Window.focusedWindow(), wnds);
if (index >= 0) {
index--;
if (index < 0) {
index = wnds.length - 1;
}
wnd = wnds[index];
wnd.focusWindow();
}
});
api.bind('<', [ 'cmd', 'shift' ], function () {
wnds = _.filter(Window.visibleWindowsMostRecentFirst(), function(wnd) { return wnd.app().title() == Window.focusedWindow().app().title(); });
index = findWindow(Window.focusedWindow(), wnds);
if (index >= 0) {
index++;
if (index > wnds.length - 1) {
index = 0;
}
wnd = wnds[index];
wnd.focusWindow();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment