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

Control applications in dock #953

Closed
404pilot opened this issue Aug 12, 2016 · 8 comments
Closed

Control applications in dock #953

404pilot opened this issue Aug 12, 2016 · 8 comments

Comments

@404pilot
Copy link

Not sure if this is a right place to ask questions.

Basically, what I am trying to do is to bind a shortcut to open 1st, 2nd, 3rd ... application in dock.

I have already checked the documentation here: http://www.hammerspoon.org/docs/hs.dockicon.html

But that is not what I want.

Thanks.

@nikitavoloboev
Copy link

Why not just bind hotkeys to applications themselves?

@asmagill
Copy link
Member

Currently choosing an application icon which is in the Dock is not directly possible... you have to know the application's name or bundle-id to specify which application to activate/open with hs.application, and there is no easy way to get the list of what applications are in (or not in) the Dock. If you know the exact location of the Dock, you could simulate mouse button presses with hs.eventtap, but this would be extremely fragile depending upon dock size, magnification settings, number of icons, etc. And hs.application("Dock"):allWindows() returns an empty table, so I'm not even sure at present how to get the dock's current location with the core modules included with Hammerspoon.

That said, if you're willing to use an experimental, still in progress, external module, I have been working on one at https://github.com/asmagill/hammerspoon_asm/tree/master/axuielement which should be able to do what you want... it's not currently (and may or may not ever be) part of the Hammerspoon core, so you'll need to manually add it, and it is still a work in progress, so syntax, etc. may change in the future, but I think something like this might do what you want:

ax = require("hs._asm.axuielement")
d = hs.application("Dock")
iconNumber = 1 -- position in the dock, starting with 1 being the furthest left item
icon = ax.applicationElement(d)[1][iconNumber]
if icon:roleDescription() == "application dock item" then
    icon:doPress()
else
    print ("not an application")
end

The exact mechanisms behind hs._asm.axuielement is discussed somewhat at the site listed above, but at its most basic, ax.applicationElement(d) is the root accessibility object for the Dock application, and it's first (and only) child object is the list of dock items (thus the [1]). The list's children are the Dock items in order (thus the [iconNumber]), and each dock item has some actions defined, the one we care about being doPress().

@404pilot
Copy link
Author

@nikitavoloboev @asmagill thanks!

@dgghita
Copy link

dgghita commented Apr 23, 2019

Hi and sorry to revive this thread. What was the outcome of the axuielement module? I see it's no longer available in the hammerspoon_asm repo. Thanks in advance!

(I'm asking for the same reason: to automate access to the first 10 apps in dock using ctrl+1, 2, 3,... or something similar)

@latenitefilms
Copy link
Contributor

@dgghita
Copy link

dgghita commented Apr 23, 2019

@latenitefilms - Thanks so much!

(Off-topic: this looks like so much fun, can't wait to try it.)

@marcelgerber
Copy link

For anyone coming here in the year 2023, this is what I used for this feature and it works beautifully in macOS Ventura:

d = hs.application("Dock")
for i=1,9 do
    hs.hotkey.bind({"alt"}, tostring(i), function()
        iconNumber = i
        icon = hs.axuielement.applicationElement(d)[1][iconNumber]
        if icon:attributeValue("AXRoleDescription") == "application dock item" then
            icon:performAction("AXPress")
        else
            print ("not an application")
        end
    end)
end

@Write
Copy link

Write commented Jan 8, 2024

For anyone coming here in the year 2023, this is what I used for this feature and it works beautifully in macOS Ventura:

d = hs.application("Dock")
for i=1,9 do
    hs.hotkey.bind({"alt"}, tostring(i), function()
        iconNumber = i
        icon = hs.axuielement.applicationElement(d)[1][iconNumber]
        if icon:attributeValue("AXRoleDescription") == "application dock item" then
            icon:performAction("AXPress")
        else
            print ("not an application")
        end
    end)
end

Hello,
Is there an update for this on Sonoma ? Everything returns 'not an application for me'
Thanks

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

7 participants