Skip to content
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

applicationElement & attributeValue has a delay for com.apple.WebKit.WebContent #3719

Open
muescha opened this issue Dec 1, 2024 · 4 comments

Comments

@muescha
Copy link
Contributor

muescha commented Dec 1, 2024

Where:

  • hs.axuielement.applicationElement
  • hs.axuielement.applicationElementForPID
  • and element:attributeValue()

where I see that the attributeValue takes the most time.

with:

  • com.apple.WebKit.WebContent

I have an speedup with when I skip it:

    if app:bundleID() == "com.apple.WebKit.WebContent" then
        print(app:bundleID())
        print(app:name())
        print("=====> skip Web")
        return nil
    end

Apps which have a WebContent:

2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Shortcat Web Content
2024-12-01 15:07:50: =====> skip Web
2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Raycast Web Content
2024-12-01 15:07:50: =====> skip Web
2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Raycast Web Content
2024-12-01 15:07:50: =====> skip Web
2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Stats-Webinhalt
2024-12-01 15:07:50: =====> skip Web
2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Stats-Webinhalt
2024-12-01 15:07:50: =====> skip Web
2024-12-01 15:07:50: com.apple.WebKit.WebContent
2024-12-01 15:07:50: Mountain Duck-Webinhalt
2024-12-01 15:07:50: =====> skip Web

My full Code:

local function findMenuExtrasMenuBarForApplication(app)

    if app:bundleID() == "com.apple.WebKit.WebContent" then
        print(app:bundleID())
        print(app:name())
        print("=====> skip Web")
        return nil
    end

    local appElement = axuielement.applicationElement(app)

    if appElement then
        local menuBar = appElement:attributeValue("AXExtrasMenuBar")
        if menuBar then
            return menuBar
        end
    end

    return nil
end


local function getMenuItemsForAllApps()

    local runningApplications = hs.application.runningApplications()

    local allMenuItems = {}

    for _, app in ipairs(runningApplications) do
        local children = findMenuExtrasMenuBarForApplication(app)
        if children then
            table.insert(allMenuItems, { app = app, children = children })
        end
    end
    return allMenuItems
end

@muescha
Copy link
Contributor Author

muescha commented Dec 1, 2024

I guess also the attributeNames in startAppWatcher in file hs.window.filter takes too much time, because skipping WebContent also speed it up:

Fix:
muescha/dot_hammerspoon@5c33399#diff-34576d415ad1a6785e8df2c0fc5cdc34c172da2f13f13baed8d4676108b4dd7dR1413

local function startAppWatcher(app,appname,retry,nologging,force)
  if not app or not appname then log.e('called startAppWatcher with no app') return end
  if apps[appname] then return not nologging and log.df('app %s already registered',appname) end
+ if app:bundleID() == "com.apple.WebKit.WebContent" then log.df('app %s is com.apple.WebKit.WebContent',appname) return end
  if app:kind()<0 or not windowfilter.isGuiApp(appname) then log.df('app %s has no GUI',appname) return end
  if not fnutils.contains(axuielement.applicationElement(app):attributeNames() or {}, "AXFocusedWindow") then
      log.df('app %s has no AXFocusedWindow element',appname)
      return
  end
[...]

Credits to: @avegetablechicken #3712

@muescha
Copy link
Contributor Author

muescha commented Dec 1, 2024

I would open an PR for window_filter.lua but I think looking in the root cause would be better:
why applicationElement and attributeNames and attributeValue are take longer time with WebContent apps?

@SilverEzhik
Copy link

I use this workaround for dealing with WebContent: #2943 (comment)

@muescha
Copy link
Contributor Author

muescha commented Dec 14, 2024

I seems a patch for window_filter.lua would be nice to have.

and a special PR to add a filter for a bundleID?

https://github.com/Hammerspoon/hammerspoon/blob/master/extensions/window/window_filter.lua#L103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants