AS Write to File: log file

I’m trying to add a few lines of code to all of my department’s scripts that will log a script’s usage. The below script can get the user’s name from their work drive, a set script name, and date/time and write it to a text file without opening the application. Problem is I need the script to find the last line of a text file and write the text there, that way one text file can hold all of the logs. So far this script can only rewrite all of the text in the file.


set AppleScript's text item delimiters to ":"
set theusername to text item 3 of (path to desktop as string)
set AppleScript's text item delimiters to ""
---sets script name
set thescriptname to "SCRIPT So-and-SO"
---gets log file
set filePath to (path to desktop as string) & "CSScriptPerformance.rtf"
try
	set fileRef to open for access filePath with write permission
	set eof fileRef to 0
	try
		set newText to theusername & tab & thescriptname & tab & (current date) & return as string
		write newText to fileRef
	end try
	close access fileRef
end try

Many thanks for any help!!!
Stacy

Lose the set eof statement and change your write statement

       write newText to fileRef starting at eof

Hi Stacy,

write . starting at eof appends the text to the file.


set theusername to short user name of (system info)

---sets script name
set thescriptname to "SCRIPT So-and-SO"
---gets log file
set filePath to (path to desktop as string) & "CSScriptPerformance.txt" -- AppleScript's write command writes plain text
try
	set fileRef to open for access file filePath with write permission
	set newText to theusername & tab & thescriptname & tab & ((current date) as string) & return
	write newText to fileRef starting at eof
	close access fileRef
on error
	try
		close access file filePath
	end try
end try

Ah ha!! that did it…thank you so much!!

I am trying to use the above code to create a log file. I’m writing it in an active text document in TextEdit rather than leaving it unopened. I’m logging the steps involved in copying data from raw datafiles into an excel sheet. The code below is only one part of several steps. Here is a summary of what I’m doing, just for context.

Using nested repeat loops I am cycling through a folder of raw data text files and copying them to excel. I want text edit to record the file name, time, and whether there was an error copying the file. It should write one thing (an *) when there is an error in a step…but if everything is fine to leave that spot blank in the list. This shouldn’t be an issue, the repeat loops are working fine. I am also able to create the text file and write the header.

Here are my issues:

For some reason I can’t get the “write…starting at eof” command to work.

On a related note, I’m also having issues opening the text file (set as theFile) in the code below as a tab delimited file in Excel. This is my raw data file which appears to be a simple text file with tab delimiters and returns at the end of lines. I can open it fine using File>Open in Excel, but when I try to use applescript it fails every time, no matter what settings I use for the import.

I am not very experienced with Applescript, which is likely the problem. I hope someone can push me in the right direction.

--sets result file record as a text file
set progFile to choose file name with prompt "Name and save a text file to record progress of data transfer." & return & "Save in the same folder as the data reduction worksheet."
tell application "TextEdit"
	make new document
	set theDate to current date
	set theTime to time string of theDate
	set text of document 1 to (theDate as text) & return & return & "Results of data import from Element ICPMS output text files" & return & return & "ICPMS output text files are listed below in the order that they were pasted into the data reduction sheet. Files that had an error likely did not paste over. They are listed and should be checked." & return & return & "File Name" & tab & "Err" & tab & "Time" & return
	save document 1 in progFile
end tell
--copies in new template sheet and copies in data from raw text files until all files are copied
tell application "Microsoft Excel"
	set sourceSheet to sheet 1 of workbook templateFile
	set destinationBook to workbook dataFile
	activate destinationBook
	copy worksheet sourceSheet after sheet (get count of sheets)
	try
		open text file filename theFile data type delimited with tab
	on error
		tell application "TextEdit"
			activate
			set newText to the name of theFile & tab & "*" & tab & time string of theDate & return
			write newText to progFile starting at eof
		end tell

	end try
end tell

I can post the full code, with repeat loops, if you want but I tried to cut it down for simplicities sake.

Model: 2.4Ghz MacBook Pro
AppleScript: 2.0.1
Browser: Safari 531.22.7
Operating System: Mac OS X (10.6)

Hi,

write . starting at eof is a part of Standard Scripting Additions to write text into a file without involving any application. TextEdit doesn’t respond to the write command.
In TextEdit you must get the text, append the new text and put it back into the document


.
try
	open text file filename theFile data type delimited with tab
on error
	tell application "TextEdit"
		activate
		set newText to the name of theFile & tab & "*" & tab & time string of theDate & return
		set text of document 1 to (get text of document 1 & newText)
	end tell
end try
.

I don’t know where the variable theFile is defined, it could be that name of theFile doesn’t work

Thanks StefanK! Works like a charm now.

theFile is defined in my script but I accidentally cut out the part where I set it when I simplified the code to post.

Do you have any good resources for figuring out how to open a text file into Excel? That’s my last bugaboo on this project.

I’ll post to an excel thread but I thought I’d ask you if you had any ideas.

Thanks again.

your syntax works if theFile is a HFS string path

Stefan,

Thanks. I finally got it to work with no issues after messing with it a bit.

open text file filename (theFile as text) data type delimited with tab

I’m not exactly sure why this works and the other didn’t. I couldn’t make it open without specifying it as text. I’m sure there is a very logical reason but my lack of basic understanding of AS makes it hard to figure out.

as text coerces an object (alias or file specifier) to a HFS string path, which is the expected class of Excel’s open text file