Skip to content

Instantly share code, notes, and snippets.

@KrisYu
Created August 13, 2017 03:00
Show Gist options
  • Save KrisYu/dfafec87e6d847d28378039d7ee8fce5 to your computer and use it in GitHub Desktop.
Save KrisYu/dfafec87e6d847d28378039d7ee8fce5 to your computer and use it in GitHub Desktop.
capture screen using terminal commands in swift
//: Playground - noun: a place where people can play
import Cocoa
public func caputreRegion(toURL fileUrl: URL) -> URL {
let fileUrlPath = fileUrl.path
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-i", "-r", fileUrlPath]
task.launch()
task.waitUntilExit()
return fileUrl
}
func captureWindow(toURL fileUrl: URL) -> URL {
let fileUrlPath = fileUrl.path
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = ["-w", fileUrlPath]
task.launch()
task.waitUntilExit()
return fileUrl
}
func captureScreen(toURL fileUrl: URL) -> URL {
let fileUrlPath = fileUrl.path
let task = Process()
task.launchPath = "/usr/sbin/screencapture"
task.arguments = [fileUrlPath]
task.launch()
task.waitUntilExit()
return fileUrl
}
let url = URL.init(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Desktop").appendingPathComponent("test").appendingPathExtension("png")
caputreRegion(toURL: url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment