• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Help using NSAppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help using NSAppleScript


  • Subject: Re: Help using NSAppleScript
  • From: Rob Keniger <email@hidden>
  • Date: Tue, 30 Dec 2008 16:16:02 +1000


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:
This email sent to email@hidden


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

  • Prev by Date: Re: Saving unseen NSView with subviews to NSImage
  • Next by Date: Re: Saving unseen NSView with subviews to NSImage
  • Previous by thread: Help using NSAppleScript
  • Next by thread: Re: Help using NSAppleScript
  • Index(es):
    • Date
    • Thread