The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   AppleScript to display Safari's memory usage (http://hintsforums.macworld.com/showthread.php?t=64782)

NovaScotian 12-16-2006 02:12 PM

Here's an AppleScript convertor to fix line ends in a saved document.

Code:

set tDoc to (choose file without invisibles)
set NL to ASCII character 10
set DocText to ConvertEnds(read tDoc, NL) -- read and convert
set F to open for access tDoc with write permission
try
        set eof of F to 0 -- erase what was there
        write DocText to F -- replace with converted endings
        close access F
on error
        close access F
end try

to ConvertEnds(txt, LineEnd)
        set tid to text item delimiters
        set text item delimiters to LineEnd
        tell txt's paragraphs to set txt to beginning & ({""} & rest)
        set text item delimiters to tid
        return txt
end ConvertEnds


NovaScotian 12-16-2006 03:52 PM

Further question:

Why doesn't something like this work?

set foo to (read (choose file without invisibles))
set the clipboard to (do shell script "echo " & foo & " | tr \\r \\n")

mark hunte 12-16-2006 04:38 PM

Quote:

Originally Posted by NovaScotian (Post 342469)
Further question:

Why doesn't something like this work?

set foo to (read (choose file without invisibles))
set the clipboard to (do shell script "echo " & foo & " | tr \\r \\n")

Try adding quoted form of. or it will stumble on a lot of files.
Code:

set foo to (read (choose file without invisibles))

set the clipboard to (do shell script "echo " & quoted form of foo & " | tr \\r \\n")


NovaScotian 12-16-2006 05:10 PM

Thanks Mark, that does solve the problem of getting the shell script to work, but the result seems to be converted back by the system because when I check, the endings are still returns.

PS: I've been informed on another list, for the benefit of those who use Script Debugger 4 that there is a File menu pick to save with Unix endings. Would have saved a lot of grief.

mark hunte 12-16-2006 05:41 PM

If I run your script from Post #41 on a plain text file, I get "Mac".

If I combine the two scripts I get "unix"

Code:

set foo to (read (choose file without invisibles))

set T to (do shell script "echo " & quoted form of foo & " | tr \\r \\n")
set NP to count paragraphs of T -- produces the same number for any ending

set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to (ASCII character 13) & (ASCII character 10)
if (count text items of T) = NP then
        set AppleScript's text item delimiters to tid
        return "Windows"
end if
set AppleScript's text item delimiters to ASCII character 13
if (count text items of T) = NP then
        set AppleScript's text item delimiters to tid
        return "Mac"
end if
set AppleScript's text item delimiters to ASCII character 10
if (count text items of T) = NP then
        set AppleScript's text item delimiters to tid
        return "Unix"
end if
set AppleScript's text item delimiters to tid


hayne 12-16-2006 05:54 PM

Quote:

Originally Posted by NovaScotian (Post 342469)
Why doesn't something like this work?

set foo to (read (choose file without invisibles))
set the clipboard to (do shell script "echo " & foo & " | tr \\r \\n")

It's a feature of 'do shell script'.
From http://developer.apple.com/technotes/tn2002/tn2065.html
Quote:

Originally Posted by above Apple doc
By default, do shell script transforms all the line endings in the result to Mac-style carriage returns ("\r" or ASCII character 13), and removes a single trailing line ending, if one exists. This means, for example, that the result of do shell script "echo foo; echo bar" is "foo\rbar", not the "foo\nbar\n" that echo actually returned. You can suppress both of these behaviors by adding the without altering line endings parameter.


NovaScotian 12-16-2006 08:34 PM

Quote:

Originally Posted by hayne (Post 342487)
It's a feature of 'do shell script'.
From http://developer.apple.com/technotes/tn2002/tn2065.html

Thanks, Hayne. The problem is checking the result. Many apps, are all too eager to "help" by changing them back. Ugh.

hayne 12-17-2006 12:27 PM

Here's a new version of the above AppleScript for displaying Safari's memory usage. This version has the warnings (for exceeding the warn-level of memory usage) delayed longer each time you okay the warning dialog. This is useful in situations where Safari is exceeding the warning-level but you don't want to quit it immediately.

This version also has a property 'useDelayLoop' that you can set to make it possible to do test runs of the script from Script Editor.

Code:

-- This AppleScript displays the amount of memory being used by Safari
-- in the title of the frontmost Safari window.
-- It is intended to be saved as an application
-- and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

property useDelayLoop : false -- set this to true if you want to run from Script Editor
property updateDelay : 10 -- seconds
property warnIntervalIncrement : 120 -- seconds

on update()
    updateAppSize("Safari", 150)
end update

on run
    if useDelayLoop then
        repeat
            my update()
            delay updateDelay
        end repeat
    end if
end run

on idle
    if useDelayLoop then
        -- we should never get here
        display alert "Internal error: idle handler called even though using delay loop"
        quit
    end if
   
    my update()
    return updateDelay
end idle

-- updateAppSize:
-- Displays the amount of memory being used by the app 'appName'
-- Pops up an alert dialog if the amount of memory used exceeds 'warnMB'
on updateAppSize(appName, warnMB)
    if my appIsRunning(appName) then
        set numMB to my getProcessMB(appName)
        showAppSizeInTitle(appName, numMB)
        my checkIfMemoryUseMeritsWarning(appName, warnMB, numMB)
    end if
end updateAppSize

-- showAppSizeInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
on showAppSizeInTitle(appName, numMB)
    tell application appName
        if (count of windows) > 0 then
            try
                set title to name of window 1
                set delim to " *** "
                set origTitle to my removeLastPart(title, delim)
                set title to origTitle & delim & numMB & " MB"
                set name of window 1 to title
            on error
                -- there shouldn't be any errors
                -- (assuming that the app responds to 'name of window')
                -- but if an error occurs, we ignore it
            end try
        end if
    end tell
end showAppSizeInTitle

-- puts up a warning dialog about 'appName's memory usage
-- but avoids doing this too soon after the last warning
-- (increases the interval between warnings each time)
on checkIfMemoryUseMeritsWarning(appName, warnMB, numMB)
    global initWarnings, lastWarnTime, numWarnings
    try
        if initWarnings then
            -- nothing to do
        end if
    on error
        -- first time this subroutine is called we initialize variables
        set lastWarnTime to ((current date) - 1)
        set numWarnings to 0
        set initWarnings to true
    end try
   
    if numMB is greater than warnMB then
        set elapsed to secondsSince(lastWarnTime)
        set warnInterval to (numWarnings * warnIntervalIncrement)
        if elapsed > warnInterval then
            tell application appName
                display alert appName & " is taking more than " & warnMB & " MB"
            end tell
            set lastWarnTime to (current date)
            set numWarnings to (numWarnings + 1)
        end if
    else
        set numWarnings to 0
    end if
end checkIfMemoryUseMeritsWarning

-- returns the number of seconds since 'aDate'
on secondsSince(aDate)
    set curr to (current date)
    set elapsed to (curr - aDate)
    return elapsed
end secondsSince

-- returns a string with Unix-style line endings
on fixLineEndings(str)
    set oldTIDs to AppleScript's text item delimiters
    set theLines to paragraphs of str
    set AppleScript's text item delimiters to ASCII character 10
    set fixedStr to theLines as string
    set AppleScript's text item delimiters to oldTIDs
    return fixedStr
end fixLineEndings

-- getProcessMB:
-- Returns the number of megabytes used by the specified process.
-- It gets this value by invoking the Unix 'ps' command via a Perl script.
on getProcessMB(procName)
    set perlCode to fixLineEndings("
# This script gets the 'RSS' of the process specified as 'procName'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is given in megabytes.
open(PS, \"/bin/ps -xc -o command,rss |\");
while (<PS>)
{
    if (/^\\s*" & procName & "\\s+(\\d+)\\s*$/o)
    {
        my $rss = $1;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print $rssMB;
        last;
    }
}
close(PS);
")
    set numMB to do shell script "perl -e " & quoted form of perlCode
    return numMB as real
end getProcessMB

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
    set saveDelim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    if (count str's text items) > 1 then
        set str to str's text 1 thru text item -2
    end if
    set AppleScript's text item delimiters to saveDelim
    return str
end removeLastPart

-- appIsRunning:
-- Returns true if the app 'appName' is running, otherwise returns false
on appIsRunning(appName)
    set isRunning to false
    tell application "System Events"
        if exists process appName then
            set isRunning to true
        end if
    end tell
    return isRunning
end appIsRunning


NovaScotian 12-17-2006 12:39 PM

Runs nicely for me from either the Script Editor or Script Debugger 4. The fixLineEndings does it - bombproof!

hayne 12-17-2006 05:45 PM

New & improved! :)
Here's an improved version of the script. This version implements 'mark hunte's idea of showing CPU usage as well as memory usage.
It shows Safari's CPU usage (in the title of the Safari window) whenever it exceeds 10%

(I don't think having it give a warning dialog upon exceeding a certain CPU usage would be good since this script is intended to be left running all of the time, and sometimes you would expect Safari to be using a lot of CPU - e.g. when playing a Flash or Java game)

This version might also be slightly more efficient (use less CPU) since it gets the process-id of the Safari process from System Events and then runs the 'ps' command only for that one process instead of getting info on all processes from 'ps' and filtering out all the rest.

Code:

-- safariInfoInTitle:
-- This AppleScript displays the amount of memory being used by Safari
-- in the title of the frontmost Safari window.
-- It also displays Safari's CPU usage if it exceeds 10%.
-- This is intended to be saved as an application
-- and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

property useDelayLoop : false -- set this to true if you want to run from Script Editor
property updateDelay : 8 -- seconds
property cpuFloor : 10 -- % above which CPU usage will be shown in the title
property warnIntervalIncrement : 120 -- seconds

on update()
    updateAppInfo("Safari", 150)
end update

on run
    if useDelayLoop then
        repeat
            my update()
            delay updateDelay
        end repeat
    end if
end run

on idle
    if useDelayLoop then
        -- we should never get here
        display alert "Internal error: idle handler called even though using delay loop"
        quit
    end if
   
    my update()
    return updateDelay
end idle

-- updateAppInfo:
-- Displays the amount of memory being used by the app 'appName'
-- Pops up an alert dialog if the amount of memory used exceeds 'warnMB'
on updateAppInfo(appName, warnMB)
    set pid to my pidOfRunningApp(appName)
    if pid is -1 then
        -- app is not running, so nothing to do
    else
        set info to my getProcessInfo(pid)
        set numMB to first word of info as real
        set percentCpu to second word of info as real
        showAppInfoInTitle(appName, numMB, percentCpu)
        my checkIfMemoryUseMeritsWarning(appName, warnMB, numMB)
    end if
end updateAppInfo

-- showAppInfoInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
on showAppInfoInTitle(appName, numMB, percentCpu)
    tell application appName
        if (count of windows) > 0 then
            try
                set title to name of window 1
                set delim to " *** "
                set origTitle to my removeLastPart(title, delim)
                set title to origTitle & delim & numMB & " MB"
                if percentCpu > cpuFloor then
                    set title to title & ", " & percentCpu & "% CPU"
                end if
                set name of window 1 to title
            on error
                -- there shouldn't be any errors
                -- (assuming that the app responds to 'name of window')
                -- but if an error occurs, we ignore it
            end try
        end if
    end tell
end showAppInfoInTitle

-- puts up a warning dialog about 'appName's memory usage
-- but avoids doing this too soon after the last warning
-- (increases the interval between warnings each time)
on checkIfMemoryUseMeritsWarning(appName, warnMB, numMB)
    global initWarnings, lastWarnTime, numWarnings
    try
        if initWarnings then
            -- nothing to do
        end if
    on error
        -- first time this subroutine is called we initialize variables
        set lastWarnTime to ((current date) - 1)
        set numWarnings to 0
        set initWarnings to true
    end try
   
    if numMB is greater than warnMB then
        set elapsed to secondsSince(lastWarnTime)
        set warnInterval to (numWarnings * warnIntervalIncrement)
        if elapsed > warnInterval then
            tell application appName
                display alert appName & " is taking more than " & warnMB & " MB"
            end tell
            set lastWarnTime to (current date)
            set numWarnings to (numWarnings + 1)
        end if
    else
        set numWarnings to 0
    end if
end checkIfMemoryUseMeritsWarning

-- returns the number of seconds since 'aDate'
on secondsSince(aDate)
    set curr to (current date)
    set elapsed to (curr - aDate)
    return elapsed
end secondsSince

-- returns a string with Unix-style line endings
on fixLineEndings(str)
    set oldTIDs to AppleScript's text item delimiters
    set theLines to paragraphs of str
    set AppleScript's text item delimiters to ASCII character 10
    set fixedStr to theLines as string
    set AppleScript's text item delimiters to oldTIDs
    return fixedStr
end fixLineEndings

-- getProcessInfo:
-- Returns info about the resource usage of the specified process.
-- It gets this info by invoking the Unix 'ps' command via a Perl script.
-- The return value gives the number of megabytes used
-- followed by the percentage of CPU used
on getProcessInfo(pid)
    set perlCode to fixLineEndings("
# This script gets the 'RSS' & '%CPU' of the process with the given 'pid'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is converted to megabytes.
open(PS, \"/bin/ps -p " & pid & " -o pid,rss,%cpu |\");
while (<PS>)
{
    if (/^\\s*" & pid & "\\s+(\\d+)\\s+([\\d.]+)\\s*$/)
    {
        my $rss = $1;
        my $cpu = $2;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print \"$rssMB $cpu\";
        last;
    }
}
close(PS);
")
    set info to do shell script "perl -e " & quoted form of perlCode
    return info
end getProcessInfo

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
    set oldTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    if (count str's text items) > 1 then
        set str to str's text 1 thru text item -2
    end if
    set AppleScript's text item delimiters to oldTIDs
    return str
end removeLastPart

-- pidOfRunningApp:
-- Returns the pid of the process if the app 'appName' is running,
-- otherwise returns -1
on pidOfRunningApp(appName)
    tell application "System Events"
        try
            set pid to the unix id of process appName
        on error
            set pid to -1
        end try
    end tell
    return pid
end pidOfRunningApp


NovaScotian 12-17-2006 08:14 PM

Works very nicely on MM, Hayne. With useDelayLoop set to true and the first handler set as:

Code:

on update()
        updateAppInfo("Safari", 150)
        updateAppInfo("NetNewsWire", 100)
        updateAppInfo("Camino", 150)
end update

I got good results on those three applications running it from Smile, Script Editor, and Script Debugger 4, and finally with useDelayLoop set to false, good results as a stay-open application.

hayne 12-18-2006 09:24 AM

Thanks for testing this, NovaScotian.

By the way, if I add a line:
updateAppInfo("firefox-bin", 150)
then it does work fine as far as giving warnings about Firefox's memory usage (if it exceeds 150 MB with the above line).
But as discussed in an earlier post, it doesn't show the memory usage in the title since Firefox is not AppleScriptable.

NovaScotian 12-18-2006 10:23 AM

There's a "hack" that makes it possible to get/set the name of a Foxfire window from Rob Griffiths in MacWorld.com. I haven't tried it for Foxfire (which I don't like) and don't particularly recommend it - I just point it out. I've used it successfully with Preview because it is useful to be able to manipulate its windows.

hayne 12-18-2006 11:07 AM

Thanks for reminding me about that hack to enable basic AppleScript support in apps that don't by default support AppleScript.
I haven't tried it yet with Firefox

hayne 12-19-2006 03:53 AM

Here's a new version of the script with the following changes:
  • support for using "Growl" (http://growl.info/) for warnings instead of AppleScript alert dialogs
  • can specify different update periods for different apps
  • added lines to watch Terminal and Mail.app as well as Safari (you can comment out any you don't want, or add other apps if you want)

Here's the script:
Code:

-- appInfoInTitle:
-- This AppleScript displays the amount of memory being used by designated applications
-- in the title of their frontmost windows.
-- It also displays the CPU usage of these apps if it exceeds 10%.
-- And it gives a warning if the memory usage of an app exceeds a specified level.
-- This was originally designed to keep watch on Safari's memory consumption
-- but it can be used for various apps.
-- It is intended to be saved as an application and left running all of the time.
-- Cameron Hayne (macdev@hayne.net)  December 2006

property scriptName : "appInfoInTitle"
property useDelayLoop : false -- set this to true if you want to run from Script Editor
property useGrowl : false -- set this to true to get warnings via Growl (http://growl.info/)
property updateDelay : 5 -- seconds
property cpuFloor : 10 -- % above which CPU usage will be shown in the title
property warnIntervalIncrement : 120 -- seconds

global appInfoList -- list of AppInfo objects (added to via 'addAppInfo')

-- registerApps:
-- Add a call to 'addAppInfo' for each app you want to watch
on registerApps()
    addAppInfo given appName:"Safari", updatePeriod:5, warnMB:100
    addAppInfo given appName:"Terminal", updatePeriod:60, warnMB:15
    addAppInfo given appName:"Mail", updatePeriod:60, warnMB:30
end registerApps

-- update:
-- This subroutine is invoked every 'updateDelay' seconds
on update()
    repeat with appInfo in appInfoList
        updateAppInfo(appInfo)
    end repeat
end update

-- the run handler (invoked at script startup)
on run
    set appInfoList to {}
    registerApps()
   
    if useGrowl then
        registerWithGrowl()
    end if
   
    if useDelayLoop then
        repeat
            update()
            delay updateDelay
        end repeat
    end if
end run

-- the idle handler (invoked every 'updateDelay' seconds)
on idle
    if useDelayLoop then
        -- we should never get here
        display alert "Internal error: idle handler called even though using delay loop"
        quit
    end if
   
    update()
    return updateDelay
end idle

-- addAppInfo:
-- Creates a new 'appInfo' object and adds it to 'appInfoList'
-- 'updatePeriod' is the time in seconds between updates for this app
-- 'warnMB' is the memory usage (in megabytes) at which warnings will be issued
on addAppInfo given appName:anAppName, updatePeriod:anUpdatePeriod, warnMB:aWarnMB
    set prevTime to (current date) - 1000
    script appInfo
        property appName : anAppName
        property updatePeriod : anUpdatePeriod
        property warnMB : aWarnMB
        property lastUpdateTime : prevTime
        property lastWarnTime : prevTime
        property numWarnings : 0
        property numMB : 0
        property percentCpu : 0
    end script
    set appInfoList to appInfoList & appInfo
end addAppInfo

-- updateAppInfo:
-- Displays the amount of memory being used by the app 'appName'
-- Pops up an alert dialog if the amount of memory used exceeds 'warnMB'
on updateAppInfo(appInfo)
    set elapsed to secondsSince(lastUpdateTime of appInfo)
    if elapsed < (updatePeriod of appInfo) then return
   
    set pid to pidOfRunningApp(appName of appInfo)
    if pid is -1 then
        -- app is not running, so nothing to do
    else
        set info to getProcessInfo(pid)
        if info is "" then return
        set numMB of appInfo to round (first word of info as real)
        set percentCpu of appInfo to round (second word of info as real)
        showAppInfoInTitle(appInfo)
        checkIfMemoryUseMeritsWarning(appInfo)
    end if
   
    set lastUpdateTime of appInfo to (current date)
end updateAppInfo

-- showAppInfoInTitle:
-- Displays the amount of memory being used by the app 'appName' in the title
-- of the frontmost window of that app.
on showAppInfoInTitle(appInfo)
    set appName to (appName of appInfo)
    set numMB to (numMB of appInfo)
    set percentCpu to (percentCpu of appInfo)
   
    try
        tell application appName
            if (count of windows) > 0 then
                set title to name of window 1
                set delim to " *** "
                set origTitle to my removeLastPart(title, delim)
                set title to origTitle & delim & numMB & " MB"
                if percentCpu > cpuFloor then
                    set title to title & ", " & percentCpu & "% CPU"
                end if
                set name of window 1 to title
            end if
        end tell
    on error
        -- there shouldn't be any errors
        -- (assuming that the app responds to 'name of window')
        -- but if an error occurs, we ignore it
    end try
end showAppInfoInTitle

-- checkIfMemoryUseMeritsWarning:
-- Puts up a warning dialog about 'appName's memory usage
-- but avoids doing this too soon after the last warning
-- (increases the interval between warnings each time)
on checkIfMemoryUseMeritsWarning(appInfo)
    set appName to (appName of appInfo)
    set numMB to (numMB of appInfo)
    set warnMB to (warnMB of appInfo)
    set lastWarnTime to (lastWarnTime of appInfo)
    set numWarnings to (numWarnings of appInfo)
   
    if numMB > warnMB then
        set elapsed to secondsSince(lastWarnTime)
        set warnInterval to (numWarnings * warnIntervalIncrement)
        if elapsed > warnInterval then
            set msg to appName & " is taking more than " & warnMB & " MB"
            displayWarning(appName, msg)
            set lastWarnTime to (current date)
            set numWarnings to (numWarnings + 1)
        end if
    else if numMB > (0.95 * warnMB) then
        -- avoid continual warnings when oscillating around 'warnMB' level
        set numWarnings to 1
    else
        set numWarnings to 0
    end if
   
    set lastWarnTime of appInfo to lastWarnTime
    set numWarnings of appInfo to numWarnings
end checkIfMemoryUseMeritsWarning

-- displayWarning:
-- Displays a warning relating to 'appName'
on displayWarning(appName, msg)
    if useGrowl then
        try
            displayWarningViaGrowl(appName, msg)
        on error
            displayWarningViaAlert(appName, msg)
        end try
    else
        displayWarningViaAlert(appName, msg)
    end if
end displayWarning

-- displayWarningViaAlert:
-- Displays a warning via an AppleScript alert dialog
on displayWarningViaAlert(appName, msg)
    tell application appName
        display alert msg
    end tell
end displayWarningViaAlert

-- displayWarningViaGrowl:
-- Displays a warning via "Growl"
on displayWarningViaGrowl(appName, msg)
    tell application "GrowlHelperApp"
        notify with name "MemoryUseWarning" title "Warning" description msg application name scriptName
    end tell
end displayWarningViaGrowl

-- registerWithGrowl:
-- Registers our notifications with the "Growl" tool
on registerWithGrowl()
    set notifList to {"MemoryUseWarning"}
    tell application "GrowlHelperApp"
        register as application scriptName all notifications notifList default notifications notifList
    end tell
end registerWithGrowl

-- secondsSince:
-- Returns the number of seconds since 'aDate'
on secondsSince(aDate)
    set curr to (current date)
    set elapsed to (curr - aDate)
    return elapsed
end secondsSince

-- fixLineEndings:
-- returns a string with Unix-style line endings
on fixLineEndings(str)
    set oldTIDs to AppleScript's text item delimiters
    set theLines to paragraphs of str
    set AppleScript's text item delimiters to ASCII character 10
    set fixedStr to theLines as string
    set AppleScript's text item delimiters to oldTIDs
    return fixedStr
end fixLineEndings

-- getProcessInfo:
-- Returns info about the resource usage of the specified process.
-- It gets this info by invoking the Unix 'ps' command via a Perl script.
-- The return value gives the number of megabytes used
-- followed by the percentage of CPU used
on getProcessInfo(pid)
    set perlCode to fixLineEndings("
# This script gets the 'RSS' & '%CPU' of the process with the given 'pid'
# The 'RSS' (resident set size) is a rough measure of how much RAM
# the process is using. The value is converted to megabytes.
open(PS, \"/bin/ps -p " & pid & " -o pid,rss,%cpu |\");
while (<PS>)
{
    if (/^\\s*" & pid & "\\s+(\\d+)\\s+([\\d.]+)\\s*$/)
    {
        my $rss = $1;
        my $cpu = $2;
        my $rssMB = sprintf(\"%.1f\", $rss / 1024);
        print \"$rssMB $cpu\";
        last;
    }
}
close(PS);
")
    set info to do shell script "perl -e " & quoted form of perlCode
    return info
end getProcessInfo

-- removeLastPart:
-- Returns the string 'str' without the last part starting with 'delim'
on removeLastPart(str, delim)
    set oldTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to delim
    if (count str's text items) > 1 then
        set str to str's text 1 thru text item -2
    end if
    set AppleScript's text item delimiters to oldTIDs
    return str
end removeLastPart

-- pidOfRunningApp:
-- Returns the pid of the process if the app 'appName' is running,
-- otherwise returns -1
on pidOfRunningApp(appName)
    tell application "System Events"
        try
            set pid to the unix id of process appName
        on error
            set pid to -1
        end try
    end tell
    return pid
end pidOfRunningApp

-- Copyright (C) 2006  Cameron Hayne - macdev@hayne.net
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.


mark hunte 12-19-2006 09:22 PM

Hayne,
Just like to say this app is very useful,

Thanks

hayne 12-21-2006 06:59 AM

Quote:

Originally Posted by mark hunte (Post 343241)
Just like to say this app is very useful,

Glad you find it useful - I wrote it to "scratch an itch" of mine and I run it all the time.
I still find it strange that sometimes this AppleScript application seems to take 10 or 15 MB of RAM and then if I quit it and re-launch it, its RAM usage is down to 5 MB.
I'd be interested to hear if others are seeing this phenomenon.

By the way, I edited the above (latest) version of the script to make it round the memory & CPU numbers to integers. It seemed silly to be reporting 1/10ths of MBs - and this saves a bit of space on the title-bar as well.

mark hunte 12-21-2006 07:24 AM

My copy never seems to get below 14mb on my tower and PB.
I suppose I should be happy with it being consistent? :)

thanks again.


Mark

NovaScotian 12-21-2006 09:58 AM

[ot]
 
Speaking of "useful", Daniel Jalkut's Blog has some very flattering remarks, Hayne, for ash as a means of running AppleScripts via ssh when he's away from his machine. Looks neat. :)

hayne 12-21-2006 12:26 PM

Quote:

Originally Posted by mark hunte (Post 343544)
My copy never seems to get below 14mb on my tower and PB.

So you've tried quitting it and relaunching and it is always taking the same amount of RAM?
At one point I thought maybe it was somehow correlated with whether you had the script open in Script Editor or not.


All times are GMT -5. The time now is 01:42 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.