![]() |
Quote:
In general, Unix utilities (e.g. Perl) will assume that all text files have the one-true-line-ending ('\n' or "Unix-style) and will silently fail if this is not the case. This is true of the script files (like this Perl script) and the data files they are processing. See the "line endings" section of this Unix FAQ for more info on this. I suspect that the problem with the Perl script that is embedded within the AppleScript (in the subroutine 'getProcessMB') not working is due to the same sort of problem. I worried a bit about this when I wrote that AppleScript - I wasn't sure that the line-endings on that embedded Perl code were going to be correct. But I typed it into Script Editor and then wrote the embedded script to a file and it all checked out fine. But I just read today on the AppleScript users mailing list that "Script Editor" and Satimage's "Smile" use different line endings - Smile uses the traditional Macintosh line endings. And I now realize that this might be relevant here. I'm guessing that maybe NovaScotian used Smile to save the AppleScript and that is why it isn't working for him. It's a bit nasty having to rely on the behaviour of one editor (Script Editor) to get the embedded line endings correct, but the only alternative I can see is to add in explicit newlines in the embedded Perl code and then string it all together into one unreadable mess of one line in the AppleScript. I thought it was bad enough that I had to escape all the backslashes and quotes in that embedded Perl! Bottom line: for the moment, NovaScotian, could you please try copy & pasting the above AppleScript into "Script Editor" instead (assuming that you were using "Smile" before). |
Quote:
That's the sort of thing I was seeing last night. But now it is taking much less as I mentioned above - I don't understand why there would be this difference. |
Quote:
Will Run VersionB shortley |
Quote:
VersionA (A for AppleScript) and VersionP (P for Perl) :) |
Doh.. :rolleyes: its late here.. or I should say early (gone 6 in the morning). Have not been bed yet.
I also find that when safari is playing up it uses lots of cpu, this sometimes happens after I have gone into another app just after safari, and at some point things get slow, a lot of beach balling. When I look for the reason why, A lot of the time its Safari in the background. I do not know how helpful this is but I added cpu to the script. It rough but seems to work. I put 50% cpu warning but I may lower it when I get a gauge on it. Code:
on showAppSizeInTitle(appName, warnMB)Url,time, usage |
Neither of Hayne's versions work for me, though the perl script above by itself does.
I'm running this using a hot key rather than a idle script: set tApp to "Safari" -- name of browser set tLim to 75 -- acceptable memory use in MB memToTitle(tApp, tLim) -- get title to include memory use (* Script concept by Cameron Hayne, version by Adam Bell *) to memToTitle(appName, tooHigh) tell application "System Events" to if exists process appName then -- it's running try tell process appName to set N to name of window 1 set OK to true -- it's running and has a window open on error set OK to false -- no window end try else set OK to false -- not running end if -- Running with window; get memory estimate to four significant figures if OK then set MB to (100 * ((last word of (do shell script "/bin/ps -xc -o command,rss | grep " & appName)) / 1024) as integer) / 100 -- Put estimate in window title tell application appName -- remove previous addition first set dlim to " @ Mem = " -- something not likely to be found in a title set tid to AppleScript's text item delimiters set AppleScript's text item delimiters to dlim if (count N's text items) > 1 then set N to N's text item 1 set AppleScript's text item delimiters to tid set name of window 1 to N & dlim & MB & " MB" if MB > tooHigh then display alert appName & " is consuming more than " & tooHigh & " MB of memory" end tell end memToTitle Note: Unlike Hayne's version, this one doe not trigger a warning if there are no windows. This works in Camino as well. |
Quote:
|
I had not, Hayne, but just did (I've been out all day). I'm using Script Debugger 4 which may be part of the problem, because in Script Editor, the script with the command below succeeds every time I run it.
Problem solved, apparently. Excellent sleuthing, Hayne. Code:
--on idle |
Last Minute Addition to previous post:
I copied the whole script from Script Editor to a new document in Script Debugger 4 and it works. - Obviously the line ending problem arises from copying from the web page, rather than having the script as an original. I have found that it also works quite nicely in Camino, but getProcessMB fails when the app is Firefox, and Shiira doesn't know about its windows. It works beautifully in NetNewsWire (which does have a memory leak and grows very slowly if left running). I may modify it slightly to watch both. Sorry to be so troublesome. |
Quote:
This is an interesting issue for any embedded script (for use with 'do shell script') in AppleScript - a much more general issue than just the particular script under discussion in this thread. It is also an issue if some embedded text in an AppleScript were to be written to a file (using 'write' in AppleScript). See for example, the following AppleScript: Code:
-- This AppleScript is intended for testing the behaviour of If I comment out the line that invokes the 'fixLineEndings' subroutine, and then run the above AppleScript from "Script Editor", it still reports that the file is "ASCII English text". But when I run the identical AppleScript (with the call to 'fixLineEndings' commented out) from "Smile", it reports that the file is "ASCII English text, with CR line terminators". |
Quote:
Quote:
(The full path to the executable is /Applications/Firefox.app/Contents/MacOS/firefox-bin ) |
Quote:
I hope you'll report back to this thread if you figure out how to deal with script that have required line endings. One way (that I haven't tried yet) would be to replace all the \r with \n in a first line of the script and another would be do do that using text item delimiters and ASCII character 10. Finally, if the server of a site happens to be a Windows machine, then it can have CRLF as a paragraph delimiter, and that's often a problem with copied material. A generic way to fix this would be great, and I see that you've got "feelers" out for that. Please let us know if you find a "best" way. |
Quote:
With the use of this subroutine, that AppleScript gives the same result whether compiled in "Script Editor" or in "Smile". If I comment out that subroutine, the same script gives different results when using "Script Editor" than when using "Smile". I'd be very interested to hear what happens if you use "Script Debugger". I think (but I haven't tested it) that the above 'fixLineEndings' subroutine might work to fix any string - even one with Windows-type line endings. |
Quote:
Code:
% getProcessRss firefox-bin |
Quote:
But even that does not work. The sdef for Firefox is very very basic. It does not even have the Apple Script basics. That could be why? |
Quote:
Good stuff, CH. PS: your fixLineEndings is the simplest way to do it too. |
For the shell-script impaired, the test for line endings can be done in AppleScript too.
set txt to read alias ((path to desktop folder as text) & "SomeText.txt") whichEnding(txt) on whichEnding(T) 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 end whichEnding |
Quote:
It seems to be returning before resetting "AppleScript's text item delimiters" to what it was. And hence the delimiter value will affect subsequent AppleScript in that script and all other until the environment is restarted (e.g.Script Editor is re-launched). |
Good catch, Hayne;
Several things wrong with it and that was only one of them. Order is important too. I've corrected the original rather than leave it stand broken. |
Quote:
Your subroutine doesn't return anything in this case ('NP' would not match any of the "pure" cases). This subject is tricky. |
| 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.