|
| 1 | +import Foundation |
| 2 | + |
| 3 | +let name = (CommandLine.arguments[0] as NSString).lastPathComponent |
| 4 | +let version = "1.0.0" |
| 5 | +let build = "2024-08-19-001" |
| 6 | + |
| 7 | +let versionMessage = """ |
| 8 | +\(name) \(version) (\(build)) |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +let copyrightMessage = """ |
| 13 | +Copyright 2024 AJ ONeal <aj@therootcompany.com> |
| 14 | +
|
| 15 | +""" |
| 16 | + |
| 17 | +let helpMessage = """ |
| 18 | +Runs a user-specified command whenever the screen is unlocked by |
| 19 | +listening for the "com.apple.screenIsUnlocked" event, using /usr/bin/command -v |
| 20 | +to find the program in the user's PATH (or the explicit path given), and then |
| 21 | +runs it with /usr/bin/command, which can run aliases and shell functions also. |
| 22 | +
|
| 23 | +USAGE |
| 24 | + \(name) [OPTIONS] <command> [--] [command-arguments] |
| 25 | +
|
| 26 | +OPTIONS |
| 27 | + --version, -V, version |
| 28 | + Display the version information and exit. |
| 29 | + --help, help |
| 30 | + Display this help and exit. |
| 31 | +
|
| 32 | +DESCRIPTION |
| 33 | + \(name) is a simple command-line tool that demonstrates how to handle |
| 34 | + version and help flags in a Swift program following POSIX conventions. |
| 35 | +
|
| 36 | +""" |
| 37 | + |
| 38 | +signal(SIGINT) { _ in |
| 39 | + printForHuman("received ctrl+c, exiting...\n") |
| 40 | + exit(0) |
| 41 | +} |
| 42 | + |
| 43 | +enum ScriptError: Error { |
| 44 | + case fileNotFound |
| 45 | +} |
| 46 | + |
| 47 | +func printForHuman(_ message: String) { |
| 48 | + if let data = message.data(using: .utf8) { |
| 49 | + FileHandle.standardError.write(data) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func getCommandPath(_ command: String) -> String? { |
| 54 | + let commandv = Process() |
| 55 | + commandv.launchPath = "/usr/bin/command" |
| 56 | + commandv.arguments = ["-v", command] |
| 57 | + |
| 58 | + let pipe = Pipe() |
| 59 | + commandv.standardOutput = pipe |
| 60 | + commandv.standardError = FileHandle.standardError |
| 61 | + |
| 62 | + try! commandv.run() |
| 63 | + commandv.waitUntilExit() |
| 64 | + |
| 65 | + let data = pipe.fileHandleForReading.readDataToEndOfFile() |
| 66 | + guard let scriptPath = String(data: data, encoding: .utf8)? |
| 67 | + .trimmingCharacters(in: .whitespacesAndNewlines) |
| 68 | + else { |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + if commandv.terminationStatus != 0, scriptPath.isEmpty { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + return scriptPath |
| 77 | +} |
| 78 | + |
| 79 | +class ScreenLockObserver { |
| 80 | + var commandPath: String |
| 81 | + var commandArgs: ArraySlice<String> |
| 82 | + |
| 83 | + init(_ commandArgs: ArraySlice<String>) { |
| 84 | + self.commandPath = commandArgs.first! |
| 85 | + self.commandArgs = commandArgs |
| 86 | + |
| 87 | + let dnc = DistributedNotificationCenter.default() |
| 88 | + |
| 89 | + _ = dnc.addObserver(forName: NSNotification.Name("com.apple.screenIsLocked"), object: nil, queue: .main) { _ in |
| 90 | + NSLog("notification: com.apple.screenIsLocked") |
| 91 | + } |
| 92 | + |
| 93 | + NSLog("Waiting for 'com.apple.screenIsUnlocked' to run \(self.commandArgs)") |
| 94 | + _ = dnc.addObserver(forName: NSNotification.Name("com.apple.screenIsUnlocked"), object: nil, queue: .main) { _ in |
| 95 | + NSLog("notification: com.apple.screenIsUnlocked") |
| 96 | + self.runOnUnlock() |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private func runOnUnlock() { |
| 101 | + let task = Process() |
| 102 | + task.launchPath = "/usr/bin/command" |
| 103 | + task.arguments = Array(commandArgs) |
| 104 | + task.standardOutput = FileHandle.standardOutput |
| 105 | + task.standardError = FileHandle.standardError |
| 106 | + |
| 107 | + do { |
| 108 | + try task.run() |
| 109 | + } catch { |
| 110 | + printForHuman("Failed to run \(self.commandPath): \(error.localizedDescription)\n") |
| 111 | + if let nsError = error as NSError? { |
| 112 | + printForHuman("Error details: \(nsError)\n") |
| 113 | + } |
| 114 | + exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + task.waitUntilExit() |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +@discardableResult |
| 122 | +func removeItem(_ array: inout ArraySlice<String>, _ item: String) -> Bool { |
| 123 | + if let index = array.firstIndex(of: item) { |
| 124 | + array.remove(at: index) |
| 125 | + return true |
| 126 | + } |
| 127 | + return false |
| 128 | +} |
| 129 | + |
| 130 | +func processArgs(_ args: inout ArraySlice<String>) -> ArraySlice<String> { |
| 131 | + var childArgs: ArraySlice<String> = [] |
| 132 | + if let delimiterIndex = args.firstIndex(of: "--") { |
| 133 | + let childArgsIndex = delimiterIndex + 1 |
| 134 | + childArgs = args[childArgsIndex...] |
| 135 | + args.removeSubrange(delimiterIndex...) |
| 136 | + } |
| 137 | + if removeItem(&args, "--help") || removeItem(&args, "help") { |
| 138 | + printForHuman(versionMessage) |
| 139 | + printForHuman("\n") |
| 140 | + printForHuman(helpMessage) |
| 141 | + printForHuman("\n") |
| 142 | + printForHuman(copyrightMessage) |
| 143 | + exit(0) |
| 144 | + } |
| 145 | + if removeItem(&args, "--version") || removeItem(&args, "-V") || removeItem(&args, "version") { |
| 146 | + printForHuman(versionMessage) |
| 147 | + printForHuman(copyrightMessage) |
| 148 | + exit(0) |
| 149 | + } |
| 150 | + |
| 151 | + childArgs = args + childArgs |
| 152 | + guard childArgs.count > 0 else { |
| 153 | + printForHuman(versionMessage) |
| 154 | + printForHuman("\n") |
| 155 | + printForHuman(helpMessage) |
| 156 | + printForHuman("\n") |
| 157 | + printForHuman(copyrightMessage) |
| 158 | + exit(1) |
| 159 | + } |
| 160 | + |
| 161 | + let commandName = childArgs.first! |
| 162 | + guard let commandPath = getCommandPath(commandName) else { |
| 163 | + printForHuman("ERROR:\n \(commandName) not found in PATH\n") |
| 164 | + exit(1) |
| 165 | + } |
| 166 | + |
| 167 | + childArgs[childArgs.startIndex] = commandPath |
| 168 | + return childArgs |
| 169 | +} |
| 170 | + |
| 171 | +var args = CommandLine.arguments[1...] |
| 172 | +let commandArgs = processArgs(&args) |
| 173 | +_ = ScreenLockObserver(commandArgs) |
| 174 | + |
| 175 | +RunLoop.main.run() |
0 commit comments