1

I'm trying to read the URLResourceKey.customIconKey and URLResourceKey.thumbnailKey file attributes with a Swift 4 app; as you guess I have some issue because no NSImage will be returned.

Questions: Does the two above keys refers to the custom icon (i.e. the Adobe Acrobat documents icon) and the thumbnails of the png / jpeg files?

If it's not the case, what the two keys really refers to?

Apple documentation has a really short description and google gave no results about.

By the way, here is the code I'm working on.

    // Get file informations
    let requiredAttributes = [URLResourceKey.nameKey, URLResourceKey.creationDateKey, URLResourceKey.contentModificationDateKey, URLResourceKey.fileSizeKey, URLResourceKey.isPackageKey, URLResourceKey.thumbnailKey, URLResourceKey.customIconKey]
    do {
        let properties = try (localURL as NSURL).resourceValues(forKeys: requiredAttributes)
        ...
        let customIcon = properties[URLResourceKey.customIconKey] as? NSImage ?? nil
        if customIcon != nil {
            let saveURL = URL(fileURLWithPath: "/Users/foo/Desktop/fileicon.png")
            try customIcon!.savePNGRepresentationToURL(url: saveURL)
        }

        let customThumbnail = properties[URLResourceKey.thumbnailKey] as? NSImage ?? nil
        if customThumbnail != nil {
            let saveURL = URL(fileURLWithPath: "/Users/foo/Desktop/filethumbnail.png")
            try customThumbnail!.savePNGRepresentationToURL(url: saveURL)
        }
   } catch {...}

When inspecting properties dictionary the two customIconKey and thumbnailKey are not set.

Thank you for your help.

Francesco Piraneo G.
  • 882
  • 3
  • 11
  • 25
  • I think you should make a Set of the keys like `let requiredAttributes: Set = ...` and use URL instead of NSURL, then `let properties = try localURL!.resourceValues(forKeys: requiredAttributes)` and then access members like `properties.thumbnail`, etc. – Eric Aya Apr 03 '19 at 15:49
  • @ayaio - Can you point me to a working sample of the solution you propose? – Francesco Piraneo G. Apr 03 '19 at 16:39
  • @ayaio - I tried what you suggest and nothing change... – Francesco Piraneo G. Apr 03 '19 at 16:59
  • Hmm, unfortunately it looks like it's not implemented?! [screenshot](https://monosnap.com/image/pcMBJKvabwaXt3xDzzs17l4f1coJhy.png) This is weird, I could swear I used this a few years ago and it worked... I don't understand. – Eric Aya Apr 03 '19 at 17:25
  • I'm testing in Mojave. You too? Maybe it worked in previous macOS versions. I don't know why it's not available anymore. – Eric Aya Apr 03 '19 at 17:27
  • With `xattr -l com.apple.ResourceFork filePath | more` I can see that my file indeed has the custom icon. So we have to find out how to get that resource without using this not implemented method. I'll ping you if I find something. – Eric Aya Apr 03 '19 at 17:38
  • @ayaio in the meanwhile thank you for support. I confirm that I'm working under Mojave and the custom icon is marked unsupported by the IDE. But the thumbnail is not so it's should work! – Francesco Piraneo G. Apr 03 '19 at 17:50
  • @ayaio pls consider that when I query with `xattr` as you indicated, I don't see anything... I suppose that for my files (png and jpg) I have to generate the thumbnail by myself. – Francesco Piraneo G. Apr 03 '19 at 18:07
  • I've asked an expert about it. See https://stackoverflow.com/questions/38343186/write-extend-file-attributes-swift-example#comment97713820_38343753 Work in progress... – Eric Aya Apr 04 '19 at 07:25

1 Answers1

0

By replacing "URLResourceKey.customIconKey" with "URLResourceKey.effectiveIconKey" the let "properties" printed the following message :

NSURLEffectiveIconKey): <NSImage 0x60c000075fc0 Size={128, 128} Reps=(
    "<NSIconRefImageRep:0x60c00008af50 iconRef=0x103 size:128x128 pixels:128x128>",
    "<NSIconRefImageRep:0x60c00008aff0 iconRef=0x103 size:128x128 pixels:256x256>",
    "<NSIconRefImageRep:0x60c00008b040 iconRef=0x103 size:256x256 pixels:256x256>",...

Is it of any help?

Fredo
  • 153
  • 2
  • 13