I am trying to rotate a log file by renaming it. The file lives in ~/Library/Logs. My script will create the file and write to it, but if I add a routine to rename the file after a certain size I get an error:
[b]AppleScript Error[/b]
Finder got an error: Can't set name of file
"iTunesSync.log" of folder "Users:trinkel:Library:Logs"
to "iTunesSync.log.1".
Here is the main part of the script:
global logName
set logName to "iTunesSync.log"
global logArch
global logDir
global logFile
set theUser to (do shell script "whoami")
set logDir to "Users:" & theUser & ":Library:Logs"
set logFile to logDir & ":" & logName
set logArch to logName & ".1"
set logSize to 1 * 1024 -- In KB
if (size of (info for file logFile)) > logSize then
tell application "Finder"
set the name of file logName of folder logDir to logArch -- Problem here
end tell
end if
set the openFile to open for access file logFile with write permission
write "Run: " & (current date) & [return] to the openFile starting at eof
close access the openFile
For both the original file and the new name I’ve tried the variable as "filename of folder . . . " and just the filename. So far all the variations have given me similar errors. I’ve also tried moving the file rather than setting the name and do shell mv, All with similar results.
If I display dialog on the variables, they seem to be OK, and again, I can create and write to the file.
I know I’m at least in the ball park 'cause I got the set name syntax straight out of several posts. This has got to be some sort of freaky data type thing and I’m just not getting it.
I don’t know if this will help or not, but this is a snippet of code from a script I wrote. This renames a file that has been created. Maybe you can adapt this for your purposes.
select file "untitled archive.sit" of folder destPath -- select your file here
set thisArchive to selection as alias -- set a variable to point to your file
set name of thisArchive to completeFile -- rename the file
I don’t know why I set it up this way but it works alright.
property logFile : file specification ((path to library folder from user domain as Unicode text) & "Logs:iTunesSync.log")
property logSize : 1 * 1024
property newName : "iTunesSync.1"
if (size of (info for file logFile)) > logSize then
try
tell application "Finder" to set name of logFile to newName
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
end if
try
set openFileRef to open for access logFile with write permission
write ("Run: " & (current date) & return) to openFileRef starting at eof
close access openFileRef
on error
try
close access openFileRef
end try
end try
I’ll give that a shot. Looks a lot more professional. I’m also going to have to either delete the existing .1 file or replace. Simply adding “with replace” didn’t seem to cut it. I’ll futz with that tonight.