|
|
change the desktop?
|
|
|
|
Fresh-Faced Recruit
Join Date: Jun 2003
Location: Charlottesville, VA
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
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...
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Jun 2003
Location: Charlottesville, VA
Status:
Offline
|
|
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.
)
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|