From 2ff0508f132c5c4ead50d9e1c2c39b033c07147c Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 6 May 2020 20:40:19 +0100 Subject: [PATCH] Update PushToTalk spoon to check list of apps when starting These changes have been submitted as a PR against the main Spoons repository here: https://github.com/Hammerspoon/Spoons/pull/169 --- hammerspoon/Spoons/PushToTalk.spoon/docs.json | 9 ++++++++ hammerspoon/Spoons/PushToTalk.spoon/init.lua | 22 +++++++++++++++++++ hammerspoon/init.lua | 1 + 3 files changed, 32 insertions(+) diff --git a/hammerspoon/Spoons/PushToTalk.spoon/docs.json b/hammerspoon/Spoons/PushToTalk.spoon/docs.json index 9501442..eddf37a 100644 --- a/hammerspoon/Spoons/PushToTalk.spoon/docs.json +++ b/hammerspoon/Spoons/PushToTalk.spoon/docs.json @@ -47,6 +47,15 @@ "signature": "PushToTalk.app_switcher", "stripped_doc": "For example this `{ ['zoom.us'] = 'push-to-talk' }` will switch mic to `push-to-talk` state when Zoom app starts.", "type": "Variable" + }, + { + "def": "PushToTalk.detect_on_start", + "desc": "Check running applications when starting PushToTalk.", + "doc": "Check running applications when starting PushToTalk.\nDefaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters.", + "name": "detect_on_start", + "signature": "PushToTalk.detect_on_start", + "stripped_doc": "Defaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters.", + "type": "Variable" } ], "desc": "Implements push-to-talk and push-to-mute functionality with `fn` key.", diff --git a/hammerspoon/Spoons/PushToTalk.spoon/init.lua b/hammerspoon/Spoons/PushToTalk.spoon/init.lua index 240c175..4caead7 100644 --- a/hammerspoon/Spoons/PushToTalk.spoon/init.lua +++ b/hammerspoon/Spoons/PushToTalk.spoon/init.lua @@ -31,12 +31,19 @@ obj.defaultState = 'unmute' obj.state = obj.defaultState obj.pushed = false + --- PushToTalk.app_switcher --- Variable --- Takes mapping from application name to mic state. --- For example this `{ ['zoom.us'] = 'push-to-talk' }` will switch mic to `push-to-talk` state when Zoom app starts. obj.app_switcher = {} +--- PushToTalk.detect_on_start +--- Variable +--- Check running applications when starting PushToTalk. +--- Defaults to false for backwards compatibility. With this disabled, PushToTalk will only change state when applications are launched or quit while PushToTalk is already active. Enable this to look through list of running applications when PushToTalk is started. If multiple apps defined in app_switcher are running, it will set state to the first one it encounters. +obj.detect_on_start = false + local function showState() local device = hs.audiodevice.defaultInputDevice() local muted = false @@ -97,6 +104,20 @@ local function eventTapWatcher(event) showState() end +local function initialState() + local apps = hs.application.runningApplications() + + for i, app in pairs(apps) do + for name, state in pairs(obj.app_switcher) do + if app:name() == name then + return state + end + end + end + + return obj.defaultState +end + --- PushToTalk:init() --- Method --- Initial setup. It's empty currently @@ -116,6 +137,7 @@ function obj:start() obj.menubar = hs.menubar.new() obj.menubar:setMenu(obj.menutable) + if obj.detect_on_start then obj.state = initialState() end obj.setState(obj.state) end diff --git a/hammerspoon/init.lua b/hammerspoon/init.lua index 00c680b..2114fd4 100644 --- a/hammerspoon/init.lua +++ b/hammerspoon/init.lua @@ -30,6 +30,7 @@ spoon.HeadphoneAutoPause:start() -- Enable push-to-talk microphone functionality when specific apps are running. hs.loadSpoon('PushToTalk') +spoon.PushToTalk.detect_on_start = true spoon.PushToTalk.app_switcher = { ['TeamSpeak 3'] = 'push-to-talk' } spoon.PushToTalk:start()