Skip to content

Instantly share code, notes, and snippets.

@Saeven
Last active June 16, 2022 14:50
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Saeven/4127bd474a8e11fdd80793e494166dc4 to your computer and use it in GitHub Desktop.
Save Saeven/4127bd474a8e11fdd80793e494166dc4 to your computer and use it in GitHub Desktop.
Hammerspoon script to launch and tile apps
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
@Valentine520
Copy link

but how can i launch a window for my app which need luanch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment