Created
April 18, 2023 11:28
-
-
Save fragmede/f1794ee3efc20da1106305e98b8b0e56 to your computer and use it in GitHub Desktop.
Hammerspoon script that creates a new Space/virtual desktop and sticks a new Chrome window into it with ⌘⌃⌥N
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
-- Grab the ID for the last desktop, which is assumed to be the most recently create one | |
function getLargestDesktopKey() | |
rawSpaceNames = hs.spaces.missionControlSpaceNames() | |
for key, value in pairs(rawSpaceNames) do | |
uuid = key | |
end | |
spaceNames = rawSpaceNames[uuid] | |
for _, nested_table in pairs(rawSpaceNames) do | |
for __, value in pairs(nested_table) do | |
local desktopNum = string.match(value, "Desktop (%d+)") | |
if desktopNum ~= nil then | |
desktopNum = tonumber(desktopNum) | |
if largestDesktop == nil or desktopNum > largestDesktop then | |
largestDesktop = desktopNum | |
end | |
end | |
end | |
end | |
desktop = string.format("Desktop %d", largestDesktop) | |
desktopKey = nil | |
for key, value in pairs(spaceNames) do | |
if value == desktop then | |
return key | |
end | |
end | |
return nil | |
end | |
-- return true if a new chrome window was found. New chrome windows are assumed to start | |
-- with a title of ' - Google Chrome - '. Windows of pages with not title presumable also | |
-- start that way though. | |
function foundNewChromeWin() | |
tableOfChromeWindows = hs.fnutils.filter(hs.window.visibleWindows(), function(win) | |
return win:application():name() == "Google Chrome" and not win:isMinimized() | |
end) | |
for key, value in pairs(tableOfChromeWindows) do | |
print(key, value) | |
if string.starts(value:title(), " - Google Chrome - ") then | |
return true | |
end | |
end | |
return false | |
end | |
-- https://stackoverflow.com/questions/22831701/lua-read-beginning-of-a-string | |
-- string.match(String, "^Start") ~= nil should also work but does not for some reason, | |
-- despite working in the console. | |
function string.starts(String,Start) | |
if string.len(String) == 0 then | |
return false | |
end | |
return string.sub(String, 1, string.len(Start)) == Start | |
end | |
-- Create a new space and stick a fresh Chrome window in it | |
-- Define a hotkey to trigger the script | |
hs.hotkey.bind({"cmd", "ctrl", "alt"}, "N", function() | |
-- Create a new Chrome window and wait for it to be created | |
hs.application.launchOrFocus("Google Chrome") | |
hs.timer.waitUntil(function() | |
return hs.application.frontmostApplication():name() == "Google Chrome" | |
end, function() | |
hs.eventtap.keyStroke({"cmd"}, "n", 0) -- Press cmd+n to create a new window | |
hs.timer.waitUntil(foundNewChromeWin, function() | |
newChromeWindow = hs.fnutils.find(hs.window.visibleWindows(), function(win) | |
return string.starts(win:title(), " - Google Chrome -") and | |
win:application():name() == "Google Chrome" and not win:isMinimized() | |
end, .1) | |
print("newChromeWindow found:", newChromeWindow) | |
-- Add new space, find its key | |
hs.spaces.addSpaceToScreen(true) | |
largestDesktopKey = getLargestDesktopKey() | |
print("largestDesktopKey ", largestDesktopKey) | |
if largestDesktopKey ~= nil then | |
-- Finally, move the window to the last desktop and focus that desktop | |
hs.spaces.moveWindowToSpace(newChromeWindow, largestDesktopKey, true) | |
hs.spaces.gotoSpace(largestDesktopKey) | |
end | |
end, .1) | |
end, .1) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment