anyone want to help me with a program?

Discussion in 'Mac Programming' started by twoodcc, Apr 11, 2007.

  1. macrumors P6


    ok, i want to make a mac program that keeps track of your computer's uptime, keep a record uptime, and maybe average uptime and more statistics if possible.

    i have taken a few programming courses, but only matlab, visual basic, and C#, so they don't really help me with this one.

    i have gone through a few mac tutorials, but they weren't like the program i want.

    i know there is a widget called "show-off" that basically does this, but i have computers on panther, and i just want to make my own program

    if anyone could help me get started, i'd really appreciate it. i don't really know how to start this

    thanks in advance
  2. macrumors G4


    Have you had a look at one of the Cocoa books like the hiligass one?
  3. macrumors P6


    i don't think i have. do you have a link to it? all i've done with Cocoa is a few tutorials
  4. macrumors G4



    Amazon (US)
  5. macrumors 68040


    The easiest way to do it is with an AppleScriptStudio application.

    inside of the idle handler say

    set timewords to every word of (do shell script "uptime")
    set timewords to every word of (do shell script "uptime")
    set uptime to (item 4 of timewords) * days + (item 6 of timewords) * hours + (item 7 of timewords) * minutes
  6. macrumors G4


    A simple obj-C application will use less CPU resource than applescript, but assuming you have a modern Mac (fast G4/G5/Intel) you'll be OK, from experience an applescript application (on an iMac G5) uses about 0.3% CPU while sleeping.
  7. macrumors P6


    thanks for the link. have you used this book before yourself?

    thanks. so if i copy and paste this into an AppleScriptStudio application, it will work? or do i need to change it some?

    well i would be running this on a G4 mac any help in speed would be nice
  8. macrumors G4


    Yep, it's excellent.
  9. macrumors P6


    thanks, i'll check it out
  10. macrumors 68040


    The code I gave will generate the uptime as seconds but won't do anything useful with it. You will need code to store it some place and calculate useful information with it.

    A straight cocoa solution would be much more efficient but not easier.
  11. macrumors P6


    ok thanks. i'll see what i can come up with
  12. macrumors P6


    well i got it working. but it doesn't do anything yet. what do you think would be the best way to display the uptime on the screen?
  13. macrumors 68040


    Have a text field that you update.
  14. macrumors P6


    thanks. i'm not really sure how to do that, but i'll give it a try
  15. macrumors P6


    well i'm gonna be honest here, i've tried to do this, but i'm not really sure how to go about it. could you help me out with it?

    thanks in advance
  16. macrumors 68040


    I was away at WWDC and not watching the forums.

    Did you end up with the Cocoa solution or the AS solution.

    For AS
    set contents of text field "Foo" to timewords

    The text field has to be named "Foo" in Applescript in Interface Builder for this to work.

    For Cocoa
    [myTextField setStringValue:timeWords];

    The text field has to be connected to an IBOutlet on the class you are working on.
  17. macrumors P6


    thanks. i'm trying the AS version.

    i did what you said, but nothing is showing up in the text box. i set it to uneditable. is that a problem? i don't want to be able to type in the box
  18. macrumors G4


    Uneditable is fine, you have to make sure the box is being pointed too correctly from the code.
  19. macrumors P6


    i guess i'm having trouble with that.
  20. macrumors P6


    well i need some help here. first, i tried to use the code that was given to me in this thread in applescript studio, but i couldn't figure out how to put it in a textbox.

    the last 2 nights i've been working with a few other members on a cocoa application that does the same thing, and none of us can figure out how to get the uptime. we tried this, but it doesn't work right:

    double newVar = currentTimeAsDuration;
    	NSString *Astring = [NSString stringWithFormat:@"%f", newVar]; 	
    	double value = [Astring intValue]; 
    	double inSeconds = value * .0001; //mili-sec
    	double inMinutes = inSeconds / 60; //sec-min
    	double inHours = inMinutes / 60; //min-hour
    	NSString *string = [NSString stringWithFormat:@"%f", inHours];
    	NSString *hoursExtract = [string substringToIndex:1];
    	NSString *minutesExtract = [string substringWithRange:NSMakeRange(2,2)];
    	NSString *secondsExtract = [string substringWithRange:NSMakeRange(4,2)];
    	NSString *finalString = [NSString stringWithFormat:@"%@h %@m %@s", hoursExtract, minutesExtract, secondsExtract];
    	[textField setStringValue:finalString]
    so if anyone can help, that would be great.

  21. macrumors P6


    well me and rev316 got it to display your uptime correctly.

    now the next step is to keep a record of your best (longest) uptime.

    can anyone help us out with that? storing data is a little over my head right now

  22. macrumors 6502

    Could you post your whole Xcode project here? I can't really see from the snippet above what is going on...

    Also, do you only want to store the longest uptime, or do you want more detailed stats?
  23. macrumors P6


    here is the updated code:

    #import "uptimeApp.h"
    @implementation uptimeApp
    - (IBAction)createTime:(id)sender
    	AbsoluteTime currTime; 
    	currTime = UpTime();
        float currentUptime = (float)AbsoluteToDuration(currTime);
    	if (0.0f > currentUptime) // negative means microseconds
    	currentUptime /= -1000000.0f;
        else // positive means milliseconds
    	currentUptime /= 1000.0f;
        int days = 0;
        float currentUptimeinMinutes = currentUptime / 60;
       	int hours = (int)currentUptimeinMinutes / 60; //get hours	
      	int diffHinMin = hours * 60; //find the minutes of the hours	
      	int minutes = currentUptimeinMinutes - diffHinMin; //difference to get minutes
        if (minutes > 59)
            hours += 1;
            minutes = 0;
      	if (hours > 23)
            days = hours / 24;
            hours = hours % 24;
        NSString *string = [NSString stringWithFormat:@"%dd %dh %dm", days, hours, minutes];
    	[textField setStringValue:string];  // display it in our outlet
    thanks. well, eventually we would like to hold more stats if possible, like your last 10 uptimes or something.
  24. macrumors 68040


    Take a look at NSUserDefaults. Very easy to use.

    defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults floatForKey:@"MaxUptime"]< currentUptime) 
        [defaults setFloat: currentUptime ForKey:@"MaxUptime"];
  25. macrumors 6502

    Here's my solution. I've changed the name of your class to AppController, because it makes more sense (and starts with a capital letter!).


    #import <Cocoa/Cocoa.h>
    @interface AppController : NSObject
    	IBOutlet NSTextField *textField;
    - (IBAction)createTime:(id)sender;
    - (NSNumber *)longestRecordedUpTime;
    - (NSArray *)lastTenRecordedUpTimes;
    #import "AppController.h"
    NSString *MBLongestRecordedUpTime = @"MBLongestRecordedUpTime";
    NSString *MBLastTenRecordedUpTimes = @"MBLastTenRecordedUpTimes";
    @interface NSNumber (MBUpTimeAdditions)
    + (id)currentUpTime;
    - (NSString *)formattedUpTimeString;
    @implementation AppController
    - (IBAction)createTime:(id)sender;
    	NSNumber *currentUpTime = [NSNumber currentUpTime];
    	[textField setStringValue:[currentUpTime formattedUpTimeString]];
    	if ([[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime] == nil || [currentUpTime compare:[[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime]] == NSOrderedDescending)
    		[[NSUserDefaults standardUserDefaults] setObject:currentUpTime forKey:MBLongestRecordedUpTime];
    	NSMutableArray *lastTenUpTimes = [[[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes] mutableCopy];
    	if (lastTenUpTimes == nil)
    		lastTenUpTimes = [[NSMutableArray alloc] initWithCapacity:10];
    	[lastTenUpTimes insertObject:currentUpTime atIndex:0];
    	if ([lastTenUpTimes count] > 10)
    		[lastTenUpTimes removeLastObject];
    	[[NSUserDefaults standardUserDefaults] setObject:lastTenUpTimes forKey:MBLastTenRecordedUpTimes];
    	[lastTenUpTimes release];
    - (NSNumber *)longestRecordedUpTime;
    	return [[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime];
    - (NSArray *)lastTenRecordedUpTimes;
    	return [[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes];
    @implementation NSNumber (MBUpTimeAdditions)
    + (id)currentUpTime;
    	return [self numberWithUnsignedLongLong:UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime())) / 1000000000];	// UpTime in seconds
    - (NSString *)formattedUpTimeString;
    	unsigned days = [self unsignedLongLongValue] / (60 * 60 * 24);
    	unsigned hours = ([self unsignedLongLongValue] / (60 * 60)) % 24;
    	unsigned minutes = ([self unsignedLongLongValue] / 60) % 60;
    	return [NSString stringWithFormat:@"%ud %uh %um", days, hours, minutes];

Share This Page