Open a short video using QuickTime Player

戏子无情 提交于 2021-02-11 12:02:57

问题


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!


回答1:


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).



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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!