Reputation: 4027
let photos = [NSURL(string: "/photo1.png")!, NSURL(string: "/photo2.png")!]
NSWorkspace.sharedWorkspace().openURLs(photos, withAppBundleIdentifier: "com.apple.Preview", options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
I tried the code above. It returns true and opens the preview app but not with photos. Single item in array didn't work either. What am I doing wrong?
Upvotes: 1
Views: 251
Reputation: 398
I know this was asked a long time ago, but I recently needed to do the same thing and got it working by using full paths to the image files.
// get application url from bundle ID
guard let previewUrl = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.Preview")
else { return }
// paths to image files
let urls = [
URL(fileURLWithPath: "/Users/myuser/path/to/image1.jpg"),
URL(fileURLWithPath: "/Users/myuser/path/to/image2.jpg")
]
// launch configuration
let config = NSWorkspace.OpenConfiguration()
config.addsToRecentItems = false
config.createsNewApplicationInstance = true
config.allowsRunningApplicationSubstitution = false
config.activates = true
// launch Preview.app
NSWorkspace.shared.open(urls, withApplicationAt: previewUrl, configuration: config) { app, error in
if let error = error {
print("unable to launch Preview.app: \(error.localizedDescription)")
return
}
if let app = app {
print("launched Preview.app with pid \(app.processIdentifier)")
}
}
Upvotes: 0