|
| 1 | +import AppKit |
| 2 | +import Foundation |
| 3 | + |
| 4 | +// MARK: - AppleScript Commands |
| 5 | + |
| 6 | +/// Play command: start or resume playback. |
| 7 | +@objc(KasetPlayCommand) |
| 8 | +final class PlayCommand: NSScriptCommand { |
| 9 | + override func performDefaultImplementation() -> Any? { |
| 10 | + Task { @MainActor in |
| 11 | + guard let playerService = PlayerService.shared else { return } |
| 12 | + await playerService.resume() |
| 13 | + } |
| 14 | + return nil |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/// Pause command: pause playback. |
| 19 | +@objc(KasetPauseCommand) |
| 20 | +final class PauseCommand: NSScriptCommand { |
| 21 | + override func performDefaultImplementation() -> Any? { |
| 22 | + Task { @MainActor in |
| 23 | + guard let playerService = PlayerService.shared else { return } |
| 24 | + await playerService.pause() |
| 25 | + } |
| 26 | + return nil |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// PlayPause command: toggle play/pause state. |
| 31 | +@objc(KasetPlayPauseCommand) |
| 32 | +final class PlayPauseCommand: NSScriptCommand { |
| 33 | + override func performDefaultImplementation() -> Any? { |
| 34 | + Task { @MainActor in |
| 35 | + guard let playerService = PlayerService.shared else { return } |
| 36 | + await playerService.playPause() |
| 37 | + } |
| 38 | + return nil |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// NextTrack command: skip to the next track. |
| 43 | +@objc(KasetNextTrackCommand) |
| 44 | +final class NextTrackCommand: NSScriptCommand { |
| 45 | + override func performDefaultImplementation() -> Any? { |
| 46 | + Task { @MainActor in |
| 47 | + guard let playerService = PlayerService.shared else { return } |
| 48 | + await playerService.next() |
| 49 | + } |
| 50 | + return nil |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// PreviousTrack command: go to the previous track. |
| 55 | +@objc(KasetPreviousTrackCommand) |
| 56 | +final class PreviousTrackCommand: NSScriptCommand { |
| 57 | + override func performDefaultImplementation() -> Any? { |
| 58 | + Task { @MainActor in |
| 59 | + guard let playerService = PlayerService.shared else { return } |
| 60 | + await playerService.previous() |
| 61 | + } |
| 62 | + return nil |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// SetVolume command: set the playback volume (0-100). |
| 67 | +@objc(KasetSetVolumeCommand) |
| 68 | +final class SetVolumeCommand: NSScriptCommand { |
| 69 | + override func performDefaultImplementation() -> Any? { |
| 70 | + guard let volumeValue = self.directParameter as? Int else { |
| 71 | + scriptErrorNumber = errAECoercionFail |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + let normalizedVolume = Double(max(0, min(100, volumeValue))) / 100.0 |
| 76 | + |
| 77 | + Task { @MainActor in |
| 78 | + guard let playerService = PlayerService.shared else { return } |
| 79 | + await playerService.setVolume(normalizedVolume) |
| 80 | + } |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// ToggleShuffle command: toggle shuffle mode. |
| 86 | +@objc(KasetToggleShuffleCommand) |
| 87 | +final class ToggleShuffleCommand: NSScriptCommand { |
| 88 | + override func performDefaultImplementation() -> Any? { |
| 89 | + Task { @MainActor in |
| 90 | + guard let playerService = PlayerService.shared else { return } |
| 91 | + playerService.toggleShuffle() |
| 92 | + } |
| 93 | + return nil |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// CycleRepeat command: cycle through repeat modes (off, all, one). |
| 98 | +@objc(KasetCycleRepeatCommand) |
| 99 | +final class CycleRepeatCommand: NSScriptCommand { |
| 100 | + override func performDefaultImplementation() -> Any? { |
| 101 | + Task { @MainActor in |
| 102 | + guard let playerService = PlayerService.shared else { return } |
| 103 | + playerService.cycleRepeatMode() |
| 104 | + } |
| 105 | + return nil |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// ToggleMute command: toggle mute state. |
| 110 | +@objc(KasetToggleMuteCommand) |
| 111 | +final class ToggleMuteCommand: NSScriptCommand { |
| 112 | + override func performDefaultImplementation() -> Any? { |
| 113 | + Task { @MainActor in |
| 114 | + guard let playerService = PlayerService.shared else { return } |
| 115 | + await playerService.toggleMute() |
| 116 | + } |
| 117 | + return nil |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/// GetPlayerInfo command: returns current player state as JSON. |
| 122 | +@objc(KasetGetPlayerInfoCommand) |
| 123 | +final class GetPlayerInfoCommand: NSScriptCommand { |
| 124 | + override func performDefaultImplementation() -> Any? { |
| 125 | + // AppleScript runs on main thread, so we can assume MainActor isolation |
| 126 | + let result: String = MainActor.assumeIsolated { |
| 127 | + guard let playerService = PlayerService.shared else { |
| 128 | + return "{\"error\": \"Player not available\"}" |
| 129 | + } |
| 130 | + |
| 131 | + let track = playerService.currentTrack |
| 132 | + let repeatMode: String = switch playerService.repeatMode { |
| 133 | + case .off: "off" |
| 134 | + case .all: "all" |
| 135 | + case .one: "one" |
| 136 | + } |
| 137 | + |
| 138 | + let likeStatus: String = switch playerService.currentTrackLikeStatus { |
| 139 | + case .like: "liked" |
| 140 | + case .dislike: "disliked" |
| 141 | + case .indifferent: "none" |
| 142 | + } |
| 143 | + |
| 144 | + var info: [String: Any] = [ |
| 145 | + "isPlaying": playerService.isPlaying, |
| 146 | + "isPaused": playerService.state == .paused, |
| 147 | + "position": playerService.progress, |
| 148 | + "duration": playerService.duration, |
| 149 | + "volume": Int(playerService.volume * 100), |
| 150 | + "shuffling": playerService.shuffleEnabled, |
| 151 | + "repeating": repeatMode, |
| 152 | + "muted": playerService.isMuted, |
| 153 | + "likeStatus": likeStatus, |
| 154 | + ] |
| 155 | + |
| 156 | + if let track { |
| 157 | + info["currentTrack"] = [ |
| 158 | + "name": track.title, |
| 159 | + "artist": track.artistsDisplay, |
| 160 | + "album": track.album?.title ?? "", |
| 161 | + "duration": track.duration ?? 0, |
| 162 | + "videoId": track.videoId, |
| 163 | + "artworkURL": track.thumbnailURL?.absoluteString ?? "", |
| 164 | + ] |
| 165 | + } |
| 166 | + |
| 167 | + if let data = try? JSONSerialization.data(withJSONObject: info, options: [.sortedKeys]), |
| 168 | + let json = String(data: data, encoding: .utf8) |
| 169 | + { |
| 170 | + return json |
| 171 | + } |
| 172 | + |
| 173 | + return "{}" |
| 174 | + } |
| 175 | + return result |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/// LikeTrack command: like/unlike the current track. |
| 180 | +@objc(KasetLikeTrackCommand) |
| 181 | +final class LikeTrackCommand: NSScriptCommand { |
| 182 | + override func performDefaultImplementation() -> Any? { |
| 183 | + Task { @MainActor in |
| 184 | + guard let playerService = PlayerService.shared else { return } |
| 185 | + playerService.likeCurrentTrack() |
| 186 | + } |
| 187 | + return nil |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +/// DislikeTrack command: dislike/undislike the current track. |
| 192 | +@objc(KasetDislikeTrackCommand) |
| 193 | +final class DislikeTrackCommand: NSScriptCommand { |
| 194 | + override func performDefaultImplementation() -> Any? { |
| 195 | + Task { @MainActor in |
| 196 | + guard let playerService = PlayerService.shared else { return } |
| 197 | + playerService.dislikeCurrentTrack() |
| 198 | + } |
| 199 | + return nil |
| 200 | + } |
| 201 | +} |
0 commit comments