luahammerspoon

Hammerspoon hs.application:kill() not callable


Trying to get Hammerspoon to quit (kill) the Music app in OS X whenever it opens. (This application has been installed by Apple in such a way as to make it very difficult to alter and it launches whenever a bluetooth device is connected. Annoying bloatware, basically.) So, I cribbed this from the Hammerspoon "Getting started" page https://www.hammerspoon.org/go/...

function applicationWatcher(appName, eventType, appObject)
  if (eventType == hs.application.watcher.launched) then
    if (appName == "Music") then
      hs.application:kill()
    end
  end
end
appWatcher = hs.application.watcher.new(applicationWatcher)
appWatcher:start()

This correctly responds to the Music app being launched, but it errors out like so... ERROR: LuaSkin: hs.application.watcher callback: /Users/seancamden/.hammerspoon/init.lua:142: method 'kill' is not callable (a nil value)

How I can make this method callable? Or, what is the right way to do this?

https://www.hammerspoon.org/docs/hs.application.watcher.html https://www.hammerspoon.org/docs/hs.application.html#kill


Solution

  • your code is pretty much right, there is only one mistake. You used the global module hs.application and tried to call an object method :kill() from it. You would have to instantiate a new object first to be able to call it's kill method. For example: hs.application.get(appName):kill().

    However, the watcher already provides you with the application object that called the function as appObject. So appObject:kill() is what you are looking for.

    function applicationWatcher(appName, eventType, appObject)
      if (eventType == hs.application.watcher.launched) then
        if (appName == "Music") then
          appObject:kill()
        end
      end
    end
    
    appWatcher = hs.application.watcher.new(applicationWatcher)
    appWatcher:start()
    

    Tested here on MacOS BigSur