Skip to content

Instantly share code, notes, and snippets.

@oguzhanogreden
Last active March 10, 2020 20:32
Show Gist options
  • Save oguzhanogreden/0f82d51ee94862e5764fb8b4d7e0226e to your computer and use it in GitHub Desktop.
Save oguzhanogreden/0f82d51ee94862e5764fb8b4d7e0226e to your computer and use it in GitHub Desktop.
Wrapper for running Arbtt on macOS Catalina 10.15
////
//// ArbttWrapper
///
//// Starting with macOS Catalina, `arbtt-capture` was not able to read the window titles:
//// https://github.com/nomeata/arbtt/issues/30
///
//// I'm not a Swift programmer and I didn't want to spend too many hours on this, therefore there
//// are a few obvious improvements. The most crucial ones, the ones that are likely to hurt any
//// user who uses this script as is, are:
//// - System signals (e.g. SIGINT) are not handled. You may stop the script, the arbtt process
//// will keep running unless you kill it manually.
//// - User experience isn't polished, as you'll notice below.
///
//// Usage:
//// 1. Run this script once with ARBTT_COMMAND set to `arbtt-capture -d`.
//// 2. Make sure the prompt for required permissions appear and grant these permissions.
//// 3. Change ARBTT_COMMAND to `arbtt-capture <options>`.
//// 4. Rerun the script.
////
//// Created by Ozan Ogreden on 10/03/2020.
import Cocoa
import Foundation
import AVFoundation
import CoreGraphics
let ARBTT_COMMAND = "~/.cabal/bin/arbtt-capture -r 10"
func getPermission() -> Bool {
// Thanks to `brianmario` of StackOverflow.
// https://stackoverflow.com/q/49413972
let mainDisplay = CGMainDisplayID()
let recordingQueue = DispatchQueue.global(qos: .background)
let displayStreamProps : [CFString : Any] = [
CGDisplayStream.minimumFrameTime: 60,
]
let displayStream = CGDisplayStream(
dispatchQueueDisplay: mainDisplay,
outputWidth: 1,
outputHeight: 1,
pixelFormat: Int32(kCVPixelFormatType_32BGRA),
properties: displayStreamProps as CFDictionary,
queue: recordingQueue,
handler: { status, displayTime, frameSurface, updateRef in
print("is only called once")
}
)
var hasPermission : Bool
if displayStream != nil {
hasPermission = true
displayStream?.stop()
} else {
hasPermission = false
displayStream?.stop()
}
return hasPermission
}
func shell(command: String) -> String {
// Thanks to anonymous user of StackOverflow.
// https://stackoverflow.com/a/50035059
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
func runTracker() {
print(shell(command: ARBTT_COMMAND))
}
let hasPermission = getPermission()
if hasPermission {
runTracker()
} else {
print("Screen Recording permissions not granted.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment