Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6888643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:03:08+00:00 2026-05-27T06:03:08+00:00

I want to know the idle time of Mac. Currently I use this code

  • 0

I want to know the idle time of Mac. Currently I use this code

CFMutableDictionaryRef properties = 0;
CFTypeRef obj;
mach_port_t masterPort;
io_iterator_t iter;
io_registry_entry_t curObj;

IOMasterPort(MACH_PORT_NULL, &masterPort);

/* Get IOHIDSystem */
IOServiceGetMatchingServices(masterPort, IOServiceMatching("IOHIDSystem"), &iter);
if (iter == 0)
{
    return -1;
}
else
{
    curObj = IOIteratorNext(iter);
}
if (IORegistryEntryCreateCFProperties(curObj, &properties, kCFAllocatorDefault, 0) == KERN_SUCCESS && properties != NULL)
{
    obj = CFDictionaryGetValue(properties, CFSTR("HIDIdleTime"));
    CFRetain(obj);
}
else
{
    return -1;
}

uint64_t tHandle = 0;
if (obj)
{
    CFTypeID type = CFGetTypeID(obj);

    if (type == CFDataGetTypeID())
    {
        CFDataGetBytes((CFDataRef) obj, CFRangeMake(0, sizeof(tHandle)), (UInt8*) &tHandle);
    }
    else if (type == CFNumberGetTypeID())
    {
        CFNumberGetValue((CFNumberRef)obj, kCFNumberSInt64Type, &tHandle);
    }
    else
    {
        // error
        tHandle = 0;
    }

    CFRelease(obj);

    tHandle /= 1000000; // return as milliseconds
}
else
{
    tHandle = -1;
}

CFRelease((CFTypeRef)properties);
IOObjectRelease(curObj);
IOObjectRelease(iter);
return (double)tHandle;

However, I want the idle time to keep 0 if any video is running.

Is there any code sample or library to check? (include video running on iTunes, VLC, youtube on browser or any other video applications)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:03:09+00:00Added an answer on May 27, 2026 at 6:03 am

    I can interpret your question several ways:

    How to determine when the screensaver kicks in?

    Check the following script (original answer):

    #!/bin/bash
    
    systemSleepTimeMinutes=`pmset -g | grep "^[ ]*sleep" | awk '{ print $2 }'`
    
    if [ $systemSleepTimeMinutes -gt "0" ]; then
        systemSleepTime=`echo "$systemSleepTimeMinutes * 60" | bc`
        devicesIdleTime=`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'`
        secondsBeforeSleep=`echo "$systemSleepTime - $devicesIdleTime" | bc`
        echo "Time before sleep (sec): $secondsBeforeSleep"
        exit 0
    else
        echo "The system is set to not sleep."
        exit 0
    fi
    

    In case the screensaver is suppressed, for example by VLC, the result is:

    The system is set to not sleep.

    Otherwise it returns the time until the screensaver kicks in, for example:

    Time before sleep (sec): 899.88056

    Run the script above through NSTask or NSAppleScript and parse the result.
    Please note “sleep” may also rever to the screensaver, depending on which one kicks in first.

    How to determine wether the screensaver is suppressed?

    The following line does the trick:

    pmset -g | grep "^[ ]*sleep" | awk '{ print $2 }'
    

    In case the screensaver is suppressed it returns 0.
    Run the line above through NSTask or NSAppleScript and parse the result.

    How to suppress the screensaver?

    The official Apple documentation (including code snippet):
    How can I prevent system sleep while my application is running?


    EDIT (Response to comment)

    However, is there a built-in method that works the same as this script?

    I’m not familiar with such a method. I actually doubt it exists.

    Quick NSAppleScript example:

    NSString *source = @"do shell script \"pmset -g | grep \\\"^[ ]*sleep\\\" | awk '{ print $2 }'\"";
    NSAppleScript *script= [[NSAppleScript alloc] initWithSource:source];
    NSDictionary *scriptError = nil;
    NSAppleEventDescriptor *descriptor=[script executeAndReturnError:&scriptError];
    if(scriptError) {
        NSLog(@"Error: %@",scriptError);
    } else {
        if ([[descriptor stringValue] intValue] == 0) {
            NSLog(@"Screensaver suppressed.");
        } else {
            NSLog(@"Screensaver not suppressed.");
        }
    }
    

    Just don’t be reticent about using NSTask or NSAppleScript 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know the difference between : $this->forward(module, action); And $this->redirect(module/action); My first
I want to know what this looks like. I don't have any ideas about
I want to query our servers to see look for disconnected/idle sessions. I know
I know this is a duplicate question. but i want know it with an
I want to know on when was the last time the system was started.
i have one application in that i want to know that is this in
Hi friends i want know sectionIndexTitlesForTableView Action is works i have the following code...
i want to code in python and i know the syntax well.. but i
I am currently dabbling in Magento and I want know how I can create
i want know if is possible, to get a specific element value of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.