hammerspoon icon indicating copy to clipboard operation
hammerspoon copied to clipboard

hs.application.find("doesntexist") is extremely slow

Open Grazfather opened this issue 3 years ago • 3 comments

It takes about 5-8 seconds on my machine, whether or not I have spotlight enable, but OSX can figure out that the app doesn't exist much faster:

> print("start"); hs.application.find("doesn't exist"); print("done")
2023-04-04 19:26:07: start
2023-04-04 19:26:14: done -- 7 seconds!
❯ time open -a doesntexist
Unable to find application named 'doesntexist'

real    0m0.036s
user    0m0.011s
sys     0m0.015s

This is something we can work around (on my end), of course, but I like to think that it's something that could be improved.

Grazfather avatar Apr 04 '23 23:04 Grazfather

For what it's worth, it returns immediately on my machine… but that machine currently has an older macOS, an older Hammerspoon, and probably lots of other differences from your setup, any of which could be causing it to behave differently.

There are a couple of important differences between the ways open -a and hs.application.find() work, so I'm not sure the difference between their times necessarily tells you anything useful:

  • open -a is asking LaunchServices to look up applications on disk that it's indexed, while hs.application.find() (most of hs.application, really) is specifically for running applications, and is thus searching for them in a very different way. Executables vs. processes, essentiallly. (You'd think that would make find() faster than open, though, given that 'running apps' ≤ 'all apps'. However:)
  • hs.application.find() also goes through all open windows, checking their titles for a match. If there are a lot of windows open, I suppose it's possible that could be slowing it down, though I still wouldn't expect it to take that long…

Does hs.window.allWindows() take a similarly long time to return? If so, does hs.inspect(hs.window._timed_allWindows()) seem to show any particular app as being responsible?

Rhys-T avatar Apr 15 '23 04:04 Rhys-T

hs.inspect(hs.window._timed_allWindows()) takes less than a second, but strangely, print("start"); hs.application.find("doesn't exist"); print("done") also is taking me less than a second today. Feel free to close thing, I'll bump it if I see it again.

Grazfather avatar Apr 23 '23 20:04 Grazfather

I was seeing the same issue (15 seconds!) . However, the strange thing is I have another machine with similar configuration (apple silicon, macintosh 12.13.1) where it returns instantly.

hs.inspect(hs.window._timed_allWindows()) takes a very long time.

Some of the culprits of long time:

["com.apple.QuickLookUIFramework.QLPreviewGenerationExtension"] = 6.0009548664093, 2023-09-05 14:47:00: took 6.01s for com.p5sys.jump.connect

Upon looking into a few of these, jump connect was found to be unresponsive. Quitting & relaunching removed the long time for that process. I also force-quit the QLPreviewGenerationExtension and that fixed the timing on that one. It was not unresponsive according to Activity Monitor. Not sure how to restart that process manually.

It seems like for each application that doesn't respond in time, hammerspoon times out at around 6 seconds?

wongjustin99 avatar Sep 05 '23 18:09 wongjustin99