So my code so far looks like this:
import Foundation
import AppKit
print("Starting")
let base = NSWorkspace()
print("Launching Terminal")
base.launchApplication("Terminal") //launches terminal
print("Terminating terminal")
let apps = base.runningApplications
for app in apps {
if app.localizedName == "Terminal" {
print(app.localizedName)
app.terminate() //Terminate
break
}
}
I have figured out how to start terminal and then how to close it, but I don't know how to pass it a command. The command I want to pass it is: "screencapture ~/Desktop/screenshot.jpg"
As Martin suggested this executes screencapture ~/Desktop/screenshot.jpg
in Swift 3
let fileURL = try! FileManager.default.url(for: .desktopDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
Process.launchedProcess(launchPath: "/usr/sbin/screencapture", arguments: [fileURL.appendingPathComponent("screenshot.jpg").path])
The way to get the URL to the desktop folder works only in non-sandboxed apps.