Re: Scripting Additions: Embracing the Horror of Unix
Re: Scripting Additions: Embracing the Horror of Unix
- Subject: Re: Scripting Additions: Embracing the Horror of Unix
- From: Nigel Smith <email@hidden>
- Date: Mon, 04 Feb 2002 11:34:11 +0000
Here's a (possibly!) useful example of how the Unix-y side of OSX can be of
help to mere mortals like myself.
I'm an occasional scripter, who can't justify the cost of ScriptDebugger and
the like. Of course, as only an occasional scripter I have more problems
than the more accomplished coders on this list, so I would really benefit
from things like ScriptDebbugers listing of current variables. Up to now
I've got round this by using lots of "display dialog..." statements in the
script, then got roundly annoyed by having to keep hitting the return key.
I'm sure others are in the same boat. So how about a constantly updated
window into which you can send any messages you want, as the script runs.
Unix has a "tail" command, which lists the last few (you can tell it how
many) lines of a file. Boring! But "tail" has an option, "-f", which
basically says "keep this file open and carry on displaying new stuff as it
comes in". You stop the updating, and quit "tail", by typing control-C.
Excellent for watching logfiles as they are updated.
So I've started putting in the following handlers in my scripts:
************
--example script part
debugStartup() of me
repeat with x from 1 to 20
delay 1
writeDebug("x= " & x & " x*x= " & x * x) of me
end repeat
--end example part
on debugStartup()
tell application "Finder"
if not (exists file "ASLog.txt" of folder "Documents" of home) then
make new file at folder "Documents" of home with <option-L>
properties {name:"ASLog.txt"}
end if
end tell
tell application "Terminal"
do script with command "tail -f ~/Documents/ASLog.txt"
end tell
end debugStartup
on writeDebug(txtString)
do shell script "echo " & txtString & " >> ~/Documents/ASLog.txt"
end writeDebug
************
debugStartup() sets up the logfile, opens a Terminal window, then starts
showing the logfile. writeDebug adds text to the end of the logfile.
Like I said, I'm only an occasional coder, so there are probably better ways
of doing this -- for example, I can't get the terminal session to end
cleanly. But perhaps this will start better minds than mine thinking...
Later,
Nigel