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 > A couple of diverse questions

A couple of diverse questions
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 23, 2003, 10:40 AM
 
I have a few different questions that I need help with to complete my project.

1) How do I interact with the command line through Cocoa? I would like to be able to control a separate command-line app through my Cocoa GUI app. I basically want to start/stop the cli app using GUI controls in my Cocoa app. I think I need to be able to determine if it is running, get the PID and then be able to send a kill command to it.

2) Is there a way to determine if a powerbook is running on battery power?

3) How does one implement those nifty little flip down triangle gui elements that then shift everything down in a window and insert another ui element just below the triangle. I'm thinking of the triangles in the Finder list view, but I've seen them in other apps to show/hide a UI element.

4) Is there an easy way to grab the last 2-3 lines of a text file using NSScanner? The file is really long, but the pertinent information is always in the last 2-3 lines so I don't want to have to scan through the whole thing. There's lots of repeating information so scanning through and rejecting the useless info until I get to the end takes along time. It would be much more efficient to just scan the last few lines.

thanks,
kman
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 23, 2003, 02:22 PM
 
1) You'll need to use BSD syscalls to look for processes. A good place to start would be to check out the source for the ps command from Darwin.

2) See the documentation for the Power Manager.

3) There's no special trick to it, really. All you need is a button with the triangle as its image. In its action method, call -setFrame: on all the views below the button to move them down to make room for the view you're adding, add the new view (call -addSubview on the window's content view), and call -setFrame:display:animate: on the window if you want the window to slide down like Finder's info panel.

4) You can use -setScanLocation: to jump ahead to an arbitrary point in the file, and start scanning from there. You could also split the file into lines (via -[NSString componentsSeparatedByString:] with @"\n" as the argument), and work with individual lines as array elements.
Rick Roe
icons.cx | weblog
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 23, 2003, 03:08 PM
 
Originally posted by Rickster:
1) You'll need to use BSD syscalls to look for processes. A good place to start would be to check out the source for the ps command from Darwin.
Actually I'm pretty sure there's an example of this in a technote somewher, but I can't be bothered to go looking for it
     
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
May 23, 2003, 03:25 PM
 
Ya NSScanner really needs to be beefed up. Word by word, sentence by sentence, and backwards scanning would be nice for starters.
you are not your signature
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 23, 2003, 03:32 PM
 
For 1, it sounds like you could just use an NSTask. No need for PIDs or signals that way.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 23, 2003, 06:08 PM
 
Originally posted by Gametes:
Ya NSScanner really needs to be beefed up. Word by word, sentence by sentence, and backwards scanning would be nice for starters.
Omni has some nice additions and their own character scanner, you could add the other functionality you're looking for as a category without much fuss.
     
Addicted to MacNN
Join Date: Nov 2002
Location: Seattle, WA
Status: Offline
Reply With Quote
May 23, 2003, 07:26 PM
 
I believe NSScanner just uses NSString's rangeOfString method (at least, that was always in the backtrace when I had array overflows that caused NSScanner to crash). And rangeOfString:options: has NSBackwardsSearch so you can start at the end of your document
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 25, 2003, 08:14 PM
 
Originally posted by kman42:
I have a few different questions that I need help with to complete my project.

1) How do I interact with the command line through Cocoa? I would like to be able to control a separate command-line app through my Cocoa GUI app. I basically want to start/stop the cli app using GUI controls in my Cocoa app. I think I need to be able to determine if it is running, get the PID and then be able to send a kill command to it.

2) Is there a way to determine if a powerbook is running on battery power?

3) How does one implement those nifty little flip down triangle gui elements that then shift everything down in a window and insert another ui element just below the triangle. I'm thinking of the triangles in the Finder list view, but I've seen them in other apps to show/hide a UI element.

4) Is there an easy way to grab the last 2-3 lines of a text file using NSScanner? The file is really long, but the pertinent information is always in the last 2-3 lines so I don't want to have to scan through the whole thing. There's lots of repeating information so scanning through and rejecting the useless info until I get to the end takes along time. It would be much more efficient to just scan the last few lines.

thanks,
kman
1. You can use NSTask, but it may not be enough for you. Check it's doumentation. It basically allows you to run CLI tools.

3. NSOutlineView does the disclosure triangles all automatically. Very little additional code of your own required. Again, check it's documentation.
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
May 28, 2003, 03:59 PM
 
Originally posted by Rickster:

3) There's no special trick to it, really. All you need is a button with the triangle as its image. In its action method, call -setFrame: on all the views below the button to move them down to make room for the view you're adding, add the new view (call -addSubview on the window's content view), and call -setFrame:display:animate: on the window if you want the window to slide down like Finder's info panel.
Okay, I've been trying to work this out. I have an NSTableView that is placed in my window in interface builder. I want to get rid of the tableView in the awakeFromNib and then have it appear/desappear later from a button action. I tried removeFromSuperview: , but it just removed the tableView and not the scrollView (as you would expect, I guess). So I tried to do removeFromSuperview on the superview of the table:

[[tableView superview] removeFromSuperView];

but I got this error:

removeFromSuperview called for NSScrollView contentView. Use setContentView: instead !!

Now I'm not really sure how to proceed. Any help?

thanks,
kman
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
May 28, 2003, 06:41 PM
 
See the documentation for NSScrollView -- the view hierarchy is deeper than you think.
Rick Roe
icons.cx | weblog
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Jun 1, 2003, 11:54 AM
 
Originally posted by Rickster:
See the documentation for NSScrollView -- the view hierarchy is deeper than you think.
So, I'm still not quite getting it. Here's my understanding of the hierarchy:

NSScrollView-->content view-->document view

My NSTableView is the document view. My thinking is that I need to put the content view and the scrool view into instance variables and retain them so that I can get at them later and then remove them. Here's what I'm doing to remove the views in awakeFromNib:

Code:
[discoveredServicesList retain]; myContentView=[[discoveredServicesList superview] retain]; myScrollView=[[myContentView superview] retain]; [myScrollView removeFromSuperview];
And here's the code for toggling the views:

Code:
-(IBAction)toggleRendezvousActivation:(id)sender { switch ( [sender state] ) { case NSOnState: [[mainWindow contentView] addSubview:myScrollView]; [myScrollView addSubView:myContentView]; [myContentView addSubview:discoveredServicesList]; [self setupService]; [self setupBrowser]; break; case NSOffState: [discoveredServicesList retain]; myContentView=[[discoveredServicesList superview] retain]; myScrollView=[[myContentView superview] retain]; [myScrollView removeFromSuperview]; [self stopService]; [serviceBrowser stop]; [domainBrowser stop]; break; } }
When I toggle the views back on, I can't get anything to display in the NSTableView even though the columns appear in the appropriate place on the window.

Am I still missing a view in the hierarchy? Is my approach fundamentally flawed?


I also have the problem that when I remove the scrollview (which is the bottom-most view) and try to resize the window, I end up cropping off the top of the view where all of the other subviews are. I can shrink the window and the window's content view, but I am left with the empty space where the tableview used to be rather than the space at the top where all of the other views are. I know it has to do with the content view being drawn from the lower-left, but I thought it would move all of the subviews down and remove the empty space rather than preserving the empty space and cropping out the subviews.

Here's what it looks like after resizing:


I think you can see how the subviews are just cropped off now.

Code:
largeMainWindowFrame=[mainWindow frame]; smallMainWindowFrame.origin.x=largeMainWindowFrame.origin.x; smallMainWindowFrame.origin.y=largeMainWindowFrame.origin.y+largeMainWindowFrame.size.height/2; smallMainWindowFrame.size.width=largeMainWindowFrame.size.width; smallMainWindowFrame.size.height=largeMainWindowFrame.size.height/2; NSSize smallMainWindowSize=smallMainWindowFrame.size; [discoveredServicesList retain]; myContentView=[[discoveredServicesList superview] retain]; myScrollView=[[myContentView superview] retain]; [myScrollView removeFromSuperview]; [mainWindow setContentSize:smallMainWindowSize]; [[mainWindow contentView] setNeedsDisplay: YES]; [mainWindow setFrame:smallMainWindowFrame display:YES animate:YES];

thanks,
kman
(Last edited by kman42; Jun 1, 2003 at 12:48 PM. )
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Jun 1, 2003, 11:15 PM
 
I basically have this working, but there is still one problem. Here's what I did and the remaining problem:

I created two views: one containing all of the user interface elements that are always present (smallView) and one that contains the NSTableView (rendezvousView). In awakeFromNib I set the size of the window's content view to the size of smallView and then do [[mainWindow contentView] addSubview:smallView]. This works great.

When I want to include the other subview, I add rendezvousView with [[mainWindow contentView] addSubview:rendezvousView positioned:NSWindowBelow relativeTo:smallView]. I then change the size of the window and animate it. The problem is that rendezvousView gets added on top of small view, not above, but on top of. I've tried changing the size of the window before adding rendezvousView, but that does the same thing. In addition, both views slide down as the window enlarges leaving a large bit of space at the top. Why isn't this method putting rendezvousView below smallView?

kman
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Jun 2, 2003, 08:45 PM
 
Well, I'm getting better. I can now move the views around approximately where I want them. My biggest problem now is that I can't get the views to be 'revealed' when the window animates to a bigger size as is done in the Finder info window. Instead, the views slide down with the window. I tried adding the views to the window content view before animating the window, but that didn't seem to help. Any other ideas?

kman
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Jun 3, 2003, 01:33 PM
 
As with most things in Cocoa, this is really easy once you learn how to do it. Just subclass NSView and override isFlipped to return YES. Make this view the contentView for your window and put ALL your other views in it, even the ones that should be hidden when the window is small. They will draw normally (from the bottom/left), but the contentView will draw from the top/left. Then you can just resize the window and the hidden objects will be revealed. Beautiful!

kman
     
   
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 03:35 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