I'm using the following code to set desktop picture:
NSURL* newImage = [[NSURL alloc] initFileURLWithPath:#"/Users/name/Pictures/test.png"];
[[NSWorkspace sharedWorkspace] setDesktopImageURL:newImage forScreen:screen options:nil error:&nserror];
It works fine and the desktop picture changed as I required. But it does not change the system-wide desktop preferences (for example, change picture every 30 minutes). How can I change the system-wide desktop preferences?
Take a look at Preferences and Settings Programming Guide. This should help.
Edit:
Here is the sample code:
NSString* newImgPath = #"/Users/cody/Desktop/stuff/imgs/Black_mac.png";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary* desktopDict = [NSMutableDictionary dictionaryWithDictionary:[defaults persistentDomainForName:#"com.apple.desktop"]];
NSMutableDictionary* bgDict = [desktopDict objectForKey:#"Background"];
NSMutableDictionary* spaces = [bgDict objectForKey:#"spaces"];
[spaces enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSMutableDictionary* obj, BOOL *stop) {
[obj enumerateKeysAndObjectsUsingBlock:^(id key, NSMutableDictionary* prefs, BOOL *stop) {
[prefs setObject:newImgPath forKey:#"ImageFilePath"];
[prefs setObject:newImgPath forKey:#"NewImageFilePath"];
[prefs setObject:#"Never" forKey:#"Change"];
}];
}];
//NSLog(#"%#", desktopDict);
[defaults setPersistentDomain:desktopDict forName:#"com.apple.desktop"];
if ([defaults synchronize] == NO)
NSLog(#"synchronize failed");
// Restart dock
system ("/usr/bin/killall Dock");
Related
I'm working on my app but i need some help.
I need a 'php-like session' in Objective C (without using connection to the internet)
I've thought about global vars, but my app seems to reset them when reloading the view.
This is my current code
SecondViewController.h
#interface SecondViewController : UIViewController
{
NSString * string;
}
SecondViewController.m
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
if (! [string isEqualToString:#"Hello"])
{
NSLog(#"Hello");
string = #"Hello";
}
else
{
NSLog(#"Bye");
}
}
#end
But everytime I reload SecondViewController the 'string' is reseted to its default value.
I'm looking for something that we use in php (a.e. $_SESSION['string'] = 'hello')
It could be helpful to you . It stores the values until unless delete the app from your device .
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving an NSString
[prefs setObject:#"TextToSave" forKey:#"keyToLookupString"];
// saving an NSInteger
[prefs setInteger:42 forKey:#"integerKey"];
// saving a Double
[prefs setDouble:3.1415 forKey:#"doubleKey"];
// saving a Float
[prefs setFloat:1.2345678 forKey:#"floatKey"];
// This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
[prefs synchronize];
**Retrieving**
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myString = [prefs stringForKey:#"keyToLookupString"];
// getting an NSInteger
NSInteger myInt = [prefs integerForKey:#"integerKey"];
// getting an Float
float myFloat = [prefs floatForKey:#"floatKey"];
In my app, the user can save an image to their documents directory. At launch, I grab the image, add a border, and put it into a UIImageview like this....
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/ImageOne.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
imageOneView.image = imageWithBorder;
Ideally, I like to check that the image is there first before adding a border. If not, load an "image not available" placeholder. Something like this:
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/ImageOne.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
NSError * error;
if (error != nil) {
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
imageOneView.image = imageWithBorder;
} else
//no image saved
[imageOneView setImage:[UIImage imageNamed:#"photoNotAvailable.png"]];
}
Of course, this doesn't work. I just can't seem to figure out how handle if "ImageOne.jpg" isn't found.
As it turns out, I need to do other things with the image elsewhere within the app. This would also depend on whether or not the user had saved an image or not. So in my method where the user can save the image, I send out a NSNotification that the image has changed. Then on my MainView, I look for the notification and key off that.
When saved:
[collectionOneUserDefinedDefaults setObject:#"image added" forKey:#"collectionOneImageAdded"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"collectionOneImageChanged" object:self];
Then in my MainView I look for the notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateCollectionOneImage) name:#"collectionOneImageChanged" object:nil];
- (void)updateCollectionOneImage {
//check if an image was ever saved, if so, replace the noPhotoAvailalble placeholder
NSUserDefaults *collectionOneUserDefinedDefaults = [NSUserDefaults standardUserDefaults];
NSString *collectionOneImageTextString = [collectionOneUserDefinedDefaults stringForKey:#"collectionOneImageAdded"];
if (collectionOneImageTextString == nil || [collectionOneImageTextString isEqualToString:#""]) {
[collectionOneImage setImage:[UIImage imageNamed:#"photoNotAvailable.png"]];
}
else {
//pull in collection one image from the Documents folder
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/CollectionOneImage.jpg", docDirectory];
UIImage *unborderedImage = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];
//image found....add border
UIImage *imageWithBorder = [self addBorderToImage:unborderedImage];
collectionOneImage.image = imageWithBorder;
}
}
It works perfectly. It in turn builds in the error handling. If the user saved and image, it gets loaded. If not, a placeholder image is loaded.
Your error handling here is doing nothing and is misguided. All you need do is simply check if unborderedImage is nil or not.
we are working on a C++ Qt applciation that copies selected text and/or images from external applications, modifies it and then paste it back. Since we are on Mac, we are doing this part with Objective-C.
We are having problems trying to get an image from the Pasteboard. It works fine for text, but we are not sure about how to handle images or combination of text+image.
Since we dont know what the user might select, we should be able to perform a generic retrieval of content of the pasteboard to modify it and putting it back in the pasteboard.
We've try this:
//we thought about retrieving some generic item from pasteboard, using NSPasteboardItem
NSArray *classes = [[NSArray alloc] initWithObjects:[NSPasteboardItem class], nil];
NSDictionary *options = [NSDictionary dictionary];
NSArray *auxArray = [[NSPasteboard generalPasteboard] readObjectsForClasses:classes options:options];
NSData *archived_data = [auxArray objectAtIndex:0];
Our solution for handling text was:
NSString *text = [[NSPasteoard generalPasteboard] stringForType:NSStringPboardType];
string text_str = string([text UTF8String]);
It didnt work, so, How can we get the user selection from the pasteboard?
We need to get the raw bytes or rtf content in order to modify it as we need, and then putting it back in the pasteboard and paste it back replacing the original user selection.
Thanks!
I think this function will help you
- (IBAction)paste:sender
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *classArray = [NSArray arrayWithObject:[NSImage class]];
NSDictionary *options = [NSDictionary dictionary];
BOOL ok = [pasteboard canReadObjectForClasses:classArray options:options];
if (ok)
{
NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
NSImage *image = [objectsToPaste objectAtIndex:0];
[imageView setImage:image];
}
}
Hope you can help me with this.
I have an app that needs to show one of two maps via the setting of a UISwitch. The settings.bundle is all set up and I am trying to write an If statement to determine if the switch is on or off, and to display the right image.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL Enabled = [defaults boolForKey:#"zones_preference"];
if (Enabled == #"Enabled") {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
}
else {
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
This builds without error, but doesn't load the image into the ScrollView. Could anyone advise on where I am going wrong?
Well, the code you've posted only creates a UIImageView object and doesn't do anything more. It's a leak too.
There's one more error in the line
if (Enabled == #"Enabled") {
Here, you are comparing a boolean to a string which will evaluate to false automatically so it needs to be corrected too.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL enabled = [defaults boolForKey:#"zones_preference"];
UIImageView * imageView;
if ( enabled ) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withzones.jpg"]];
} else {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"withoutzones.jpg"]];
}
imageView.frame = imageViewFrame; // Where "imageViewFrame" is an appropriate frame.
[scrollView addSubview:imageView];
[imageView release];
Deepak, that was very useful and thank you. I had been working with a variable, but the mistake I had made was that I was trying to add the setup of the variable in with the creation of the UIImageView.
I'll work with this and see how I get on.
I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image.
I want it to be like other apps where it shows icon.png when not clicked and icon-active.png when clicked.
The current code is this:
- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
[[NSColor selectedMenuItemColor] set];
NSRectFill(rect);
}
// Draw some text, just to show how it's done.
NSString *text = #"3"; // whatever you want
NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
textColor = [NSColor selectedMenuItemTextColor];
}
NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
msgFont, NSFontAttributeName,
textColor, NSForegroundColorAttributeName,
paraStyle, NSParagraphStyleAttributeName,
nil];
[paraStyle release];
NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;
[text drawInRect:msgRect withAttributes:msgAttrs];
}
Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501.
Thanks!
Use an NSImage and draw it where desired. For example:
NSString *name = clicked? #"icon-active" : #"icon";
NSImage *image = [NSImage imageNamed:name];
NSPoint p = [self bounds].origin;
[image drawAtPoint:p fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
If this is for a status item and you just want an icon with no programmatic drawing, drop the view and set the status item's image and alternateImage. The former is what the status item uses normally; the status item switches to the alternate image (if it has one) when the user opens its menu.