I create files with proprietary extension (just a ".string") and want to hide it to the user.
I am trying the following :
let savePanel = NSSavePanel()
savePanel.title = "My file"
savePanel.prompt = "Create"
savePanel.beginWithCompletionHandler() { (result) -> Void in
if (result == NSFileHandlingPanelOKButton) {
let fileWithExtensionURL = savePanel.URL!.URLByAppendingPathExtension("myExt")
saveFile() // Create the file
// try to hide extension in Finder
do { try NSFileManager.defaultManager().setAttributes([NSFileExtensionHidden: NSNumber(bool: true)], ofItemAtPath: fileWithExtensionURL.path!) } /
catch { Swift.print("Unable to hide extension", error) }
}
}
The try is successful (no error message), but extension still shows in Finder.
Writing is done with key archiving :
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
// encode objects
archiver.finishEncoding()
data.writeToURL(fileWithExtensionURL, atomically: true)
1. Is it a question of access rights to the file ? How to set it ?
Note : When I look at file information in Finder , the check box "hide extension" is set ON but disabled
2. I would also need this file to launch automatically the application if double clicked. How can I set it ?
3. I understand this should be done with UTI instead of mere file extension ; but my app is not document based, and I've found nowhere how to create UTI for the files in this case.
Thanks for any help on this.
I create files with proprietary extension (just a ".string") and want to hide it to the user.
The issue you’re having is that macOS does not know that
.string
is a file extension. You can reproduce this without any code:
Create a text file called
.foo.flibble
In Finder, do a File > Get Info.
Note the Hide Extension checkbox is greyed out.
If you repeat the steps with a file extension that the system knows by default (for example,
foo.ps
), the
Hide Extension checkbox is visible.
To make this work you have to tell the system that
.string
is a valid file extension by configuring various properties in your
Info.plist
. Use
UTExportedTypeDeclarations
if your app should be considered authoritative for that extension, or
UTImportedTypeDeclarations
if you only want your app’s definitions to apply if no other definitions are available.
Finally, if you want your app to open when the user double clicks one of these files, claim it via
CFBundleDocumentTypes
.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"