How to hide packages file like example.app? - swift

I am listing directories on a outlineView and I can't find how to hide package files.
let urls = try FileManager.default.contentsOfDirectory(at: url,
includingPropertiesForKeys: [.isDirectoryKey],
options: [.skipsHiddenFiles, .skipsPackageDescendants])
I can only so skip regular files like text.txt
All files with .app extension are listed

The skipsPackageDescendants option is meant to prevent deep directory traversal from traversing into the packages. The flag doesn't mean "ignore all packages". It actually means "don't enumerate the contents inside the package".
contentsOfDirectory() doesn't do a deep directory traversal, so the option is meaningless in that context. The enumerator(at:...) method would honor that flag by not enumerating files inside the .app bundle; it would still enumerate the .app itself.
If you're not interested in certain kinds of files, you would use
let urls = try FileManager.default.contentsOfDirectory(
at: url,
includingPropertiesForKeys: [.isDirectoryKey],
options: [.skipsHiddenFiles]
).filter { $0.pathExtension != "app" }

Related

How to get root path of the app container with reading from userDomainMask for comparison checks?

I an reading the contents of directory in the library directory like this:
FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first?
.appendingPathComponent("Sounds")
In different scenario's I get the following absolute paths when I get a file:
file:///var/mobile/Containers/Data/Application/DE78-43C6-BB73-4DA0E2B9D799/Library/Sounds/abc123.m4a
And other times I get a private directory in the path:
file:///private/var/mobile/Containers/Data/Application/DE78-43C6-BB73-4DA0E2B9D799/Library/Sounds/abc123.m4a
How can I normalize this because I want to compare if one of the files exist in an another array, but because of all the scenarios with and without private, my comparison checks fails?

Is there a way to set up AVPlayer without specifying filetypes?

I am using a TableViewController to list content of multiple filetypes, say ".mov" and ".wav".
I want to open these files in AVPlayer. However, I can only get these to play if I hardcode the filetype extension.
The problem with that is that if I designate ".mov" then my .wav files will not play, or vice versa.
[This is my code ][2]
If your path contains a name already, you can pass nil for type:
Bundle.main.path(forResource: path, ofType: nil)
Just make sure that your itemArray contains not only file names, but also file extensions, like this:
itemArray = [
"file1.mov",
"file2.wav",
...
]

Get path of file from a SubGroup in project with Swift 5

I want to access the path of plist file
I am accessing path with following line
I can access when file is in root folder with following line
let filePath = Bundle.main.path(forResource: Constants.googleServiceFileName, ofType: "plist")
but when i move files in sub group and use following code its return nil
let filePath = Bundle.main.path(forResource: Constants.googleServiceFileName, ofType: "plist",inDirectory: "Firebase")
but it's return nil ..
The group in which you keep a resource in Xcode is not necessarily related to its final location in the bundle. Your file is probably still in the top level Resources directory.
If you want to be completely sure where the build puts your file, look for the build log in the Report Navigator, locate the "Copy " line and expand it using the button at the right hand edge. That will tell you exactly where it is.
In the above, I have an Excel spreadsheet in the group testData. You can see that the build has put it in the top level Resources directory.

fileManger returns Error I have no permission to view files in Package

I' trying to get file names from directory. Now I have two ways to access that place: one by Open Panel where I can set directory by dragging files from opened by "Open Package Content" Finder window, manually select all files (end execute). And second – by iterating URLs. The second way doesn't work because of:
The file “glyphs” couldn’t be opened because you don’t have permission to view it
I can view it from Finder, access from the same app by Open Panel... Hmm.
I create URL like this:
print ("filename <\(filename)>, \(isDirectory ? "Directory" : "File")")
let urlf = url.appendingPathComponent(filename, isDirectory: isDirectory)
print( "\(urlf.isFileURL ? "this is a file" : "not a file:") \(urlf)" )
Output from this code is:
filename <glyphs>, Directory
this is a file: file:///Users/lukasz/Documents/ClanTestDesignSpace/masters/ClnNew-BlackUltraExpandedHigh.ufo/glyphs/
URL is OK, I made "copy glyphs as pathname" in finder and compared both: they're exactly the same except last slash. But it's not Directory anymore this is a file.
Anyway I send this URL to:
let fileURLs = try fileManager.contentsOfDirectory(at: urlf, includingPropertiesForKeys: nil)
Always returns Error.
I tried to change includingPropertiesForKeys: [URLResourceKey.nameKey] bot with no effect.
I checked sandbox (User Selected File : read/write). What I can check more?
From a sandboxed application, you cannot programatically access files in any directory. Once you enable User Selected File : read/write access in Sandbox settings, you can use an NSOpenPanel/NSSavePanel to let your user select the files they want to give access to, but without explicit user interaction you cannot access the ~/Documents folder that you tried accessing.

How to get the app list as displayed in Finder "Open in app" dialog for target file format (or file)?

I need to get list of the apps which compatible with a specific file format ( the same as displayed in Open With in Finder for a specific file).
At first tried function "LSCopyAllRoleHandlersForContentType" but in return not all apps ( often completely empty list). Programs appear in this list only if at least once in Finder make them the default for the file format.
Then I tried to get info about file associations formats from Info.plist of applications (using "CFBundleDocumentTypes" key). But for example for "png" I get only Browsers & no applications as Sketch, Skitch,Photoshop and etc.
Maybe I missing something...Thanks in advance.
Probably because your path was invalid:
// don't include file://
let url = NSURL.fileURL(withPath: "file:///Users/xxx/Desktop/public.html")
This works for me:
let url = URL(fileURLWithPath: "/Users/xxx/Desktop/public.html") as CFURL
if let applicationURLs = LSCopyApplicationURLsForURL(url, .all)?.takeRetainedValue() {
print(applicationURLs)
}