Skip to content

Commit

Permalink
improve browser helpers and add outlook tab finder
Browse files Browse the repository at this point in the history
  • Loading branch information
brettinternet committed Apr 2, 2024
1 parent 39f4d99 commit fae64bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 7 additions & 0 deletions mac/.hammerspoon/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ local actions = {
["/play_pause_youtube"] = playPauseOrOpenYoutube,
["/bounce_displays"] = bounceDisplays,
["/sleep"] = systemSleep,
["/outlook"] = function()
local success, windowId = openBrowserTab("com.google.Chrome", "outlook.office.com")
print(windowId)
if windowId then
hs.application.get("com.google.Chrome"):activate()
end
end,
["/calendar"] = function()
openDisposableBrowserWindow("com.google.Chrome", "https://pdq-hq.slack.com/archives/D03JDATJ7D5")
hs.application.get("com.tinyspeck.slackmacgap"):activate()
Expand Down
19 changes: 7 additions & 12 deletions mac/.hammerspoon/media.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,18 @@ hs.hotkey.bind(nil, "f13", toggleAudioOutput)

-- Play/Pause YouTube

local function openBrowserTab(browser, url)
function openBrowserTab(browser, url)
local script = ([[(function() {
var browser = Application('%s');
browser.activate();
var tabIndex;
for (win of browser.windows()) {
tabIndex = win.tabs().findIndex(tab => tab.url().match(/%s/));
var tabIndex = win.tabs().findIndex(tab => tab.url().match(/%s/));
if (tabIndex !== -1) {
win.activeTabIndex = (tabIndex + 1);
win.index = 1;
break;
return win.id();
}
}
return tabIndex;
})();
]]):format(browser, url)
return hs.osascript.javascript(script)
Expand All @@ -97,7 +94,6 @@ end
local function openNewBrowserWindow(browser, url)
local script = ([[(function() {
var browser = Application('%s');
browser.activate();
var window = browser.Window().make();
window.tabs[0].url = '%s';
return window.id();
Expand All @@ -110,15 +106,15 @@ function playPauseOrOpenYoutube()
local currentApp = hs.application.frontmostApplication()
local browser = "com.google.Chrome"
local chrome = hs.application.get(browser)
local success, tabIndex = openBrowserTab(browser, "youtube.com")
local success, windowId = openBrowserTab(browser, "youtube.com")
if success then
if tabIndex > -1 then
if windowId then
local playKey = hs.eventtap.event.newKeyEvent(nil, "k", true)
playKey:post(chrome)
else
local success = openNewBrowserWindow(browser, "https://youtube.com")
if success then
hs.application.launchOrFocusByBundleID(browser)
chrome:activate()
end
end
end
Expand Down Expand Up @@ -149,7 +145,6 @@ function openDisposableBrowserWindow(browser, url)
}
})();
]]):format(browser, windowId)
hs.osascript.javascript(script)
-- hs.timer.doAfter(1, function() hs.osascript.javascript(script) end)
hs.timer.doAfter(0.5, function() hs.osascript.javascript(script) end)
end
end

0 comments on commit fae64bd

Please sign in to comment.