-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApplyColorProfile.swift
98 lines (80 loc) · 3.25 KB
/
ApplyColorProfile.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Cocoa
import ColorSync
import CoreGraphics
import Foundation
let FACTORY_PROFILES = kColorSyncFactoryProfiles.takeUnretainedValue() as String
let DEVICE_PROFILE_URL = kColorSyncDeviceProfileURL.takeUnretainedValue() as String
let DEVICE_CLASS = kColorSyncDisplayDeviceClass.takeUnretainedValue()
let DEFAULT_PROFILE = kColorSyncDeviceDefaultProfileID.takeUnretainedValue()
extension UUID {
var cfUUID: CFUUID {
let b = uuid
return CFUUIDCreateWithBytes(nil, b.0, b.1, b.2, b.3, b.4, b.5, b.6, b.7, b.8, b.9, b.10, b.11, b.12, b.13, b.14, b.15)
}
}
func setDisplayProfile(_ path: String, _ display: MPDisplay) {
if path == "factory" {
print("Resetting profile for \(display.displayName ?? "display") to factory default")
let profileDict = [DEFAULT_PROFILE: nil] as CFDictionary
guard ColorSyncDeviceSetCustomProfiles(DEVICE_CLASS, display.uuid.cfUUID, profileDict) else {
print("Failed to set factory profile")
return
}
return
}
print("Setting profile \(path) for \(display.displayName ?? "display")")
let iccURL = URL(fileURLWithPath: path)
let uuid = display.uuid.cfUUID
var err: Unmanaged<CFError>?
guard let profile = ColorSyncProfileCreateWithURL(iccURL as CFURL, &err)?.takeRetainedValue() else {
print("Failed to create profile from \(path)")
return
}
let profileName = ColorSyncProfileCopyDescriptionString(profile)?.takeRetainedValue() as String? ?? iccURL.deletingPathExtension().lastPathComponent
print("Profile name: \(profileName)")
guard let deviceInfo = ColorSyncDeviceCopyDeviceInfo(DEVICE_CLASS, uuid)?.takeRetainedValue() else {
print("Failed to get device info for \(uuid)")
return
}
print("Device info: \(deviceInfo)")
print("Setting profile \(profileName) for \(uuid)")
let profileDict = [DEFAULT_PROFILE: iccURL] as CFDictionary
guard ColorSyncDeviceSetCustomProfiles(DEVICE_CLASS, uuid, profileDict) else {
print("Failed to set custom profile")
return
}
}
func main() {
guard let mgr = MPDisplayMgr(), let displays = mgr.displays else {
print("No displays")
return
}
guard CommandLine.arguments.count >= 3 else {
print("""
Usage: \(CommandLine.arguments[0]) <display> <profile>
display: Can be a display ID, UUID, or name. Use "all" to apply to all displays.
profile: Path to an ICC profile. Use "factory" to reset to default.
""")
return
}
let display = CommandLine.arguments[1]
let profilePath = CommandLine.arguments[2]
guard FileManager.default.fileExists(atPath: profilePath) || profilePath == "factory" else {
print("File not found: \(profilePath)")
return
}
// Example: `ApplyColorProfile all HighAmbientLight.icc`
if display.lowercased() == "all" {
for display in displays.filter(\.hasPresets) {
setDisplayProfile(profilePath, display)
}
return
}
// Example: `ApplyColorProfile DELL HighAmbientLight.icc`
guard let display = mgr.matchDisplay(filter: display) else {
print("No display found for query: \(display)")
return
}
setDisplayProfile(profilePath, display)
}
main()