Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

swift5 - Open a short video using QuickTime Player

I have a short video on my local machine. I am trying to open it using NSWorkspace on QuickTime Player.

let stringUrl = "~/Desktop/myrec.mov"
let url = URL(string: stringUrl)!

let config = NSWorkspace.OpenConfiguration()
config.activates = true

NSWorkspace.shared.open(
    [url],
    withApplicationAt: URL(string: "~/Applications/QuickTime Player")!,
    configuration: config
)

The error I am getting is:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file cap/cap.swift

Which has to do with withApplicationAt being the incorrect URL and returning nil.

I'm sure withApplicationAtURL is wrong, but I have no idea how to find the url for the quicktime player app.

I am very new to Swift and am having trouble reading the docs, especially since they don't seem up to date (e.g. my compiler says openFile is deprecated and led me to open).

I'm also not sure if this is the correct/best way to go about accomplishing what I am trying to do (open a small local video on quicktime player).

Any recommendations, tips, advice, or answers would be greatly appreciated!

question from:https://stackoverflow.com/questions/65874089/open-a-short-video-using-quicktime-player

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this. Works on my Mac:

let stringUrl = "/Users/whoever/Desktop/myrec.mov"
let url = URL(fileURLWithPath: stringUrl)

let config = NSWorkspace.OpenConfiguration()
config.activates = true

NSWorkspace.shared.open(
    [url],
    withApplicationAt: URL(fileURLWithPath: "/System/Applications/QuickTime Player.app"),
    configuration: config
)

Please note that that I replaced the initializer of URL with the correct one. Also note, that I've swapped the QuickTime Player path with the one that Finder gives me (right-click while holding option key).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...