AsObjC: CPU usage monitoring example

Here is CPU usage monitoring example, but the same approach may be used for monitoring other system info.

Save and run as Stay-Open Application:


-- CPU usage monitoring
-- written by me, right now

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property wController : false
property aView : missing value

set {aWidth, aHeight} to {300, 120}
set notificationTitle to "CPU usage:"
set notificationText to ""

set notificationText to current application's NSString's stringWithString:notificationText
set paramObj to {aWidth, aHeight, notificationTitle, notificationText}
my performSelectorOnMainThread:"displayNotification:" withObject:(paramObj) waitUntilDone:true



----------------------------------------- HANDLERS ------------------------------------------------

on idle
	set cpuUsageInfo to my cpuUsageInfoRecord()
	set notificationText to "
    USER:    " & user of cpuUsageInfo & " %
    SYSTEM:  " & sys of cpuUsageInfo & " %
    IDLE:    " & |idle| of cpuUsageInfo & " %"
	aView's setString:notificationText
	return 1
end idle

on quit
	my wController's |close|()
	continue quit
end quit

on displayNotification:paramObj
	copy paramObj to {aWidth, aHeight, aTitle, notificationText}
	set aColor to current application's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.9
	set aView to current application's NSTextView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
	aView's setRichText:true
	aView's useAllLigatures:true
	aView's setTextColor:(current application's NSColor's cyanColor()) --
	aView's setBackgroundColor:aColor
	aView's setEditable:true
	aView's setFont:(current application's NSFont's fontWithName:"Menlo" |size|:20)
	set aWin to makeWinWithView(aView, aWidth, aHeight, aTitle, 0.9)
	aView's setString:notificationText
	set my wController to current application's NSWindowController's alloc()
	my (wController's initWithWindow:aWin)
	my (wController's showWindow:me)
end displayNotification:

on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
	set aScreen to current application's NSScreen's mainScreen()
	set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
	set aBacking to current application's NSTitledWindowMask
	set aDefer to current application's NSBackingStoreBuffered
	set aWin to current application's NSWindow's alloc()
	(aWin's initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
	aWin's setTitle:aTitle
	aWin's setDelegate:me
	aWin's setDisplaysWhenScreenProfileChanges:true
	aWin's setHasShadow:true
	aWin's setIgnoresMouseEvents:false
	aWin's setLevel:(current application's NSNormalWindowLevel)
	aWin's setOpaque:false
	aWin's setAlphaValue:alphaV --append
	aWin's setReleasedWhenClosed:true
	aWin's |center|()
	aWin's setContentView:aView
	return aWin
end makeWinWithView

on cpuUsageInfoRecord()
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {": ", ", ", "% "}}
	set cpuUsageInfo to text items of (do shell script "top -l 1 | grep 'CPU usage'")
	set AppleScript's text item delimiters to ATID
	tell cpuUsageInfo to return {user:item 2 as real, sys:item 4 as real, |idle|:item 6 as real}
end cpuUsageInfoRecord