How can I print a list result as a list in a txt file?

I am trying to write a script that will send Safari’s URL and Title to a host machine log file. So far I have come up with the following way of doing it, but I cannot get the variable urlWords to print into the log file as a list rather than as a string.

Once the file is updated launchd on the host machine will will launch a script that will read the changes, what I would also like to know is, is there a better way to go about updating this file and making sure that the right changes will be read, or should I be doing something else entirely?

set DT to current date
set MacName to computer name of (system info)

set mountString to "afp://user:pass@pip-boy-3000.home/Bootle ROOT/"

tell application "Safari" to set longURL to URL of front document
set urlWords to words of longURL as list
set i to count of urlWords
set theURL to item 2 of urlWords
tell application "Safari"
	tell window 1 to set SafTitle to name
end tell
set AllTheWords to words of SafTitle & urlWords as list

set DataPack to return & "000" & return & MacName & " requests:" & return & SafTitle & return & theURL & return & "Words:" & return & urlWords & return & DT & return & "000" & return

mount volume mountString
set filePath to "/Volumes/Bootle ROOT/" & "LogFile.txt"
set f to open for access POSIX file filePath with write permission

write DataPack to f starting at eof
close access f

Add “as list” to your write command.

Read Nigel Garvey’s article on Read/Write: http://macscripter.net/viewtopic.php?id=24745

I tried that already, it made no difference. I’ll have a look at the link, but that will likely take a while.

Hi, Nedanator.

  1. You’re concatenating urlWords into the text DataPack, so of course the value used is its value coerced to text.

  2. The problem with saving a list to a file is that, unless the list is the entire content of the file, you have to know where it starts and ends in the file before you can read it back again. You’re appending the data to the end of the file, which means you have virtually no chance of reading them back again in the original form.

I can’t see why you’d want to write the URL words as a list anyway. (Do you really mean an AppleScript list?) Assuming it was necessary, I’d make DataPack a list containing the first part of the text, urlWords, and the rest of the text:

set DataPack to {return & "000" & return & MacName & " requests:" & return & SafTitle & return & theURL & return & "Words:", urlWords, DT as text & return & "000" & return}

Each DataPack would then have to be an item in another list and that is what you’d have to write as the sole content of the file:

set f to open for access POSIX file filePath with write permission
try
	if ((get eof f) is 0) then
		set uberList to {}
	else
		set uberList to (read f as list)
	end if
	set end of uberList to DataPack
	set eof f to 0 -- Empty the file.
	write uberList to f
end try
close access f

You’d then read the file ‘as list’ and every item in it would be a DataPack list.