-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathIsNowPlaying.swift
executable file
·59 lines (48 loc) · 2.11 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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)] [-v (prints now playing info)]")
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)
}
guard CommandLine.arguments.count >= 2, !CommandLine.arguments.contains("-q"), CommandLine.arguments.contains("-v") else {
RunLoop.main.run(until: Date() + 0.1)
exit(0)
}
let MRMediaRemoteGetNowPlayingInfoPointer = CFBundleGetFunctionPointerForName(
bundle,
"MRMediaRemoteGetNowPlayingInfo" as CFString
)!
typealias MRMediaRemoteGetNowPlayingInfoFunction = @convention(c) (DispatchQueue, @escaping ([String: Any]?) -> Void) -> Void
let MRMediaRemoteGetNowPlayingInfo = unsafeBitCast(
MRMediaRemoteGetNowPlayingInfoPointer,
to: MRMediaRemoteGetNowPlayingInfoFunction.self
)
MRMediaRemoteGetNowPlayingInfo(DispatchQueue.main) { info in
guard var info else {
print("No info")
exit(1)
}
// set kMRMediaRemoteNowPlayingInfoArtworkData to "exists" to avoid crash
if info["kMRMediaRemoteNowPlayingInfoArtworkData"] != nil {
info["kMRMediaRemoteNowPlayingInfoArtworkData"] = "exists"
}
print(info)
}
RunLoop.main.run(until: Date() + 0.1)