-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIsNowPlaying.swift
executable file
·30 lines (24 loc) · 1.14 KB
/
IsNowPlaying.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env swift
import Foundation
if CommandLine.arguments.count >= 2, ["-h", "--help"].contains(CommandLine.arguments[1]) {
print("Usage: \(CommandLine.arguments[0]) [-q (exits with non-zero status code if not playing)]")
exit(0)
}
let bundle = CFBundleCreate(kCFAllocatorDefault, NSURL(fileURLWithPath: "/System/Library/PrivateFrameworks/MediaRemote.framework"))!
let MRMediaRemoteGetNowPlayingApplicationIsPlayingPointer = CFBundleGetFunctionPointerForName(
bundle,
"MRMediaRemoteGetNowPlayingApplicationIsPlaying" as CFString
)!
typealias MRMediaRemoteGetNowPlayingApplicationIsPlayingFunction = @convention(c) (DispatchQueue, @escaping (Bool) -> Void) -> Void
let MRMediaRemoteGetNowPlayingApplicationIsPlaying = unsafeBitCast(
MRMediaRemoteGetNowPlayingApplicationIsPlayingPointer,
to: MRMediaRemoteGetNowPlayingApplicationIsPlayingFunction.self
)
MRMediaRemoteGetNowPlayingApplicationIsPlaying(DispatchQueue.main) { playing in
guard CommandLine.arguments.count >= 2, CommandLine.arguments[1] == "-q" else {
print(playing)
return
}
exit(playing ? 0 : 1)
}
RunLoop.main.run(until: Date() + 0.1)