Last active
June 16, 2022 14:50
-
-
Save Saeven/4127bd474a8e11fdd80793e494166dc4 to your computer and use it in GitHub Desktop.
Hammerspoon script to launch and tile apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local workApplications = { 'Mail','HipChat','PhpStorm','Safari','Charles'} | |
local workApplicationWatcher; | |
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", function() | |
hs.notify.new({title="Hammerspoon", informativeText="Setting home layout"}):send() | |
local homeMonitor = "LG ULTRAWIDE" | |
local windowLayout = { | |
{"PhpStorm", nil, homeMonitor, {x=0, y=0, w=0.6, h=1}, nil, nil}, | |
{"Charles", nil, homeMonitor, {x=0.6, y=0.6, w=0.4, h=0.4}, nil, nil}, | |
{"Safari", nil, homeMonitor, {x=0.6, y=0, w=0.4, h=0.6}, nil, nil}, | |
} | |
hs.layout.apply(windowLayout) | |
end) | |
-- | |
-- Try to setup the workplace workflow | |
-- | |
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "W", function() | |
hs.notify.new({title="Hammerspoon", informativeText="Starting work applications"}):send() | |
-- | |
-- Discover which apps are needed (not launched or visible) | |
-- | |
local neededApps = {} | |
for key, value in pairs(workApplications) do | |
local app = hs.application.find(value); | |
if app == nil then | |
table.insert(neededApps,value) | |
else | |
local windows = app:allWindows(); | |
for s, t in pairs(windows) do | |
t:raise() | |
end | |
end | |
end | |
if #neededApps == 0 then | |
doWorkLayout() | |
else | |
workApplicationWatcher = hs.application.watcher.new(appLaunched) | |
workApplicationWatcher:start() | |
for key, value in pairs(neededApps) do | |
hs.application.launchOrFocus(value) | |
end | |
end | |
end) | |
-- | |
-- Watcher for launching apps, when app launches are required | |
-- | |
function appLaunched( appName, eventType, app ) | |
if eventType ~= hs.application.watcher.launched then | |
return | |
end | |
local launchCount = 0 | |
for key, value in pairs(workApplications) do | |
if hs.application.find(value) ~= nil then | |
launchCount = launchCount + 1 | |
end | |
end | |
if launchCount == #workApplications then | |
workApplicationWatcher:stop() | |
doWorkLayout() | |
end | |
end | |
-- | |
-- Success, set up the work layout | |
-- | |
function doWorkLayout() | |
local laptopMonitor = "Color LCD" | |
local samsungMonitor = "SyncMaster" | |
local lgMonitor = "LG IPS FULLHD" | |
local windowLayout = { | |
{"Mail", nil, laptopMonitor, {x=0, y=0, w=1, h=0.5}, nil, nil}, | |
{"HipChat", nil, laptopMonitor, {x=0, y=0.5, w=1, h=0.5}, nil, nil}, | |
{"PhpStorm", nil, samsungMonitor, {x=0, y=0, w=1, h=1}, nil, nil }, | |
{"Safari", nil, lgMonitor, {x=0,y=0,w=0.6, h=1}, nil, nil }, | |
{"Charles", nil, lgMonitor, {x=0.6,y=0,w=0.4, h=1}, nil, nil }, | |
} | |
hs.notify.new({title="Hammerspoon", informativeText="Applying work layout"}):send() | |
hs.layout.apply(windowLayout) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
but how can i launch a window for my app which need luanch?