Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help using NSAppleScript




On 29/12/2008, at 9:04 AM, email@hidden wrote:

I am new to Cocoa/Objective-C and new to the forum, so apologies if this is a newbie questions.

I am trying to use NSAppleScript from Objective-C to execute the following one liner, and covert the result into an NSRect:

"Tell application \"Finder\" to return (bounds of window of desktop)"

When I call the compileAndReturnError method of NSAppleScript it crashes the debugger. I also am not sure how to actually access the result of the NSAppleScript call once I get the script to run.

I would be grateful if someone can offer advice and/or a pointer in the right direction to getting this to work.


I have no idea how you are managing to "crash the debugger" with this code. I also don't understand why you are using AppleScript to get the dimensions of the desktop. You should be using the Cocoa NSScreen class if you want to find out screen dimensions.

Here is one way that you could get the bounds using AppleScript from the Finder using your source. Note that I have done no sanity checking of the NSAppleEventDescriptor that is returned:

NSAppleScript* script=[[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to return (bounds of window of desktop)"];
NSDictionary* scriptError=nil;
NSAppleEventDescriptor* descriptor=[script executeAndReturnError:&scriptError];
if(scriptError)
{
NSLog(@"Error: %@",scriptError);
return;
}


NSRect desktopFrame;
desktopFrame.origin.x=(CGFloat)[[descriptor descriptorAtIndex:1] int32Value];
desktopFrame.origin.y=(CGFloat)[[descriptor descriptorAtIndex:2] int32Value];
desktopFrame.size.width=(CGFloat)[[descriptor descriptorAtIndex:3] int32Value];
desktopFrame.size.height=(CGFloat)[[descriptor descriptorAtIndex:4] int32Value];


//note that this does not return an origin in Cocoa base coordinates for multiple-monitor systems
NSLog(@"Desktop frame: %@",NSStringFromRect(desktopFrame));


Here is how you would find out the size of the desktop using the NSScreen class, which will be much faster and more reliable, as well as returning a result in Cocoa base coordinates:

NSRect desktopFrame=NSZeroRect;
for(NSScreen* screen in [NSScreen screens])
{
	desktopFrame=NSUnionRect(desktopFrame, [screen frame]);
}
NSLog(@"Desktop frame: %@",NSStringFromRect(desktopFrame));


-- Rob Keniger



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Help using NSAppleScript (From: email@hidden)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.