8000 Use Safari web apps · Issue #37 · seanfisk/combootcha · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use Safari web apps #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
seanfisk opened this issue Mar 6, 2024 · 0 comments
Open

Use Safari web apps #37

seanfisk opened this issue Mar 6, 2024 · 0 comments
Labels
blocked External factor preventing completion

Comments

@seanfisk
Copy link
Owner
seanfisk commented Mar 6, 2024

I cannot currently use these as my MacBook is too old and limited to Sonoma. However, I want to preserve some code I've written for it.

Consider checking for the most updated code before importing this.

Setup instructions:

#### Safari web apps

macOS Sonoma introduced a feature called [Safari web apps][] which are macOS' implementation of a generic feature called a [site-specific browser][]. While [Firefox](https://pwasforfirefox.filips.si/) and [Google Chrome](https://support.google.com/chrome/answer/9658361?sjid=12950535267183201417-NC) also support variants of this, I've found the native macOS implementation to work best.

To create the web apps, open the following sites in Safari:

- [Gmail](https://mail.google.com/)

First, log into the site. Safari web apps use the favicon as the app icon, so we need to be logged in for the correct favicon to load.

After logging into to the site, click **File > Add to Dock…**. For the name, use the exact name in the list above. Accept the default icon and URL.

Repeat this for every site in the list.

[Safari web apps]: https://support.apple.com/en-us/104996
[site-specific browser]: https://en.wikipedia.org/wiki/Site-specific_browser

From Hammerspoon init.lua:

-- Helpful stuff:
-- https://github.com/Hammerspoon/hammerspoon/issues/1025
-- https://github.com/Hammerspoon/hammerspoon/issues/664

pasteboardWatcher = hs.pasteboard.watcher.new(function(pasteboardContents)
    pasteboardWatcher:stop()
    -- Do not use hs.pasteboard.readURL; it does not do what you think it does!
    if pasteboardContents then
      hs.urlevent.openURL(pasteboardContents)
    else
      hs.alert.show("Pasteboard contents were not a valid string")
    end
end)

-- Select Copy Link and open the URL from the pasteboard when we ⌘-Click in the given applications
leftClickWatcher = hs.eventtap.new({hs.eventtap.event.types.leftMouseUp}, function(event)
    if not event:getFlags():containExactly({""}) then
      return false -- Propagate event
    end

    pasteboardWatcher:start()
    hs.timer.doAfter(
      2, function()
        if pasteboardWatcher:running() then
          hs.alert.show("Timed out waiting for pasteboard contents")
          pasteboardWatcher:stop()
        end
    end)

    -- TODO Is there a way to do this without using UI automation?

    -- Once we right-click, the menu is delayed in appearing. Thus we do not want to post the key strokes to navigate the context menu immediately. I have tried and it doesn't work well.
    hs.timer.doAfter(
      0.1, function()
        function modKeyStroke(modifiers, key)
          -- TODO Add application parameter?
          hs.eventtap.keyStroke(modifiers, key, 0)
        end
        function keyStroke(key)
          modKeyStroke({}, key)
        end

        -- Send a right arrow event. This will focus the first menu item but won't advance the selection.
        keyStroke("right")
        -- Now jump down to the next section
        modKeyStroke({""}, "down")
        -- Now up one item to the Copy Link
        keyStroke("up")
        keyStroke("return")
    end)

    -- Delete original event and post replacement right-click event
    local replacementEvents = {}
    local mousePosition = hs.mouse.absolutePosition()
    for _, type in ipairs({hs.eventtap.event.types.rightMouseDown, hs.eventtap.event.types.rightMouseUp}) do
      table.insert(replacementEvents, hs.eventtap.event.newMouseEvent(type, mousePosition, {}))
    end
    return true, replacementEvents
end)

-- https://www.lua.org/pil/11.5.html
function set(list)
  local set = {}
  for _, l in ipairs(list) do set[l] = true end
  return set
end

-- We don't use all these apps in every setup, but that's ok. It's simpler to keep them in the list and they won't be hurting anything.
safariWebAppNames = set{"Gmail", "Google Chat", "Google Calendar", "Google Keep"}
deactivateEventTypes = set{hs.application.watcher.deactivated, hs.application.watcher.terminated}

hs.application.watcher.new(function(appName, eventType, hsApplication)
    if not safariWebAppNames[appName] then
      return
    end
    if eventType == hs.application.watcher.activated then
      print("Application " .. appName .. " activated; starting left click watcher")
      leftClickWatcher:start()
      return
    end
    if deactivateEventTypes[eventType] then
      print("Application " .. appName .. " deactivated; stopping left click watcher")
      leftClickWatcher:stop()
      return
    end
    -- Ignore other event types
end):start()
@seanfisk seanfisk added the blocked External factor preventing completion label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked External factor preventing completion
Projects
None yet
Development

No branches or pull requests

1 participant
0