0

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"

user6879072
  • 441
  • 1
  • 7
  • 17
  • 1
    Why would you want to start a Terminal for this purpose? It should be possible to execute /usr/sbin/screencapture directly via NSTask (or Process in Swift 3). – Martin R Oct 02 '16 at 12:39
  • I taught about that but where does that save the screenshot what is the name of the file and in what file format? – user6879072 Oct 02 '16 at 12:41
  • Type "man screencapture" in the Terminal and you'll see all its command-line options, e.g. `/usr/bin/screencapture -t pdf destinationFile`. – Martin R Oct 02 '16 at 12:43
  • But this is for the command if type it in terminal, but if I launch screencapture it runs and gives me the following text: "Last login: Sun Oct 2 14:45:19 on ttys002 Kristofs-iMac:~ kocsiskritsof$ /usr/sbin/screencapture ; exit; screencapture: no file specified logout Saving session... ...copying shared history... ...saving history...truncating history files... ...completed. [Process completed]" After this I can't do anything but exit. – user6879072 Oct 02 '16 at 12:47

1 Answers1

0

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.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • This works, thanks! Would you mind giving me a little bit of explanation of the code or point me to the proper documentation. – user6879072 Oct 02 '16 at 12:56
  • The first line retrieves the URL for the desktop of the user domain. The second line executes a shell task. Command- or Option-click on the symbols to get the declaration / documentation. – vadian Oct 02 '16 at 13:02
  • And is it possible to capture all the monitors like if I pressed CMD+Shift+4 and two pictures would be saved to the desktop each with one of the desktops? – user6879072 Oct 02 '16 at 13:08
  • I guess, I'm not that familiar with `screencapture`, Read the man page and add further parameters to the `arguments` array. – vadian Oct 02 '16 at 13:10
  • Ok, so I found that the command to take a screenshot of the second window is: "screencapture files ~/Desktop/screenshot.jpg" So how do I add the "files" argument? UPDATE: Nevermind I figured it out – user6879072 Oct 02 '16 at 13:30
  • `Process.launchedProcess(launchPath: "/usr/sbin/screencapture", arguments: ["files", fileURL.appendingPathComponent("screenshot.jpg").path])` – vadian Oct 02 '16 at 13:32