Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > change the desktop?

change the desktop?
Thread Tools
Wafflestomper
Fresh-Faced Recruit
Join Date: Jun 2003
Location: Charlottesville, VA
Status: Offline
Reply With Quote
Jul 10, 2003, 10:59 PM
 
I'm just trying to write a simple application to change the desktop. It doesn't seem like it should be that difficult, but I have no idea where to look for the right method call or whatever I might need. I've seen like 3 or 4 other applicatoins that change the desktop in various ways, but I can't seem to find the right thing.

As background, I'm fresh out of college and teaching myself how to program for os x, which is fun stuff. I'm reading/working through "OS X for Java Geeks" and "Cocoa Programming for Mac OS X" (which I just started yesterday). I've never really interacted with other applications/the system/a control panel or whatever it is that I'm trying to interact with, and I just don't know what exactly I'm trying to do.

any help would be appreciated

EDIT: I'm trying to change the desktop picture programmatically in case that was unclear
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 10, 2003, 11:27 PM
 
Last I checked, there was no way to do this with straight Cocoa. However, you can do it with an Applescript. In a Cocoa app, you can use NSTask to call command line applications, such as osascript (which will run applescripts from the command line). You can have a method like this:

- (void)changeDesktopPicture
{
NSString *command = @"tell application \"Finder\"\n set the desktop picture to \"Users:foobar:folder:newPic.jpg\"\n end tell";
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/osascript"];
[task setArguments:[NSArray arrayWithObjects:@"-e", command, nil]];
[task launch];
[task release];
}

You'll probably have to play with it a few times to make sure your Applescript syntax is correct and your path is correct, and you have to make sure you escape quotes and use '\n' for line breaks, as well as colons as path separators, and you need '-e' as the first argument passed to osascript...

Hope that helps...
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 10, 2003, 11:49 PM
 
Originally posted by disco_stu:
Last I checked, there was no way to do this with straight Cocoa. However, you can do it with an Applescript. In a Cocoa app, you can use NSTask to call command line applications, such as osascript (which will run applescripts from the command line). You can have a method like this:

- (void)changeDesktopPicture
{
NSString *command = @"tell application \"Finder\"\n set the desktop picture to \"Users:foobar:folder:newPic.jpg\"\n end tell";
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/osascript"];
[task setArguments:[NSArray arrayWithObjects:@"-e", command, nil]];
[task launch];
[task release];
}

You'll probably have to play with it a few times to make sure your Applescript syntax is correct and your path is correct, and you have to make sure you escape quotes and use '\n' for line breaks, as well as colons as path separators, and you need '-e' as the first argument passed to osascript...

Hope that helps...
Or you could use Cocoa's built in AppleScript classes, instead of using NSTask and osascript
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 11, 2003, 12:17 AM
 
Originally posted by Brass:
Or you could use Cocoa's built in AppleScript classes, instead of using NSTask and osascript
Doh! Who knew?

well, here's what the code would look like in that case:

- (void)changeDesktopPicture
{
NSDictionary *errors;
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\"\n set the desktop picture to \"Users:foobar:folder:newPic.jpg\"\n end tell"];
[script executeAndReturnError:&errors];
[script release];
}

Thanks for the heads up, Brass!
     
Wafflestomper  (op)
Fresh-Faced Recruit
Join Date: Jun 2003
Location: Charlottesville, VA
Status: Offline
Reply With Quote
Jul 11, 2003, 12:59 AM
 
thanks a bunch you guys...that's perfect. I was looking forever trying to figure out how to do that with cocoa, just plain overlooking applescript. And who knew there was an applescript class in cocoa.

as a sidenote, is applescript the best way to interact with the finder for this type of thing, or are there some cocoa classes that do it too? Just out of curiosity.

Thanks again disco and brass
( Last edited by Wafflestomper; Jul 11, 2003 at 01:05 AM. )
     
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Jul 11, 2003, 01:08 AM
 
as a sidenote, applescript the best way to interact with the finder for this type of thing, or are there some cocoa classes that do it too? Just out of curiosity.
If you're just interested in changing the desktop pic, Apple has some sample code with a different approach here: http://developer.apple.com/samplecod...ppDockMenu.htm. It's a Cocoa app, but the actual function to change the desktop picture is a LOT of Apple Event and Carbon calls, which I'm not familiar with.

If you're interested in interacting with the filesystem (ie, moving and copying files/folders) as well, then take a look at NSFileManager in the Foundation API.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:30 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,