Created
November 4, 2022 18:36
-
-
Save marcusmoore/505365ac73d1c3fc00ee754b72a6d9b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- github.com/rsefer/hammerspoon-config | |
--- === SDC Music === | |
local obj = {} | |
obj.__index = obj | |
obj.name = "SpotifyListener" | |
local trackNotification | |
function trackString(artist, track) | |
track = track:gsub(artist, '') | |
track = track:gsub(' | ', '') | |
return artist .. ' - ' .. '"' .. track .. '"' | |
end | |
function obj:getCurrentPlayerState() | |
playbackState = obj.player.module:getPlaybackState() | |
workingStateString = 'paused' | |
if playbackState == obj.player.module.state_paused then | |
workingStateString = 'paused' | |
elseif playbackState == obj.player.module.state_playing then | |
workingStateString = 'playing' | |
elseif playbackState == obj.player.module.state_stopped then | |
workingStateString = 'stopped' | |
end | |
return workingStateString | |
end | |
-- function obj:updateCurrentTrackInfo() | |
-- if obj.player.module:getCurrentTrack() then | |
-- obj.currentTrack = { | |
-- -- artist = obj.player.module:getCurrentArtist() or '', | |
-- -- album = obj.player.module:getCurrentAlbum() or '', | |
-- -- name = obj.player.module:getCurrentTrack() or '', | |
-- -- duration = obj.player.module:getDuration() or 300, | |
-- -- position = obj.player.module:getPosition() or 0, | |
-- -- albumArt = obj.currentTrack.albumArt or '' | |
-- } | |
-- else | |
-- obj.currentTrack = {} | |
-- end | |
-- end | |
function obj:notifyTrack() | |
workingArtist = obj.currentTrack.artist or '' | |
if not workingArtist or string.len(workingArtist) < 1 then | |
workingArtist = obj.currentTrack.album or '' | |
end | |
if not obj.currentTrack then return end | |
if trackNotification then | |
trackNotification:withdraw() | |
end | |
trackNotification = hs.notify.new(function() | |
hs.application.launchOrFocus(obj.player.name) | |
end, { | |
-- hasActionButton = true, | |
-- actionButtonTitle = 'Open', | |
-- title = 'Track: ' .. obj.currentTrack.name, | |
-- subTitle = 'Artist: ' .. workingArtist, | |
-- informativeText = 'Album: ' .. obj.currentTrack.album, | |
informativeText = 'hi', | |
-- setIdImage = obj.currentTrack.albumArt, | |
-- contentImage = obj.currentTrack.albumArt, | |
withdrawAfter = 2.5 | |
}):send() | |
end | |
-- function obj:toggleNowPlaying() | |
-- hs.osascript.applescript('tell application "System Events" to tell process "ControlCenter" to tell menu bar 1 to click (menu bar item "Now Playing")') | |
-- end | |
function obj:unloadPlayerMenus() | |
obj.menus.controlMenu:setIcon(nil) | |
obj.menus.titleMenu:setIcon(nil) | |
end | |
function obj:playerCheck() | |
if obj.timer ~= nil then | |
obj.timer:stop() | |
end | |
obj.timer = hs.timer.doUntil(function() | |
return obj.player.isDormant | |
end, function() | |
if obj:getCurrentPlayerState() == 'playing' then | |
obj.player.lastTimePlayed = os.time() | |
end | |
obj.player.isDormant = (os.time() - obj.player.lastTimePlayed) > 5 * 60 | |
obj:setPlayerMenus() | |
end, 4) | |
end | |
function obj:init() | |
-- if hs.settings.get('musicPlayerName') == 'Spotify' then | |
self.player = { | |
name = 'Spotify', | |
app = hs.application.get('Spotify'), | |
module = hs.spotify, | |
icon = hs.image.imageFromAppBundle('com.spotify.client'):setSize({ w = hs.settings.get('menuIconSize'), h = hs.settings.get('menuIconSize') }), | |
distributedPlaybackChangedString = 'com.spotify.client.PlaybackStateChanged', | |
color = '1db954' | |
} | |
-- else | |
-- self.player = { | |
-- name = 'Music', | |
-- app = hs.application.get('Music'), | |
-- module = hs.itunes, | |
-- icon = hs.image.imageFromAppBundle('com.apple.Music'):setSize({ w = hs.settings.get('menuIconSize'), h = hs.settings.get('menuIconSize') }), | |
-- distributedPlaybackChangedString = 'com.apple.Music.playerInfo', | |
-- color = 'f46060' | |
-- } | |
-- end | |
self.player.lastState = nil | |
self.player.isDormant = false | |
self.player.lastTimePlayed = os.time() | |
self.menus = { | |
-- titleMenu = hs.menubar.new():setClickCallback(obj.toggleNowPlaying), | |
-- controlMenu = hs.menubar.new():setClickCallback(obj.player.module.playpause), | |
-- playerMenu = hs.menubar.new():setClickCallback(obj.togglePlayer):setIcon(self.player.icon, false) | |
} | |
self.currentTrack = {} | |
self.timer = nil | |
-- self.episodesUpdateTimer = hs.timer.doEvery(15 * 60, function() | |
-- if not hs.settings.get('spotify_podcasts_episode_date') or (os.date('*t').hour > 6 and (60 * 60 * 1) < os.time() - hs.settings.get('spotify_podcasts_episode_date')) then | |
-- obj:setEpisodeChooserToolbar() | |
-- obj:getSpotifyPodcastEpisodes() | |
-- end | |
-- end):stop() | |
-- self.episodesList = self:transformEpisodesList() | |
-- self.episodeChooserToolbar = nil | |
-- self.episodeChooser = hs.chooser.new(function(choice) | |
-- if not choice then return end | |
-- hs.osascript.applescript('tell application "Spotify" to play track "' .. choice.uri .. '"') | |
-- end):width(40) | |
-- :placeholderText('Episodes') | |
-- :choices(self.episodesList) | |
-- self.episodesListWatchKey = hs.settings.watchKey('settings_spotify_podcast_episodes_watcher', 'spotify_podcast_episodes', function() | |
-- obj:setEpisodeChooserToolbar() | |
-- obj:transformEpisodesList() | |
-- obj.episodeChooser:choices(obj.episodesList) | |
-- end) | |
self.watcher = hs.application.watcher.new(function(name, event, app) | |
if name == self.player.name then | |
if event == 2 or event == hs.application.watcher.terminated then | |
obj:unloadPlayerMenus() | |
obj.menus.playerMenu:setIcon(self.icon, false) | |
end | |
end | |
end) | |
self.distributednotifications = hs.distributednotifications.new(function(name, object, userInfo) | |
-- if userInfo['Player State'] == 'Playing' then | |
-- obj.player.isDormant = false | |
-- -- obj:getTrackAlbumArt() | |
-- if obj.player.lastState ~= 'Paused' then | |
-- -- obj:updateCurrentTrackInfo() | |
-- -- obj:toggleNowPlaying() | |
-- -- hs.timer.doAfter(2, self.toggleNowPlaying) | |
-- obj:notifyTrack() | |
-- end | |
-- obj.player.lastTimePlayed = os.time() | |
-- obj:playerCheck() | |
-- end | |
obj:notifyTrack() | |
obj.player.lastState = userInfo['Player State'] | |
-- obj:setPlayerMenus() | |
end, self.player.distributedPlaybackChangedString) | |
end | |
function obj:start() | |
self.watcher:start() | |
self.distributednotifications:start() | |
-- self.episodesUpdateTimer:start() | |
-- self:setEpisodeChooserToolbar() | |
if obj:getCurrentPlayerState() == 'playing' then | |
-- obj:getTrackAlbumArt() | |
self:playerCheck() | |
end | |
end | |
function obj:stop() | |
self.watcher:stop() | |
self.episodesUpdateTimer:stop() | |
self.distributednotifications:stop() | |
if self.timer ~= nil then | |
self.timer:stop() | |
end | |
end | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment