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 > Easy Q: How do I get the current screen resolution?

Easy Q: How do I get the current screen resolution?
Thread Tools
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Jun 15, 2002, 04:00 PM
 
I need a simple command-line app that will return the current screen height and width (for main screen only, I guess).

(For the curious, it's to insert into a PHP web page running the VNC Java applet; that way, the page will give the applet the proper dimensions).

I'm comfortable with writing a Carbon "Tool" command-line app if that's the way to go about things. What system call do I use? I've dug around Developer Connection, but only turned up examples using ancient Toolbox calls.

I've also noticed that I could parse the file /Library/Preferences/com.apple.windowserver.plist, which contains keys for Height and Width, but I'm not sure if that's an okay way to go.

Appreciate any pointers
Mithras
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 15, 2002, 06:53 PM
 
I think the normal system call would be: </font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">CFDictionaryRef CGDisplayCurrentMode(CGDirectDisplayID display)</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">It's defined in /System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Headers/CGDirectDisplay.h.
Of course, this is a C framework, so you probably will have to go the tool route if you use that. I'm not really familiar enough with Java to have any idea whether or not it has such a function.

<small>[ 06-16-2002, 04:36 AM: Message edited by: Chuckit ]</small>
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Enthusiast
Join Date: Feb 2002
Location: Boston/Cambridge
Status: Offline
Reply With Quote
Jun 15, 2002, 07:04 PM
 
Do you mean the resolution of the server's screen or the resolution of the screen of the user trying to access the website?
     
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Jun 16, 2002, 03:19 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Chuckit:
<strong>I'm not really familiar enough with Java to have any idea whether or not it has such a function.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">it does:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">import java.awt.*;

public class ScreenRes
{
public static void main (String[] args)
{
Rectangle b = GraphicsEnvironment.getLocalGraphicsEnvironment(). getDefaultScreenDevice().getDefaultConfiguration() .getBounds();
System.out.println(&quot;Width: &quot; + b.width + &quot; / Height: &quot; + b.height);
System.exit(0);
}
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">i've not tested this with a secondary monitor connected, but it should give you the resolution of the main screen.
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Jun 16, 2002, 05:10 AM
 
For anyone who's interested, I thought I'd give a Cocoa/Obj-C example:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">- (void)logScreenResolution
{
NSRect screenFrame;
NSSize screenSize;

screenFrame = [[NSScreen mainScreen] frame];
screenSize = screenFrame.size;

NSLog(@&quot;Width: %f / Height: %f&quot;, screenSize.width, screenSize.height);
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">

<small>[ 06-16-2002, 06:15 AM: Message edited by: Ibson ]</small>
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jun 16, 2002, 07:29 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Ibson:
<strong>For anyone who's interested, I thought I'd give a Cocoa/Obj-C example:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">- (void)logScreenResolution
{
NSRect screenFrame;
NSSize screenSize;

screenFrame = [[NSScreen mainScreen] frame];
screenSize = screenFrame.size;

NSLog(@&quot;Width: %f / Height: %f&quot;, screenSize.width, screenSize.height);
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif"></strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Incidentally, CoreGraphics can make an almost exact equivalent for this method.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">void logScreenResolution(void)
{
CGRect screenFrame;
CGSize screenSize;

screenFrame = CGDisplayBounds(kCGDirectMainDisplay);
screenSize = screenFrame.size;

printf(&quot;Width: %f / Height %f&quot;, screeenSize.width, screenSize.height);
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Just so people can compare. The function I gave originally was much more general and returns a dictionary of all the information about the screen, and you can then retrieve the exact details you're looking for.
(And even more incidentally, you could actually achieve the same effect as the above function in one line. But then the similarities between the two frameworks wouldn't look as eerie. <img border="0" title="" alt="[Wink]" src="wink.gif" /> )

EDIT: Me no can type.

<small>[ 06-16-2002, 08:31 AM: Message edited by: Chuckit ]</small>
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mithras  (op)
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Jun 16, 2002, 11:12 AM
 
Thanks a ton for all the helpful responses!
I went with Chuckit's CoreGraphics tool, since it most closely matched my simple needs; but I played with the Java version too.

Here's a little screenshot of the result (click for the big version):

<a href="http://homepage.mac.com/mybulkmail/screenshots/screeninfo.jpg" target="_blank"> <img src="http://homepage.mac.com/mybulkmail/screenshots/screeninfo-small.jpg" alt=" - " /></a>

That's an Internet Explorer window on one Mac, with resolution 1024x768. In the IE window is the PHP web page, displaying the contents of the server's screen at 800x600. The PHP puts up a nice Aqua-style window with blue background, which is why the picture might look rather confusing at first.

Without y'all's app to get the server's resolution, the applet and fake Aqua window on the web page wouldn't be properly sized.

By the way I'm hoping to release the project to the public within a couple of weeks. I'm still working on the iTunes interface part, and on making it super-easy to generate the self-signed SSL certificate.

For future reference, here's my complete code:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;ApplicationServices/ApplicationServices.h&gt;

void showusage();

int main (int argc, const char * argv[]) {
# check for argument sanity
if (argc != 2)
showusage();

# return screen height or width as requested
if (strcmp(argv[1],&quot;width&quot == 0)
printf(&quot;%d\n&quot;,(int) CGDisplayBounds(kCGDirectMainDisplay).size.width);
else if (strcmp(argv[1],&quot;height&quot == 0)
printf(&quot;%d\n&quot;,(int) CGDisplayBounds(kCGDirectMainDisplay).size.height) ;
else
# complain if passed an incorrect argument
showusage();
return 0;
}

void showusage()
{
printf(&quot;Usage: screeninfo (height | width)\n&quot;
exit(127);
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 01:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2