Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'My version...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
My version...
Authored by: porkchop_d_clown on Feb 06, '02 11:48:40AM

First, I want to thank the author of the original script, it intrigued me enough to get off my butt and learn some applescript (I'm a UNIX programmer by day).

With that said, I think you will find this version shorter and considerably faster than the original - first, because it doesn't need to make repeated calls to the shell, second because it uses math rather than string manipulation to round the results.

Please tell me what you think!



set MaxProcs to 5 set dstring to "OS X Virtual Memory Statistics:" & return set dstring to dstring & get_swap_info() set dstring to dstring & return & return & "Top " & MaxProcs & " Applications: " set dstring to dstring & get_big_procs(MaxProcs) display dialog dstring buttons {"Ok"} default button 1 -- Parses out the value of one line of output from vm_stat and -- returns it as a string in MB. on vmparse(x, n) -- extract the nth line of the output. set AppleScript's text item delimiters to {"."} set y to text item n of x as string -- Convert the # 4k pages to the nearest megabyte set AppleScript's text item delimiters to {" "} set a to (word -1 of y as integer) + 128 set a to (a - (a mod 256)) / 256 as integer return (a as string) & " MB" end vmparse -- Find out swap space stats and returns them as a string. on get_swap_info() -- get info about vm files do shell script "ls -l /var/vm" set r to result set c to number of paragraphs of r -- add up swap file sizes set t to 0 repeat with i from 2 to c set t to t + (word 6 of paragraph i of r) as integer end repeat -- round to the nearest megabyte. set t to t + 524288 set t to (t - (t mod 1048576)) / 1048576 as integer set o to " Total Swap: " & t & " MB in " & c - 1 & " files" do shell script "vm_stat" set r to result set o to o & return & " Used Swap: " & vmparse(r, 10) set o to o & return & " Free RAM : " & vmparse(r, 1) as string return o end get_swap_info


[ Reply to This | # ]
rest of the script
Authored by: porkchop_d_clown on Feb 06, '02 11:51:39AM

The comment form truncated the script. Here's the last part:

-- Returns a list of the largest num applications on get_big_procs(num) do shell script "ps auxmww" set r to paragraphs 2 through (num + 2) of result as list set o to "" repeat with c from 1 to num set e to item c of r -- get size rounded to nearest MB set vsz to ((word 6 of e) as integer) + 512 set vsz to ((vsz - (vsz mod 1024)) / 1024) as integer set o to o & return & " " & vsz & " MB " -- extract the process name set AppleScript's text item delimiters to {""} set ao to offset of "/" in e set a to (text items ao through -1 of e) as string -- if the name contains ".app", truncate the name there. set ao to offset of ".app" in a if (ao is not 0) then set a to (text items 1 through (ao - 1)) of a as string -- extract the executable name from the process name. set AppleScript's text item delimiters to {"/"} set a to text item -1 of a set o to o & a end repeat return o end get_big_procs


[ Reply to This | # ]
Max No of App Details?
Authored by: wuf810 on Feb 06, '02 07:12:23PM

Is there anyway to increase the number of Top "n" apps? Even though I modified the variable used for the loop, it appears (for me at least) that the maximum is 7. Any ideas?



[ Reply to This | # ]
Max No of App Details?
Authored by: porkchop_d_clown on Feb 06, '02 10:44:42PM

I think the problem is that there is some upper limit to the # of characters you can send to a dialog. Try this: replace the top portion of the script (everything above 'on vmparse(x,n)') with this code:


-- Change this to change the # of applications displayed.
set MaxProcs to 10

if (MaxProcs is less than or equal to 5) then
set dstring to "OS X Virtual Memory Statistics:" & return
set dstring to dstring & get_swap_info()
set dstring to dstring & return & return & "Top " & MaxProcs & " Applications: "
set dstring to dstring & get_big_procs(MaxProcs)
display dialog dstring buttons {"Ok"} default button 1
else
display dialog "OS X Virtual Memory Statistics:" & return & get_swap_info() buttons {"More Info", "Done"} default button 1

if (button returned of the result is "More Info") then
display dialog "Top " & MaxProcs & " Applications: " & get_big_procs(MaxProcs) buttons {"Done"} default button 1
end if
end if

This fragment will display one dialog if you ask for 5 or fewer apps, and two dialogs if you want more than 5. I'm also working on a version that handles UNIX command line arguments correctly.



[ Reply to This | # ]
Max No of App Details?
Authored by: petey on Feb 07, '02 02:55:34AM

rather than fragment into 2 dialogs, get rid of the text section headers. that gives room for a few more apps. using my layout, i can fit 9 - 11 depending on length of the app names.

and Porkchop,

i like your code much better than mine, but i like my display layout much better than yours. keep it sparse and easy to read at a glance. and keep apps under 10MB left aligned.

now that you've learned AS, will you please learn AS Studio? all i want for Christmas is to have this info always sitting in a window on the desktop. i'm limping along with 384MB, which really isn't enough for this OS, so i need to do manual memory management. actually, all i want for Christmas is for 512MB laptop RAM modules to get cheap.



[ Reply to This | # ]
Max No of App Details?
Authored by: porkchop_d_clown on Feb 08, '02 10:53:35AM

heh. No sympathy from me - I'm using a 256mb iBook.

Believe it or not, I was downloading the developer tools when I read your message last night. Stayed up till 3 playing with it. (No, I have no life whatsoever. :-P) I've got something that's so-so about doing what you want. I couldn't get it to refresh at a fixed interval, AppleScript has an "on idle" event but doesn't seem to have an "on timeout" or "on delay" event.

So, the result is that (on my box) it refreshes at odd intervals, every 20-60 seconds or so.

OSXHints doesn't really have a place to submit binaries, so drop me a note to my username at mac.com and I'll reply with an executable for you to play with.



[ Reply to This | # ]
My version...
Authored by: petey on Feb 06, '02 07:01:00PM

much more elegant code. using word & paragraph - who woulda thunk it?

it has some trouble on my machine getting some app names - (it appends the -psn_0_ stuff to the ends of some names)



[ Reply to This | # ]
-psn
Authored by: porkchop_d_clown on Feb 06, '02 10:34:51PM

Yeah - basically those are UNIX command line arguments. I'm not sure how to deal with them because there's no easy way to tell where the application name stops and the arguments begin, when the application name is allowed to have spaces in it. I'm pondering it though; we'll see what I can come up with.



[ Reply to This | # ]
-psn
Authored by: petey on Feb 06, '02 11:33:45PM

just filter out " -psn" the same way you filter out ".app"



[ Reply to This | # ]
-psn
Authored by: mcroft on Feb 06, '02 11:58:37PM

yup, I added this just after the filer for .app

-- if the name contains "-psn", truncate the name there.
set ao to offset of " -psn" in a
if (ao is not 0) then set a to (text items 1 through (ao - 1)) of a as string



[ Reply to This | # ]
-psn
Authored by: porkchop_d_clown on Feb 07, '02 05:25:19PM

Well, I want a general version that will kill any command line args. They almost always have a dash in front, and a space, so I think I have a solution.



[ Reply to This | # ]