Re: Loop to erase the contents of my logs ...
Re: Loop to erase the contents of my logs ...
- Subject: Re: Loop to erase the contents of my logs ...
- From: Christopher Nebel <email@hidden>
- Date: Tue, 10 Jun 2003 15:44:04 -0700
On Tuesday, June 10, 2003, at 09:57 AM, Charles Heizer wrote:
I'm stuck. I'm trying to write a small apple script to clear the
contents of all of my logs before I image my system, but I keep
hitting a wall. I can do this with a foreach statement using a shell
script but I wanted to find out how to do it with applescript.
Next time you want help with a script, be sure to mention what the
exact problem is. Throwing out an entire script and saying "this
doesn't work" tends to produce random advice.
Here is my code --
property attributes : {}
property multiple : 1
set logLoc to "/private/var/log3"
set logLoc to (POSIX file logLoc) as Unicode text
tell application "Finder"
set logLoc to name of every file of folder logLoc whose name ends
with ".log"
end tell
This is half the problem -- it's perfectly valid code, but you're
getting a list of file names, which are meaningless without the context
of folder logLoc. (The old logLoc, that is.) See below.
repeat multiple times
repeat with c in logLoc
set end of attributes to {c}
end repeat
end repeat
I have no idea why you're doing this. If the goal was to copy logLoc
to attributes, you could have done that without the loop by saying
"copy logLoc to attributes". (There's still a slight difference in
semantics, but not one that matters.) Of course, I don't know why you
thought you needed to do that, nor do I know why you thought you might
need to do it more than once.
activate
This is irrelevant to the task at hand.
repeat with i in attributes
set theLog to item 1 of i
set theLog to open for access theLog with write permission
set eof theLog to 0
close access theLog
end repeat
OK, two problems here.
First, "set theLog to item 1 of i" is bad voodoo. I'm guessing that
you meant to resolve i to an object, instead of the object specifier it
really is, but what you'll wind up with, since "attributes" is a list
of strings, is the first character of each string -- probably not what
you wanted.
Second is the other half of the fundamental problem above: "attributes"
is just a list of file names, which aren't useful with "open for
access" without the context of a folder. (That's not quite true, but
at the very least, they won't refer to the files you originally got.)
The stuff to truncate the files is correct.
Given all that, let's try this again:
set logLoc to "/private/var/log3"
set logLoc to (POSIX file logLoc) as Unicode text
tell application "Finder"
set logs to (every file of folder logLoc whose name ends with ".log")
as alias list
end tell
repeat with i in logs
set theLog to open for access i with write permission
set eof theLog to 0
close access theLog
end repeat
This is much the same as your original script, except that now the
first chunk gets references to the files themselves, not just their
names. (For obscure reasons, we need to coerce them to aliases with
"as alias list". Ordinarily, the Finder returns Finder object
specifiers, which don't travel well outside the Finder.)
I omitted the mysterious middle bit, and the last bit just loops
through the file references, setting their lengths to zero.
Incidentally, you might need to put the loop body in a "try" block; not
all the logs in /var/log are user-writable.
--Chris Nebel
Apple Development Tools
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.