Hi,
Is there a way to check for idle time (no user input)?
Edited: I don’t know what subclassing is yet.
Thanks,
kel
Hi,
Is there a way to check for idle time (no user input)?
Edited: I don’t know what subclassing is yet.
Thanks,
kel
I didn’t think there was. I’ll use the unix script way.
Thanks anyway,
kel
Hi Stefan,
Then, someone did manage to get the idle time in ObjC. I wish I knew how to write things in ObjC, but maybe later I can use that. I don’t like to use things I cannot modify. I’m saving the link though and copying the page. I still need to learn how to wrap an ObjC program in Cocoa-AppleScript. It’s too bad they didn’t implement many of the ObjC methods in Cocoa-Applescript. That link was there from Tiger. I think I might need to download more developer things also.
Thanks for the link,
kel
The code works fine in 10.8 “ as well as the shell script works for a long time
Is it hard to wrap it from Cocoa-AppleScript?
In your project, add IOKit.framework in Build Phases > Link Binary With Libraries
Add a new Objective-C class, name it IdleTime
replace the contents of the.h file with
@interface IdleTime : NSObject
+ (NSInteger)systemIdleTime;
@end
replace the contents of the .m file with
#import "IdleTime.h"
#import <IOKit/IOKitLib.h>
@implementation IdleTime
+ (NSInteger)systemIdleTime
{
NSInteger idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = (CFNumberRef) CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}
@end
In the AppleScript script class write
script WhatEverItsNameIs
property parent : class "NSObject"
property IdleTime : class "IdleTime"
-- .
IdleTime's systemIdleTime()
-- .
end script
Hi Stefan,
I was just reading about Objective C programming in Xcode, so I understand some of what you’re writing. I still need to try adding it to the Cocoa-AppleScript project. It doesn’t look that hard to do with your instructions.
Thanks a lot,
kel
I think I’m close to making it work! It’s returning 0 seconds.
Yeah, it worked!! I was making the stupid mistake of placing the delay 5 after the call.
Thanks a lot,
kel
For anybody interested, here’s sort of a template with instructions mainly from StefanK:
-- Instructions:
-- Add IOKit.framework in Build Phases > Link Binary With Libraries
-- Add a new Objective-C class, name it IdleTime
-- Note: to add a new class, go to menu File > New > File...
-- Replace contents of .h file with @interface
-- Replace contents of .m file with Objective C subroutine
-- Add property to appDelegate (script)
--
(* .h file
#import <Foundation/Foundation.h>
@interface IdleTime : NSObject
+ (NSInteger)systemIdleTime;
@end
*)
(* .m file
#import "IdleTime.h"
#import <IOKit/IOKitLib.h>
@implementation IdleTime
+ (NSInteger)systemIdleTime
{
NSInteger idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = (CFNumberRef) CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
}
@end
*)
script AppDelegate
property parent : class "NSObject"
property IdleTime: class "IdleTime"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
delay 5
set sysIdleTime to IdleTime's systemIdleTime()
display dialog (sysIdleTime as string)
return
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Edited: one thing I don’t understand is this line:
I couldn’t find anything on the internet about the “nanoseconds >> 30”.
gl,
kel
I couldn’t find anything on the internet about the “nanoseconds >> 30”.
The result is in nanoseconds, and the >> is a binary shift operator, so it’s shifting the value 30 binary places.
Sub-classing: taking a standard class and adding or changing what it does, making your own custom version that has the parent class’s abilities, and also those of your own.
For example, there is NSTableView class, and by itself it displays table views in the standard way. You can make your own class “MyNewTableView”, and to it you add a function that beeps when you call a -(void)beep method. Also, you want to make it do something different than normal when you double-click a row. NSTableView already has a method called “doubleAction”. So you re-write the method called “doubleAction” (to show an alert and play a beep or something). So you’ve taken the built-in method and made it do something different than what it normally does.
I think this is about as simple an explanation I can make.
In ObjC, you make a lot of subclasses based off the main class “NSObject”, which is the parent class for almost everything. In essence, most things are subclasses.
Hi Shane,
Ok, thanks. I need to read about Objective C math.
Thanks a lot,
Hi SuperMacGuy,
I’ve been reading on creating classes and subclassing.
Thanks a lot,
kel
Actually, that’s C math.
Hi Shane,
I’ve read that Objective-C uses C’s math. Trying to find it at the Developer Library ro on this computer in math.h.
Thanks,
kel
Objective-C is a superset of C – it’s C plus some other stuff. The maths stuff with primitive types is all done at the C level. Google for C operators and you’ll find more than you want to know.
Ok, found the good stuff.
Thanks for giving me the good search terms.
Actually they’re not called binary operations but they are called bitwise operations which makes them easy to find them with Google or an very explanation on Wikipedia.
And wikipeadia says:
“The bit shifts are sometimes considered bitwise operations, because they operate on the binary representation of an integer instead of its numerical value; however, the bit shifts do not operate on pairs of corresponding bits, and therefore cannot properly be called bit-wise.”
What’s in a name…