pandomic
pandomic

Reputation: 627

Swift (macOS): Launch TextEdit with file

I'm trying to launch a TextEdit app and pass a file path to it to open, but unfortunately, it doesn't work - TextEdit is launched in a regular mode.

Here it is what I'm trying to do:

let workspace = NSWorkspace.shared
let textEditUrl = URL(fileURLWithPath: "/Applications/TextEdit.app")
let file = "‎⁨⁩/path/to/file.whatever⁩"
let configuration = [NSWorkspace.LaunchConfigurationKey.arguments : file]

// I also tried
// let configuration = [NSWorkspace.LaunchConfigurationKey.arguments :[file]]

do {
    try workspace.launchApplication(
        at: textEditUrl, options: [], configuration: configuration
    )
} catch {
    // Left blank
}

Upvotes: 1

Views: 366

Answers (1)

Matteo Pacini
Matteo Pacini

Reputation: 22866

There's the good old NSTask - renamed Process in Swift.

Process.launchedProcess(launchPath: "/usr/bin/open", arguments: [
    "-a",
    "TextEdit",
    "PATH_TO_YOUR_FILE"
])

Upvotes: 1

Related Questions