Question

For my little program that I'm running called SourceControl I'm trying the following: Read a file(that was downloaded from internet) at a certain (for now from a static) path on the computer.

The main idea is to get the source from where the file was downloaded.

like so:

File source (image here)

I've tried some code but what I get is a pointer to another object which isn't really what I'd like... I want to show the actual string like so: "http://www.sunjets.be/_images/boekingsengine/grevas1_nl.jpg"

Get the NSFileExtendedAttributes to eventually get the the com.apple.metadata:kMDItemWhereFroms:

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //Static path to my file/image
        NSString* myPath = @"/Users/Verbe/Klassieke_Oudheid.JPG";

        //creating the filemanager
        NSFileManager *filemgr = [[NSFileManager alloc]init];

        //if the path exists, whiii it exists!
        if ([filemgr fileExistsAtPath:myPath])
        {
            NSLog(@"file exists!");

            //Set the fileattributes to the dictionary!
            NSDictionary *fileAttributes = [filemgr attributesOfItemAtPath:myPath error:
                                            nil];

            for (NSString* myKey in fileAttributes)
            {
                if ([myKey isEqualToString:@"NSFileExtendedAttributes"])
                {
                    NSLog(@"MyKey = %@ with attribute: %@",myKey, [fileAttributes objectForKey:myKey]);

                }
            }

        }
        else
        {
            NSLog(@"File does not exist!");
        }

}
    return 0;

The result is the following:

2013-10-11 04:23:32.167 CustomInit[1016:303] file exists! 2013-10-11 04:23:32.190 CustomInit[1016:303] MyKey = NSFileExtendedAttributes with attribute: { "com.apple.metadata:kMDItemDownloadedDate" = <62706c69 73743030 a1013341 b8078c4f a88e8a08 0a000000 00000001 01000000 00000000 02000000 00000000 00000000 00000000 13>; "com.apple.metadata:kMDItemWhereFroms" = <62706c69 73743030 a1015f10 3b687474 703a2f2f 7777772e 73756e6a 6574732e 62652f5f 696d6167 65732f62 6f656b69 6e677365 6e67696e 652f6772 65766173 315f6e6c 2e6a7067 080a0000 00000000 01010000 00000000 00020000 00000000 00000000 00000000 0048>; "com.apple.quarantine" = <30303030 3b353235 37353463 663b5361 66617269 3b>; } Program ended with exit code: 0

Note that the com.apple.metadata:kMDItemDownloadedDate and com.apple.metadata:kMDItemWhereFroms are actually there!

What's going on here and how do I fix it?

Was it helpful?

Solution

These are Metadata (Spotlight) attributes, so you're meant to use the Metadata framework. Create an MDItem with the URL to the file and ask that item for its value for the attribute.

Like so:

MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, myFileURL);
NSArray *whereFroms = CFBridgingRelease(MDItemCopyAttribute(item, kMDItemWhereFroms));

No plist-decoding necessary.

OTHER TIPS

Value return from [[fileAttributes objectForKey:myKey] objectForKey:@"com.apple.metadata:kMDItemWhereFroms"] are apple binary plist (bplist00). Use propertyListFromData:mutabilityOption:format:errorDescription: function of NSPropertyListSerialization class for Conversion.

NSString* myPath = @"/Users/new/Downloads/TelephoneBill.pdf";

//creating the filemanager
NSFileManager *filemgr = [[NSFileManager alloc]init];

//if the path exists, whiii it exists!
if ([filemgr fileExistsAtPath:myPath])
{
    NSLog(@"file exists!");

    //Set the fileattributes to the dictionary!
    NSDictionary *fileAttributes = [filemgr attributesOfItemAtPath:myPath error:
                                    nil];

    for (NSString* myKey in fileAttributes)
    {
        if ([myKey isEqualToString:@"NSFileExtendedAttributes"])
        {
            NSLog(@"MyKey = %@ with attribute: %@",myKey, [fileAttributes objectForKey:myKey]);

            NSData *whereFromData = [[fileAttributes objectForKey:myKey] objectForKey:@"com.apple.metadata:kMDItemWhereFroms"];
            NSString *error;
            NSPropertyListFormat format;
            id plist = [NSPropertyListSerialization propertyListFromData:whereFromData
                                                     mutabilityOption:NSPropertyListImmutable
                                                               format:&format
                                                     errorDescription:&error];
            NSLog(@"plist %@",plist);
        }
    }

}
else
{
    NSLog(@"File does not exist!");
}  



plist (
    "https://www.airtel.in/myaccount/BillGuide/RevamP/download?fileName=EA29BB",
    "https://www.airtel.in/myaccount/BillGuide/billSummary.action?param=myBillSummary&res=6DAED0342C197C"
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top