Find out if .png file is a screenshot? - macos

When I get the attributes of a specific file with the following code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:p error:nil];
Is there an attribute to know if it's a screenshot or not?
Right now I'm using this awkward piece of code:
if ([fileExtension isEqualToString:#"png"]) {
NSMutableArray *separatetFilename;
separatetFilename = [NSMutableArray arrayWithArray:[fileName componentsSeparatedByString:#" "]]; /* screenshots have multiple spaces in their names, I split them up to use the information */
if ([separatetFilename count] == 4) { /* screenshots names have 4 parts when splittet by spaces */
if ([[separatetFilename objectAtIndex:0] isEqualToString:#"Bildschirmfoto"]) { /* Bildschirmfoto = Screenshot in German */
/* Do Something */
}
}
}
With this solution I would have to put in localized Strings for the "Screenshot" string... I searched the documentary but didn't find anything. Is there a "isScreenshot" flag or something I could read?

Your question "Is there a "isScreenshot" flag or something I could read?" and the answer is "YES".
I know of two ways to get the answer:
a) ask the NSFile~Manager
b) ask for metadata.
You already used:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:p error:nil];
If you now have a look at the keys of this dictionary e.g. with
NSLog( #"the keys: %#", [attributes allKeys] );
you will find the key #"NSFileExtendedAttributes*". The value of this key is a dictionary with keys like com.apple.metadata:kMDItemIsScreenCapture or com.apple.metadata:kMDItemScreenCaptureType or .... This is what you asked for. The corresponding values are NSData-objects with a binary property list. Evaluating these plist give something like 1 (it is a screenshot) and window for a window screenshot or selection for a part of the window.
Looking at the metadata can be done with:
MDItemRef item = MDItemCreate( kCFAllocatorDefault, (CFStringRef)p );
If you only need the above mentioned values create a list (an NSArray) with the corresponding keys:
NSArray *ary = [NSArray arrayWithObjects:
#"kMDItemIsScreenCapture", #"kMDItemScreenCaptureType", nil];
NSDictionary *dict =
(NSDictionary *)MDItemCopyAttributes( item, (CFArrayRef)ary );
and test what it returns;
NSLog( #"the metadata are %#", dict );
In this case the returned values are __NSCFBoolean (i.e. NSNumber) and __NSCFString (i.e. NSString) which is a bit simpler to work with than a binary plist.

Related

NSUserDefaults: how to get only the keys I've set

All of the methods to get keys from NSUserDefaults return heaps of keys from domains other than the app itself (e.g., NSGlobalDomain). I just want the keys and values that my app has set. This is useful for debugging and verifying that there are no orphaned keys, etc.
I could ignore the keys that aren't mine (if I know all of them -- during development I may have set keys I'm no longer using), but there might be a collision of keys in other domains and I'll not see my app's value.
Other discussions suggest looking at the dictionary file associated with the app, but that's not very elegant.
How can I get only my app's keys form NSUserdefaults?
Elegant approach
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *dict = [defaults persistentDomainForName:bundleIdentifier];
File approach:
NSString *bundleIdentifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleIdentifier"];
NSString *path = [NSString stringWithFormat:#"~/Library/Preferences/%#.plist",bundleIdentifier];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[path stringByExpandingTildeInPath]];
NSArray *keys = [dict allKeys];
Tested with sandboxing.
Marek's code updated to Swift 5:
guard let bundleIdentifier = Bundle.main.bundleIdentifier else { return }
let dict = UserDefaults.standard.persistentDomain(forName: bundleIdentifier)

Add Mutable Dictionary to a Mutable Dictionary for conversion to JSON

I am trying to build a dictionary which has as dictionary within it (eventually I hope to convert to a JSON). The problem is I am having problems building it.
So far I have this, what it should do is build a small dictionary with keys and add it to a larger dictionary, reset and then load the small dict and then add it to the large one.
NSMutableDictionary *nestedList = [[NSMutableDictionary alloc]init];
NSMutableDictionary *nestedSections = [[NSMutableDictionary alloc] init];
[nestedList addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:46], #"menuHeight",
#"editText", #"menuMethod",
[NSNumber numberWithInt:1], #"menuOption",
nil]];
[nestedSections addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
nestedList, "#Basic",
nil]];
[nestedList removeAllObjects];
[nestedList addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:92], #"menuHeight",
#"sendText", #"menuMethod",
[NSNumber numberWithInt:1], #"menuOption",
nil]];
[nestedSections addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
nestedList, "#Pro",
nil]];
I then hoped to address like so;
NSString *string = [[nestedSections objectForKey:#"Pro"] objectForKey:#"menuMethod"];
NSLog(#"Method is : %#", string);
Log would hope to read sendText
The first dictionary builds fine but as soon as I try and add it to the second t bums out with EXC_BAD_ACCESS
I think it is a memory addressing problem because they are both mutable but I am not sure, maybe nestedList should not be mutable. Any help appreciated.
Ultimately I would like to convert this to a JSON like;
{
"Basic":
{
"menuHeight":"46",
"menuMethod":"editText",
"menuOption":"1",
},
"Pro":
{
"menuHeight":"96",
"menuMethod":"sendText",
"menuOption":"1",
}
}
A. NSMutableDictionary does not copy the values (only the keys). Therefore you add the same dictionary two times and change both (= the one) when removing objects and so on. Beside this in your sample JSON the numbers looks like strings not like numbers. I think, that this is a typo.
B. Adding modern Objective-C for better readability it should look like this:
NSDictionary *basicDictionary =
#{
#"menuHeight" : #46,
#"menuMethod" : "editText",
#"menuOption : #1
}
NSDictionary *proDictionary =
#{
#"menuHeight" : #96,
#"menuMethod" : "sendText",
#"menuOption : #1
}
NSDictionary *nestedSections = #{ #"Pro" : proDictionary, #"Basic" : basicDictionary };

Read plist into nsmutabledictionary, update the dictionary and then save it again

I am really new to saving files in objective C but what I'm trying to accomplish is reading a plist file located in the documents directory on launch or creating it if it doesn't exist.
It should be read in to a NSMutableDictionary. Later on in the app I should be able to save items to the NSMutableDict with categories as keys + text.
The before launch in the viewWillUnload the NSMutableDictionary should be saved into the plist file again.
I have created the plist but I need a way to write to the NSMutableDictionary the right way (category and my result.text string.
And I also need to save the NSMutableDictionary to the plist file and read the plist into the dictionary on launch.
Some help with this would be awsome :D
Thanks guys.
In the savefile void I am doing this:
storeDict = [[ NSMutableDictionary alloc]
init];
[storeDict setObject:resultText.text forKey:#"kvitto"];
[storeDict setObject:kategori forKey:#"kategori"];
[storeDict writeToFile:[self saveFilePath] atomically:YES];
saveFilePath looks like this:
- (NSString *) saveFilePath {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[path objectAtIndex:0] stringByAppendingString:#"savefile.plist"];
}
The values are strings collected from a code that the user have scanned so don't worry bout them.
Well so how do I save this correctly keeping the data that already exists in the savefile.plist.
Thanks again
If you want dictionaries and arrays contained in the dictionary to be mutable as well then do the following:
NSData *data = [NSData dataWithContentsOfFile:path];
NSError *error;
storeDict =
[NSPropertyListSerialization
propertyListWithData:data
options:NSPropertyListMutableContainersAndLeaves
format:nil
error: &error];
Why can't you use writeToFile api? Before writing question here you must google or check apple documentation for NSMutableDictionary.
[dict writeToFile:path atomically:YES];
To load saved dictionary
dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
Happy coding!

Reading a plist

I'm trying to read ~/Library/Preferences/com.apple.mail.plist (on Snow Leopard) to get the email address and other information to enter into the about dialog. I'm using the following code, which is obviously wrong:
NSBundle* bundle;
bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:#"~/Library/Preferences/com.apple.mail.plist" ofType:#"plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *item = [plistData valueForKeyPath:#"MailAccounts.Item 2.AccountName"];
NSLog(#"Result = %#", item);
Moreover, the value I need to read is MailAcounts -> Item 2 -> AccountName and I am not sure I am doing this correctly (due to the space in the Item 2 key).
I tried reading Apple's developer guide to plist files but no help there.
How can I read a plist and extract the values as an NSString?
Thanks.
The first level is an array, so you need to use "MailAccounts.AccountName" and treat it as NSArray*:
NSString *plistPath = [#"~/Library/Preferences/com.apple.mail.plist" stringByExpandingTildeInPath];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *item = [plistData valueForKeyPath:#"MailAccounts.AccountName"];
NSLog(#"Account: %#", [item objectAtIndex:2]);
Alternatively you can go by keys and pull the array from "MailAccounts" first using valueForKey: (which will yield NSArray*) and then objectAtIndex: to get the dictionary of that particular account (useful if you need more than the name).
Two things:
You don't want or need to use NSBundle to get the path to the file. The file lies outside of the app bundle. So you should just have
NSString *plistPath = #"~/Library/Preferences/com.apple.mail.plist";
You have to expand the tilde in the path to the user directory. NSString has a method for this. Use something like
NSString *plistPath = [#"~/Library/Preferences/com.apple.mail.plist" stringByExpandingTildeInPath];

Sort a NSMutuableArray

I have a NSMutableArray that is loaded with a inforamtion from a dictionary...
[self.data removeAllObjects];
NSMutableDictionary *rows = [[NSMutableDictionary alloc] initWithDictionary:[acacheDB.myDataset getRowsForTable:#"sites"]];
self.data = [[NSMutableArray alloc] initWithArray:[rows allValues]];
There are two key value pairs in the rows dictionary.
I need to sort the self.data NSMutableArray in alphabetical order.
How is this accomplished??
thanks
tony
If the values are plain strings you can use the following to create a sorted array:
NSArray *sorted = [values sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
This should do:
[self.data removeAllObjects];
NSArray *values = [[acacheDB.myDataset getRowsForTable:#"sites"] allValues];
NSSortDescriptor *alphaDescriptor = [[NSSortDescriptor alloc] initWithKey:#"DCFProgramName" ascending:YES selector:#selector(localizedCaseInsensitiveCompare:)];
NSArray *sortedValues = [values sortedArrayUsingDescriptors:[NSMutableArray arrayWithObjects:alphaDescriptor, nil]];
[alphaDesc release];
[self.data addObjectsFromArray:sortedValues];
There's no need to clear an NSMutableArray if you're replacing it shortly afterwards.
There's no need to create an additional NSMutableDictionary, if you're not modifying anything in it.
There's no need to create an additional NSMutableArray, if you could just as well just add the values to the existing one.
Also: There are some serious memory leaks in your code. (2x alloc + 0x release = 2x leak)
Edit: updated code snippet to reflect OP's update on data structure.

Resources