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

hs.application constructors returning duplicate objects #2720

Closed
joshkaplan opened this issue Mar 4, 2021 · 6 comments · Fixed by #2723
Closed

hs.application constructors returning duplicate objects #2720

joshkaplan opened this issue Mar 4, 2021 · 6 comments · Fixed by #2723
Assignees
Milestone

Comments

@joshkaplan
Copy link

All hs.application constructors seem to be returning duplicate objects. I've tested hs.application.get() and hs.window.focusedWindow():application().

> hs.application.get('Google Chrome')
hs.application: Google Chrome (0x600000f95938)

> hs.application.get('Google Chrome')
hs.application: Google Chrome (0x600000f03ff8)

> hs.application.get('Google Chrome')
hs.application: Google Chrome (0x600000fad6b8)

I have also noticed that hs.application.isFrontmost now always returns returns false. I suspect the two are related?

@joshkaplan
Copy link
Author

If others are experiencing issues, a temporary workaround for determining the front most application would be to compare bundleIDs:

hs.window.focusedWindow():application():bundleID() == hs.application.get('Google Chrome'):bundleID()

@ash14
Copy link

ash14 commented Mar 4, 2021

+1, on Big Sur 11.2.1:

Version 0.9.82 (5614)

> app = hs.application.frontmostApplication()
2021-03-04 11:21:17: -- Loading extension: application


> app
hs.application: Hammerspoon (0x60000160c6b8)

> app:isFrontmost()
true

Version 0.9.83 (5819)

> app = hs.application.frontmostApplication()
2021-03-04 11:22:01: -- Loading extension: application


> app
hs.application: Hammerspoon (0x6000022d6f78)

> app:isFrontmost()
false

@dasmurphy
Copy link

I can confirm the bug also on MacOS 10.14.6. All tests from @joshkaplan and @ash14 have the same results.

@cmsj cmsj self-assigned this Mar 4, 2021
@cmsj
Copy link
Member

cmsj commented Mar 4, 2021

I rewrote isFrontmost() for 0.9.83 to try and make it more reliable. It seems like I missed something. Thanks for the reports, I'll investigate and probably push out a 0.9.84 quickly to at least revert it.

@gp42
Copy link

gp42 commented Mar 4, 2021

my apologies, opened a similar issue: #2721
Will close it, lets track this issue here.

@cmsj
Copy link
Member

cmsj commented Mar 4, 2021

So just to give a bit of background here, the hs.application objects being different C pointers is expected behaviour - hs.application objects are pretty lightweight Objective C objecst that don't contain any state of their own, so it's easier/simpler to create new ones as needed than it is to try and cache/reuse them. We provide Lua metamethods like __eq so that comparisons between the objects behaves as expected:

> foo1 = hs.application.get('Safari')
> foo2 = hs.application.get('Safari')
> foo1
hs.application: Safari (0x6000026c3e78)
> foo2
hs.application: Safari (0x6000029408b8)
> foo1 == foo2
true

The issue here is that :isFrontmost() was consistently failing in CI and I decided to rewrite it (24e0b05) to use a different underlying strategy which passes CI, but it turns out that our CI tests for hs.application are not working as intended.

I'm reverting that patch in #2723 and I'll release a 0.9.84 brownbag later today.

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

Successfully merging a pull request may close this issue.

5 participants