System Info you probably don't need to know!

First, it is possible to find out how much user memory is available on a given machine, and to find out if there’s a lot of swapping going on:

set UsrMem to ((do shell script "/usr/sbin/sysctl -n hw.usermem") / 1048576) as integer
set SwapUse to word 6 of (do shell script "/usr/sbin/sysctl -n vm.swapusage")
display dialog "The available user memory is " & UsrMem & "MB," & return & "and the swap space now in use is " & SwapUse & "B"

Something else of some interest is how long you’ve been running since the last start or restart. This requires an osax otherwise very useful in timing scripts called GetMilliSec 1.0.1 to be found in osaxen.com (part of MacScripter). It can be downloaded from this link and put in your ~/Library/ScriptingAdditions/ folder

The script is a slight modification of one that appeared in the forums (but I can’t recall who it was, unfortunately). It goes like this (Using Kai’s method of getting an icon):

set theIcon to ((path to application "iCal" as Unicode text) & "Contents:Resources:App.icns") as alias
set t to (GetMilliSec) / 1000.0
set hh to t div 3600.0
set t to t - hh * 3600.0
set mm to t div 60.0
set t to t - mm * 60.0
set ss to t as integer
display dialog "It has been " & hh & " Hrs, " & mm & " Min, " & ss & " Secs" & return & "since last system start or restart on" & return & (current date) - (GetMilliSec) / 1000.0 with icon theIcon

Note that this causes iCal to launch.

Yeah - if you’re just trying to borrow the app’s icon, and it’s not already open, that could be a bit of a pain.

This might be a better approach here:

tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
tell (GetMilliSec) div 1000 to set {d, h, m, s} to {(my (current date)) - it, it div hours, it mod hours div minutes, it mod minutes}
display dialog "Last system start/restart: " & return & d & return & return & "Total running time: " & ¬
	return & h & " hours, " & m & " minutes, " & s & " seconds" with icon i

Using shell script: :slight_smile:

tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
tell (do shell script "last reboot") to set b to (my date (text 37 thru 46)) + (time of my date (text 48 thru 52)) -- doesn't seem to like not having a year
tell (current date) - b to set {d, h, m} to {it div days, it mod days div hours, it mod hours div minutes}
display dialog "Last system boot:" & return & b & return & return & ¬
	"Total running time:" & return & d & " days, " & h & " hours, " & m & " minutes" with icon i

Thank you both for very different but significant improvements. I particularly like the tell GetMilliSec bit personally, though I’m not sure that mine isn’t more transparent to folks not used to “bundling” their commands. The parentheses around GetMilliSec are interesting too - the compiler puts them there for no reason I can see.

I didn’t know that QDenzel’s “last reboot” call existed, but I had played around at length with {do shell script “/usr/sbin/sysctl -n kern.boottime”} without success. Although it’s supposed to be about boottime in microseconds, no amount of manipulation with the current date and kern.boottime would produce the same (and correct result) for the last boot date as GetMilliSec. I know that one is correct because I specifically rebooted the machine to test it. With some searching, I found out that kern.boottime may be off by a factor of two, but clearly there’s also a constant in the number, and I couldn’t find out what it was. Enjoyed myself, but never got there. I did find this which returns the correct time, but don’t know awk, so don’t know what it’s doing:

do shell script "/usr/sbin/sysctl kern.boottime | awk '{ print $6 }' "

Thank you Bruce for pointing out that iCal would launch (and for quietly fixing one of my links) - I didn’t notice because iCal was running on my machine when I tested - when I owned a machine with 896 MB of memory, I was less apt to leave things running than I am now with 2 GB.

Kai’s approach to getting an icon is the way to go - making the Finder get the icon from com.apple.iCal instead of the Application opening to get it from its own resources and handing it over (actually didn’t occur to me that the app would launch for that - it’s just a file in a bundle).

Hi Adam,
Having no real knowledge of the terminal, I just went looking too. My first choice was to look at the log files in /var/log/, where I discovered it in wtmp. When trying to see if anything in the terminal could read it and display the boot time, I came across last reboot.

sysctl looks promising though, although you sounded unsure about its accuracy:

The value returned by the script below appears to be the number of seconds since the first of January, 1970:

do shell script "/usr/sbin/sysctl -n kern.boottime"

But getting the exact difference (between yours and mine), the time of the starting date looks to be 10:00 AM, not 12:00 AM as I would expect: (Well, at least for me. What do you get?)

tell (do shell script "last reboot") to set b to (my date (text 37 thru 46)) + (time of my date (text 48 thru 52))
b - (do shell script "/usr/sbin/sysctl -n kern.boottime")

--> My result is date "Thursday, 1 January 1970 10:00:25 AM"

So ignoring that little peculiarity, I get this using kai’s version:

tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
set b to (date "Thursday, 1 January 1970 10:00:00 AM") + (do shell script "/usr/sbin/sysctl -n kern.boottime")
tell (current date) - b to set {d, h, m, s} to {it div days, it mod days div hours, it mod hours div minutes, it mod minutes}
display dialog "Last system boot:" & return & b & return & return & ¬
	"Total running time:" & return & d & " days, " & h & " hours, " & m & " minutes, " & s & " seconds" with icon i

You said: “My result is date “Thursday, 1 January 1970 10:00:25 AM””
Mine is: “Wednesday, December 31, 1969 8:59:52 PM”

So ignoring the seconds, yours is 12 hours after mine. Shows up again in your last script. For me the correct answer (returned by the GetMilliSec version) is Wednesday, April 5, 2006 2:09:47 (according to my clock it was 2:10 - close enough). Your version returns Thursday, April 6, 2006, 3:10:08 AM. I’m running at GMT -3 which is Atlantic Daylight Time and that may account for the hour, but the 12 hours later is definitely there.

Okay, I’ve got it now, thanks to you. Thought the time thing was a little odd.
Try this:

tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
set b to (date "Thursday, 1 January 1970 12:00:00 AM") + (do shell script "/usr/sbin/sysctl -n kern.boottime") + (time to GMT)  -- added + (time to GMT)
tell (current date) - b to set {d, h, m, s} to {it div days, it mod days div hours, it mod hours div minutes, it mod minutes}
display dialog "Last system boot:" & return & b & return & return & ¬
	"Total running time:" & return & d & " days, " & h & " hours, " & m & " minutes, " & s & " seconds" with icon i

And the count-from date script should be:

tell (do shell script "last reboot") to set b to (my date (text 37 thru 46)) + (time of my date (text 48 thru 52))
b - (do shell script "/usr/sbin/sysctl -n kern.boottime") - (time to GMT) -- added - (time to GMT)

--> My result is date "Thursday, 1 January 1970 12:00:25 AM"

On balance, I think I prefer the kernel state (sysctl) method. Since no year is specified for the date returned by ‘last reboot’, there’s a potential issue when the year changes - if the last reboot occurs in one year and the script is run in the following year (before a further reboot). I also got an initial error from ‘last reboot’ - although this might have been a glitch on my machine. Following a reboot, it now seems to be behaving itself.

Qwerty’s right about the value returned from sysctl’s -n option (to return only variable values). While time zone is not considered for the start of the Unix epoch, it is for subsequent date calculations - and this difference is fixed by the ‘time to GMT’ adjustment. (I nearly posted something very similar, late last night. At the time, though, bed seemed a far more attractive proposition. Sorry, long day.) :wink:

As far as the parsing of the ‘kern.boottime’ value is concerned, awk ‘{ print $6 }’ returns only the time field. What’s needed is something more like the following (adjusted here to address Adam’s concern about ‘transparency’):

set d to date (do shell script "sysctl kern.boottime | awk '{print $5,$4,$7,$6}'")
tell (current date) - d
	set h to it div hours
	set m to it mod hours div minutes
	set s to it mod minutes
end tell
tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
display dialog "Last start/restart:" & return & d & return & return & "Time elapsed: " & return & h & ¬
	" hours, " & m & " minutes, " & s & " seconds" buttons {"OK"} default button 1 with icon i

For yet another option, it also seems possible to use one or two files to achieve something similar (albeit with minor timing variations due to when certain events occur in the boot-up sequence):

set t to ""
repeat with i in {{"SystemConfiguration:NetworkInterfaces.plist", "start/restart"}, {"com.apple.loginwindow.plist", "login"}}
	set d to modification date of (info for file ((path to preferences folder from local domain as Unicode text) & i's item 1))
	tell (current date) - d to set {h, m, s} to {it div hours, it mod hours div minutes, it mod minutes}
	set t to t & "Last " & i's item 2 & ":" & return & d & return & return & "Time elapsed: " & ¬
		return & h & " hours, " & m & " minutes, " & s & " seconds" & return & return
end repeat
tell application "Finder" to set i to path to resource "App.icns" in bundle application file id "com.apple.iCal" as alias
display dialog t's text 1 thru -3 buttons {"OK"} default button 1 with icon i

GetMilliSec is a command; those don’t divide well at all. :stuck_out_tongue: However, if the command is executed, the resulting number will be very well suited for mathematical use.

Consider another example.

-- OS X v10.4+ only
long user name of (system info)

Why are there parentheses here? System info is command that returns a record (not a class that contains properties).

this doesn’t satisfy me as it doesn’t contain info on last cold boot

do shell script "last reboot"

I prefer using following to get the duration of my current session:

do shell script "uptime"

Adam wrote in this article http://macscripter.net/articles/456_0_10_0_C/ how to make the results look nicer.
As of today it didn’t work on uptimes shorter than a day. With a little modification you’ll have it.

Luke