From 4159cc4e8ab54bf48a96703e7d68b0347ac97411 Mon Sep 17 00:00:00 2001 From: Brecht Machiels Date: Thu, 15 Jun 2023 11:12:13 +0200 Subject: [PATCH 1/3] MusicAppMediaFix: support multiple media apps The MusicAppMediaFix.mediaApps table lists the names of applications that should receive media key events. This spoon sends the events to the applications that was last active. --- Source/MusicAppMediaFix.spoon/init.lua | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Source/MusicAppMediaFix.spoon/init.lua b/Source/MusicAppMediaFix.spoon/init.lua index e0348885..1531654f 100644 --- a/Source/MusicAppMediaFix.spoon/init.lua +++ b/Source/MusicAppMediaFix.spoon/init.lua @@ -1,6 +1,7 @@ --- === MusicAppMediaFix === --- ---- Override macOS behaviour and send all media keys (play/prev/next) to Music.app +--- Override macOS behaviour and send all media keys (play/prev/next) to +--- the last active of a specified list of apps in MusicAppMediaFix.mediaApps local obj = { __gc = true } --obj.__index = obj setmetatable(obj, obj) @@ -16,6 +17,9 @@ obj.homepage = "https://github.com/Hammerspoon/Spoons" obj.license = "MIT - https://opensource.org/licenses/MIT" obj.eventtap = nil +obj.appWatcher = nil +obj.mediaApps = {"Music"} +obj.currentApp = nil -- Internal function used to find our location, so we know where to load files from local function script_path() @@ -25,7 +29,9 @@ end obj.spoonPath = script_path() function obj:init() + self.currentApp = self.mediaApps[1] self.eventtap = hs.eventtap.new({hs.eventtap.event.types.systemDefined}, self.mediaKeyCallback) + self.appWatcher = hs.application.watcher.new(self.appWatcherCallback) end function obj.mediaKeyCallback(event) @@ -39,11 +45,11 @@ function obj.mediaKeyCallback(event) -- handle action if data["down"] == false or data["repeat"] == true then if data["key"] == "PLAY" then - hs.applescript('tell application "Music" to playpause') + hs.applescript('tell application "' .. obj.currentApp .. '" to playpause') elseif data["key"] == "FAST" then - hs.applescript('tell application "Music" to next track') + hs.applescript('tell application "' .. obj.currentApp .. '" to next track') elseif data["key"] == "REWIND" then - hs.applescript('tell application "Music" to previous track') + hs.applescript('tell application "' .. obj.currentApp .. '" to previous track') end end @@ -51,6 +57,14 @@ function obj.mediaKeyCallback(event) return true, nil end +function obj.appWatcherCallback(appName, eventType, app) + if hs.fnutils.contains(obj.mediaApps, appName) + and (eventType == hs.application.watcher.activated) then + obj.currentApp = appName + print("Sending media key events to " .. appName) + end +end + --- MusicAppMediaFix:start() --- Method --- Starts the hs.eventtap that powers this Spoon @@ -64,6 +78,7 @@ function obj:start() if self.eventtap:isEnabled() ~= true then self.eventtap:start() end + self.appWatcher:start() return self end @@ -80,6 +95,7 @@ function obj:stop() if self.eventtap:isEnabled() then self.eventtap:stop() end + self.appWatcher:stop() return self end From c70e04c7d2c25d4abe4eea95864b07ab443bec95 Mon Sep 17 00:00:00 2001 From: Brecht Machiels Date: Thu, 15 Jun 2023 11:48:15 +0200 Subject: [PATCH 2/3] MusicAppMediaFix: pause all apps other than the current --- Source/MusicAppMediaFix.spoon/init.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/MusicAppMediaFix.spoon/init.lua b/Source/MusicAppMediaFix.spoon/init.lua index 1531654f..5ef8a9c9 100644 --- a/Source/MusicAppMediaFix.spoon/init.lua +++ b/Source/MusicAppMediaFix.spoon/init.lua @@ -44,6 +44,11 @@ function obj.mediaKeyCallback(event) -- handle action if data["down"] == false or data["repeat"] == true then + for i, app in pairs(obj.mediaApps) do + if app ~= obj.currentApp then + hs.osascript.applescript('tell application "' .. app .. '" to pause') + end + end if data["key"] == "PLAY" then hs.applescript('tell application "' .. obj.currentApp .. '" to playpause') elseif data["key"] == "FAST" then From c633d4ce29086125a30a899fe5e03b4ad341a43e Mon Sep 17 00:00:00 2001 From: Brecht Machiels Date: Sat, 3 May 2025 16:33:04 +0200 Subject: [PATCH 3/3] MusicAppMediaFix: add docstring for obj.mediaApps variable --- Source/MusicAppMediaFix.spoon/init.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/MusicAppMediaFix.spoon/init.lua b/Source/MusicAppMediaFix.spoon/init.lua index 5ef8a9c9..de07f6bf 100644 --- a/Source/MusicAppMediaFix.spoon/init.lua +++ b/Source/MusicAppMediaFix.spoon/init.lua @@ -1,7 +1,7 @@ --- === MusicAppMediaFix === --- --- Override macOS behaviour and send all media keys (play/prev/next) to ---- the last active of a specified list of apps in MusicAppMediaFix.mediaApps +--- the last active of a specified list of apps local obj = { __gc = true } --obj.__index = obj setmetatable(obj, obj) @@ -16,9 +16,13 @@ obj.author = "Matheus Salmi , Chris Jones obj.homepage = "https://github.com/Hammerspoon/Spoons" obj.license = "MIT - https://opensource.org/licenses/MIT" +--- MusicAppMediaFix.mediaApps +--- Variable +--- List of applications to control with the media keys +obj.mediaApps = { "Music" } + obj.eventtap = nil obj.appWatcher = nil -obj.mediaApps = {"Music"} obj.currentApp = nil -- Internal function used to find our location, so we know where to load files from