From 2e4ae68a5aee708ef87ebd6c43b5ff738da844f0 Mon Sep 17 00:00:00 2001 From: Peter Hrvola Date: Mon, 16 Mar 2020 12:44:38 +0100 Subject: [PATCH] Add Music app --- MTMR.xcodeproj/project.pbxproj | 8 ++++++ MTMR/AppleScripts/Music.next.scpt | 7 +++++ MTMR/AppleScripts/Music.nowPlaying.scpt | 10 +++++++ MTMR/AppleScripts/PlaySmart.scpt | 4 +++ MTMR/Widgets/MusicBarItem.swift | 36 +++++++++++++++++++++++++ MTMR/defaultPreset.json | 20 ++++++++++++++ 6 files changed, 85 insertions(+) create mode 100644 MTMR/AppleScripts/Music.next.scpt create mode 100644 MTMR/AppleScripts/Music.nowPlaying.scpt diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index a720a6d..55233c8 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -22,6 +22,8 @@ 4CC9FEDC22FDEA65001512EB /* AMR_ANSIEscapeHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 4CC9FEDB22FDEA65001512EB /* AMR_ANSIEscapeHelper.m */; }; 4CDC6E5022FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDC6E4F22FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift */; }; 4CFF5E5C22E623DD00BFB1EE /* YandexWeatherBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CFF5E5B22E623DD00BFB1EE /* YandexWeatherBarItem.swift */; }; + 5DC6CA02241F92CB005CD5E8 /* Music.nowPlaying.scpt in Resources */ = {isa = PBXBuildFile; fileRef = 5DC6CA00241F92CB005CD5E8 /* Music.nowPlaying.scpt */; }; + 5DC6CA03241F92CB005CD5E8 /* Music.next.scpt in Resources */ = {isa = PBXBuildFile; fileRef = 5DC6CA01241F92CB005CD5E8 /* Music.next.scpt */; }; 60173D3E20C0031B002C305F /* LaunchAtLoginController.m in Sources */ = {isa = PBXBuildFile; fileRef = 60173D3D20C0031B002C305F /* LaunchAtLoginController.m */; }; 6027D1B92080E52A004FFDC7 /* BrightnessViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6027D1B72080E52A004FFDC7 /* BrightnessViewController.swift */; }; 6027D1BA2080E52A004FFDC7 /* VolumeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6027D1B82080E52A004FFDC7 /* VolumeViewController.swift */; }; @@ -100,6 +102,8 @@ 4CC9FEDB22FDEA65001512EB /* AMR_ANSIEscapeHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMR_ANSIEscapeHelper.m; sourceTree = ""; }; 4CDC6E4F22FCA93F0069ADD4 /* ShellScriptTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellScriptTouchBarItem.swift; sourceTree = ""; }; 4CFF5E5B22E623DD00BFB1EE /* YandexWeatherBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YandexWeatherBarItem.swift; sourceTree = ""; }; + 5DC6CA00241F92CB005CD5E8 /* Music.nowPlaying.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Music.nowPlaying.scpt; sourceTree = ""; }; + 5DC6CA01241F92CB005CD5E8 /* Music.next.scpt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = Music.next.scpt; sourceTree = ""; }; 60173D3C20C0031B002C305F /* LaunchAtLoginController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LaunchAtLoginController.h; sourceTree = ""; }; 60173D3D20C0031B002C305F /* LaunchAtLoginController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LaunchAtLoginController.m; sourceTree = ""; }; 6027D1B72080E52A004FFDC7 /* BrightnessViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BrightnessViewController.swift; sourceTree = ""; }; @@ -258,6 +262,8 @@ B0B17426207D6AFE0004B740 /* AppleScripts */ = { isa = PBXGroup; children = ( + 5DC6CA01241F92CB005CD5E8 /* Music.next.scpt */, + 5DC6CA00241F92CB005CD5E8 /* Music.nowPlaying.scpt */, B0B1742A207D6B580004B740 /* Battery.scpt */, B0B17429207D6B580004B740 /* Finder.scpt */, B0B1742C207D6B590004B740 /* iTunes.next.scpt */, @@ -407,6 +413,8 @@ B0B1743A207D6B590004B740 /* iTunes.nowPlaying.scpt in Resources */, B082B257205C7D8000BC04DC /* Assets.xcassets in Resources */, B0B17437207D6B590004B740 /* Spotify.nowPlaying.scpt in Resources */, + 5DC6CA02241F92CB005CD5E8 /* Music.nowPlaying.scpt in Resources */, + 5DC6CA03241F92CB005CD5E8 /* Music.next.scpt in Resources */, B0B17436207D6B590004B740 /* iTunes.next.scpt in Resources */, B082B25A205C7D8000BC04DC /* Main.storyboard in Resources */, B0B17435207D6B590004B740 /* Spotify.next.scpt in Resources */, diff --git a/MTMR/AppleScripts/Music.next.scpt b/MTMR/AppleScripts/Music.next.scpt new file mode 100644 index 0000000..9101064 --- /dev/null +++ b/MTMR/AppleScripts/Music.next.scpt @@ -0,0 +1,7 @@ +if application "Music" is running then + tell application "Music" + if player state is playing then + next track + end if + end tell +end if diff --git a/MTMR/AppleScripts/Music.nowPlaying.scpt b/MTMR/AppleScripts/Music.nowPlaying.scpt new file mode 100644 index 0000000..81644b4 --- /dev/null +++ b/MTMR/AppleScripts/Music.nowPlaying.scpt @@ -0,0 +1,10 @@ +if application "Music" is running then + tell application "Music" + if player state is playing then + return (get artist of current track) & " – " & (get name of current track) + else + return "" + end if + end tell +end if +return "" diff --git a/MTMR/AppleScripts/PlaySmart.scpt b/MTMR/AppleScripts/PlaySmart.scpt index eed3e02..2807287 100644 --- a/MTMR/AppleScripts/PlaySmart.scpt +++ b/MTMR/AppleScripts/PlaySmart.scpt @@ -2,6 +2,10 @@ if application "iTunes" is running then tell application "iTunes" to playpause end if +if application "Music" is running then + tell application "Music" to playpause +end if + if application "Spotify" is running then tell application "Spotify" to playpause end if diff --git a/MTMR/Widgets/MusicBarItem.swift b/MTMR/Widgets/MusicBarItem.swift index 5bc8e7b..23b2029 100644 --- a/MTMR/Widgets/MusicBarItem.swift +++ b/MTMR/Widgets/MusicBarItem.swift @@ -11,6 +11,7 @@ import ScriptingBridge class MusicBarItem: CustomButtonTouchBarItem { private enum Player: String { + case Music = "com.apple.Music" case iTunes = "com.apple.iTunes" case Spotify = "com.spotify.client" case VOX = "com.coppertino.Vox" @@ -19,6 +20,7 @@ class MusicBarItem: CustomButtonTouchBarItem { } private let playerBundleIdentifiers = [ + Player.Music, Player.iTunes, Player.Spotify, Player.VOX, @@ -71,6 +73,10 @@ class MusicBarItem: CustomButtonTouchBarItem { let mp = (musicPlayer as iTunesApplication) mp.playpause!() return + } else if ident == .Music { + let mp = (musicPlayer as MusicApplication) + mp.playpause!() + return } else if ident == .VOX { let mp = (musicPlayer as VoxApplication) mp.playpause!() @@ -134,6 +140,11 @@ class MusicBarItem: CustomButtonTouchBarItem { mp.nextTrack!() updatePlayer() return + } else if ident == .Music { + let mp = (musicPlayer as MusicApplication) + mp.nextTrack!() + updatePlayer() + return } else if ident == .VOX { let mp = (musicPlayer as VoxApplication) mp.next!() @@ -188,6 +199,8 @@ class MusicBarItem: CustomButtonTouchBarItem { tempTitle = (musicPlayer as SpotifyApplication).title } else if ident == .iTunes { tempTitle = (musicPlayer as iTunesApplication).title + } else if ident == .Music { + tempTitle = (musicPlayer as MusicApplication).title } else if ident == .VOX { tempTitle = (musicPlayer as VoxApplication).title } else if ident == .Safari { @@ -321,6 +334,29 @@ extension iTunesApplication { } } +@objc protocol MusicApplication { + @objc optional var currentTrack: MusicTrack { get } + @objc optional func playpause() + @objc optional func nextTrack() + @objc optional func previousTrack() +} + +extension SBApplication: MusicApplication {} + +@objc protocol MusicTrack { + @objc optional var artist: String { get } + @objc optional var name: String { get } +} + +extension SBObject: MusicTrack {} + +extension MusicApplication { + var title: String { + guard let t = currentTrack else { return "" } + return (t.artist ?? "") + " — " + (t.name ?? "") + } +} + @objc protocol VoxApplication { @objc optional func playpause() @objc optional func next() diff --git a/MTMR/defaultPreset.json b/MTMR/defaultPreset.json index 62b43d8..dfa019e 100644 --- a/MTMR/defaultPreset.json +++ b/MTMR/defaultPreset.json @@ -28,6 +28,26 @@ } }, + // Music + { + "type": "appleScriptTitledButton", + "source": { + "inline": "if application \"Music\" is running then\rtell application \"Music\"\rif player state is playing then\rreturn (get artist of current track) & \" – \" & (get name of current track)\relse\rreturn \"\"\rend if\rend tell\rend if\rreturn \"\"\r" + }, + "action": "appleScript", + "actionAppleScript": { + "inline": "if application \"Music\" is running then\rtell application \"Music\"\rif player state is playing then\rnext track\rend if\rend tell\rend if\r" + }, + "longAction": "appleScript", + "longActionAppleScript": { + "inline": "if application \"Music\" is running then\rtell application \"Music\"\rif player state is playing then\rprev track\rend if\rend tell\rend if\r" + }, + "refreshInterval": 2, + "image": { + "base64": "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAADAUExURUdwTOVVZCzB+3qc0gkDBgEBAgcKEwAAAA4EBP5aVU2V95iJv7V3rtBOvH5W6jaOyclScKZGX3wuQCMuUqZN7+NQYXtDvFd9/sxYni2z6UhBhyhvnIp7sf38/PXz9ePm69/k6fHv8/j29+3q7/v6+ufq7uvu8fTw8+1egOFki/dbboVj/HNy/T+j/dNtnEul81vC8Vmf3OeRqOBVofK4xZfF7sDb7PLe6LxU1KKK79PL6vrW3fh4g5Zi4bi16daa0A3Qc90AAAAddFJOUwD3/v0uOlYNG/z+/v7998OYYztt/Le2/eDqi5jEo2rNTwAABMtJREFUWMPdl1tjqjoQhauC0Hqvta26JVxUQBQFRPBS/f//as8kISBqd/twXs5KjBDyrZkEFfP09D+WoigqFxz+mlbVXncweEZVBoNuD1x+E1vtMVZoURn01J/mob52KRNuj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