Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.07k stars 583 forks source link

feature request: hs.application:isRunning() #604

Closed lowne closed 9 years ago

lowne commented 9 years ago

... for possibly outdated references. For now I'm checking :bundleID() as it apparently returns nil for dead processes, but idk if that's guaranteed to work (and it's inelegant)

cmsj commented 9 years ago

this could just be a simple wrapper around a search of hs.application.runningApplications()

lowne commented 9 years ago

I had assumed there would be a more direct way to do this with the c object (check if the PID still exists) - if that's not the case then sure, I'll do it Lua side.

cmsj commented 9 years ago

@lowne I think that's going to be safest. AFAICS there isn't a decent API for checking if a specific app is running, so we'd be doing the same thing in C.

zzamboni commented 9 years ago

Couldn't this be a simple wrapper around hs.application.get()? I've been using this in my config and seems to work fine (see for example https://github.com/zzamboni/hammerspoon/blob/f291e402205b48f72f06fc96f2a151de84ad334b/extensions/spotify/init.lua#L179)

(apologies for the completely misguided comment - I also didn't get you were asking for an instance method and not a class method - my reading of Lua syntax nuances is still weak).

lowne commented 9 years ago

@cmsj from the docs:

runningApplicationWithProcessIdentifier: Returns the running application with the given process identifier, or nil if no application has that pid.

so it seems it'd just be a check for nsobject_for_app(...)==nil (see :bundleID() in internal.m)

(EDIT: I'm aware of the ridiculousness of not just doing it myself, but my Xcode allergy is getting worse)

cmsj commented 9 years ago

oh, hrm, for some reason I thought you wanted to go from an application name to its running status, but you very clearly asked for an hs.application object method. My bad :)

In that case, yes, that function should work fine

lowne commented 9 years ago

Yes, in hindsight OP wasn't worded very clearly, sorry. In case you missed the edit above: I'm aware of the ridiculousness of not just doing it myself, but my Xcode allergy is getting worse. :)