So I had it working, then I tweaked something re: doc controller - subclassed as
class DocumentController : NSDocumentController {
override func typeForContents(of url: URL) throws -> String {
return "Document"
}
}
and a URL helper to resolve .webloc files:
extension URL {
var webloc : URL? {
get {
do {
let data = try Data.init(contentsOf: self) as Data
let dict = try! PropertyListSerialization.propertyList(from:data, options: [], format: nil) as! [String:Any]
let urlString = dict["URL"] as! String
return URL.init(string: urlString)
}
catch
{
return nil
}
}
}
}
when I lost the Finder menu "open with" entry for my test app while triaging an open vs make file doc issue - see below. I do have an app delegate openFile: as
func application(_ sender: NSApplication, openFile: String) -> Bool {
let urlString = (openFile.hasPrefix("file://") ? openFile : "file://" + openFile)
let fileURL = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!
return self.doOpenFile(fileURL: fileURL)
}
func doOpenFile(fileURL: URL) -> Bool {
let dc = NSDocumentController.shared()
var status : Bool = false
var itemURL = fileURL
// Resolve alias, container webloc bookmark before target bookmarking
if let original = (itemURL as NSURL).resolvedFinderAlias() { itemURL = original }
if itemURL.absoluteString.hasSuffix("webloc") {
if isSandboxed() != storeBookmark(url: itemURL as URL) {
Swift.print("Yoink, unable to bookmark (\(itemURL))")
}
if let webURL = itemURL.webloc {
itemURL = webURL
}
}
else
{
if isSandboxed() != storeBookmark(url: itemURL as URL) {
Swift.print("Yoink, unable to bookmark (\(itemURL))")
return false
}
}
dc.openDocument(withContentsOf: itemURL, display: true, completionHandler: { (nextURL, wasOpen, error) in
if error != nil {
NSApp.presentError(error!)
Swift.print("Yoink, unable to open doc for (\(String(describing: nextURL)))")
status = false
}
else
{
let newWindow = nextURL?.windowControllers.first?.window
(newWindow?.contentView?.subviews.first as! MyWebView).next(url: itemURL)
newWindow?.offsetFromKeyWindow()
status = true
}
})
return status
}
I was getting an error (presentError:) by my view controller as I was trying to open a 'webloc' url rather than make a doc for it - subtle distintction?
Anyway now my menu entry is gone in the Finder, so I'm in search of a checklist to verify I have [still] set things up right, My app's single doc Info.plist appears as (relevant do docs) as:
when am I missing !? This work uses others' re: resolving Finder alias, sandbox support, and webloc contents resolution.
I was trying to resolved an issue where a search string encoded filename - webloc, was not getting resolved by my -URL.webloc() function when I make "some" change and lost the Finder's "open with" entry for my app.
So for the benefit of me and others, we need a check list (recipe) to run thru. This app is a test mini-browser, using WKWebView with a next(url:) function to load the next url.