HELP -- Writing to a text file

I’ve got a simple problem. Can you help?

I’ve got a list of IP addresses in IP_AddressList. The IP addresses include return characters. I want to append some text and the current date to each IP in the list, separated with a tab, and write the file to disk. In other words, create a tab-delimited text file: IP address date text string

The date should be in a format that is readable by Excel.

This works OK to export the IP addresses only:

set theFile to choose file name default name “IP Addresses”
open for access theFile with write permission
set eof theFile to 0
repeat with the_IP in IP_AddressList
write the_IP
end repeat
close access theFile

I tried to append the string and date to the date as follows:
write the_IP & (ASCII character 9) & date string of (current date) & “text string” to theFile

Interestingly, I got a tab character between each character in the IP address. I don’t understand this because the Repeat loop is iterating over the IP_AddressList.

Any help would be most appreciated.
TIA, Steve

Sounds obvious, I know, but can we see a sample (even made up) of what your IP_Address list looks like?

Have you at any point earlier set AppleScript’s text item delimiters to something other than “”?

Have you tried this: write (the_IP & tab & date string of (current date) & space & “text string”) to theFile

Hi, Adam. Entire script follows.

  • text delimiters are changed, but are are restored to default delimiter before writing file.
  • The “write the_IP & (ASCII character 9) to theFile” line is putting a tab between each character.
  • If I use “write the_IP” alone, the file is written correctly as shown immediately below (copied from saved file “IP Addresses”):
    201.153.17.177
    218.61.140.54
    219.146.109.23
    60.11.195.165
    83.19.206.156
    222.80.237.157
    86.199.226.61
    222.82.28.57
    220.93.66.30
    59.32.127.192
    220.188.79.157
    218.78.185.204

tell application “Microsoft Entourage”
set deletedMessages to messages in deleted mail folder

-- if there are no messages selected, warn user and quit
if deletedMessages is {} then
	display dialog "Deleted Items folder is empty" with icon 1
	return
end if

set IP_AddressList to "" -- List of IP Addresses to be exported
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "X-YahooFilteredBulk: "

repeat with theMessage in deletedMessages
	set hdrSource to (the headers of theMessage)
	if (hdrSource contains "X-YahooFilteredBulk: ") then
		set ipAddr to paragraph 1 of text item 2 of hdrSource
		set IP_AddressList to IP_AddressList & {ipAddr & (ASCII character 13)}
	end if
end repeat
set AppleScript's text item delimiters to astid

try
	if IP_AddressList is not "" then
		set theFile to choose file name default name "IP Addresses"
		open for access theFile with write permission
		set eof theFile to 0
		repeat with the_IP in IP_AddressList
			write the_IP & (ASCII character 9) to theFile
		end repeat
		close access theFile
	end if
on error
	display dialog "There was an error saving the selection." buttons {"OK"}
end try

end tell

Adam…I tried the line you suggested, but got the same results as shown below. The date string and text string are getting written out between every character in the IP addresses. You can see the IP addresses below, e.g., 201.153.17. etc, interspersed in the data and “text string” text.

Steve

2 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string3 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string7 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string7 Saturday, February 4, 2006 text string7 Saturday, February 4, 2006 text string
Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string8 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string4 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string4 Saturday, February 4, 2006 text string
Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string9 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string4 Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string9 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string3 Saturday, February 4, 2006 text string
Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string9 Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string
Saturday, February 4, 2006 text string8 Saturday, February 4, 2006 text string3 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string9 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string6 Saturday, February 4, 2006 text string
Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string8 Saturday, February 4, 2006 text string0 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string2 Saturday, February 4, 2006 text string3 Saturday, February 4, 2006 text string7 Saturday, February 4, 2006 text string. Saturday, February 4, 2006 text string1 Saturday, February 4, 2006 text string5 Saturday, February 4, 2006 text string7 Saturday, February 4, 2006 text string

Sometimes I think it’s easier to use some a shell command to do any file writing - it’s just faster that typing…open for access, close for access, etc… and you can set the formatting of the output pretty easily as well. the only tricky part is the escaping… anway - does this help?

set IP_AddressList to {"201.153.17.177", "218.61.140.54", "219.146.109.23", "60.11.195.165", "83.19.206.156", "222.80.237.157", "86.199.226.61", "222.82.28.57", "220.93.66.30", "59.32.127.192", "220.188.79.157", "218.78.185.204"} as list

set theFile to (path to desktop folder as string) & "IP Addresses"

repeat with the_IP in IP_AddressList
	do shell script "printf \"" & the_IP & "\\t" & date string of (current date) & space & "text string" & "\\n\" >>" & (quoted form of POSIX path of theFile)
end repeat

--returns in a file called IP_adresses on the desktop

201.153.17.177	Saturday, February 4, 2006 text string
218.61.140.54	Saturday, February 4, 2006 text string
219.146.109.23	Saturday, February 4, 2006 text string
60.11.195.165	Saturday, February 4, 2006 text string
83.19.206.156	Saturday, February 4, 2006 text string
222.80.237.157	Saturday, February 4, 2006 text string
86.199.226.61	Saturday, February 4, 2006 text string
222.82.28.57	Saturday, February 4, 2006 text string
220.93.66.30	Saturday, February 4, 2006 text string
59.32.127.192	Saturday, February 4, 2006 text string
220.188.79.157	Saturday, February 4, 2006 text string
218.78.185.204	Saturday, February 4, 2006 text string 

That’s great, Chris. Thanks. I’ve been studying the shell, bash, awk, sed, grep, cut, etc a lot the last couple weeks and I’m starting to get the hang of it. Thanks for developing the shell script for me. It’s very easy to understand.

I’m still stumped why my AppleScript didn’t work, tho. It worked fine to save the text file with the IP addresses alone. But I couldn’t get it to save tab delimited lines with the other two fields.

Steve

Chris…still getting weird results.

If I run your script by itself, I get a file with exactly what I want. When I put your code into my script, two things happen:

  1. the command “set theFile to (path to desktop folder as string)” compiles to "set theFile to (path to desktop folder recipient type string) "
  2. the output file has the same problems I had before:
    GOOD OUTPUT from your script by itself:
    201.153.17.177 Saturday, February 4, 2006 text string
    218.61.140.54 Saturday, February 4, 2006 text string
    219.146.109.23 Saturday, February 4, 2006 text string
    60.11.195.165 Saturday, February 4, 2006 text string
    83.19.206.156 Saturday, February 4, 2006 text string
    222.80.237.157 Saturday, February 4, 2006 text string
    86.199.226.61 Saturday, February 4, 2006 text string
    222.82.28.57 Saturday, February 4, 2006 text string
    220.93.66.30 Saturday, February 4, 2006 text string
    59.32.127.192 Saturday, February 4, 2006 text string
    220.188.79.157 Saturday, February 4, 2006 text string
    218.78.185.204 Saturday, February 4, 2006 text string

BAD OUTPUT from your script with shell command in my script (script at end):(:
2 Saturday, February 4, 2006 text string
0 Saturday, February 4, 2006 text string
1 Saturday, February 4, 2006 text string
. Saturday, February 4, 2006 text string
1 Saturday, February 4, 2006 text string
5 Saturday, February 4, 2006 text string
3 Saturday, February 4, 2006 text string
. Saturday, February 4, 2006 text string
1 Saturday, February 4, 2006 text string
7 Saturday, February 4, 2006 text string
. Saturday, February 4, 2006 text string
1 Saturday, February 4, 2006 text string
7 Saturday, February 4, 2006 text string
7 Saturday, February 4, 2006 text string
2 Saturday, February 4, 2006 text string
1 Saturday, February 4, 2006 text string

Am I doing something wrong constructing my list? I’m stumped.

SCRIPT
tell application “Microsoft Entourage”

set deletedMessages to messages in deleted mail folder

-- if there are no messages selected, warn user and quit
if deletedMessages is {} then
	display dialog "Deleted Items folder is empty" with icon 1
	return
end if

set IP_AddressList to "" -- List of IP Addresses to be exported
set AppleScript's text item delimiters to "X-YahooFilteredBulk: "

repeat with theMessage in deletedMessages
	set hdrSource to (the headers of theMessage)
	if (hdrSource contains "X-YahooFilteredBulk: ") then
		set ipAddr to paragraph 1 of text item 2 of hdrSource
		--set IP_AddressList to IP_AddressList & {ipAddr & (ASCII character 13)}
		set IP_AddressList to IP_AddressList & {ipAddr}
	end if
end repeat

set theFile to (path to desktop folder recipient type string) & "IP Addresses.txt"
repeat with the_IP in IP_AddressList
	do shell script "printf \"" & the_IP & "\\t" & date string of (current date) & space & "text string" & "\\n\" >>" & (quoted form of POSIX path of theFile)
end repeat

end tell

sdrenker - just while you are troubleshooting your script - you may want to hard-code the file reference.
ie: change

set theFile to (path to desktop folder as string) & "IP Addresses.txt"

to:

set theFile to "Macintosh HD:Users:xxxxx:Desktop:IP Addresses.txt"

As far as your list - what Jaques said should do the trick - but if you’re still having issues - put a

return IP_AddressList

after your repeat loop and then look at the results log - just so you can see if the list you’ve built looks the way it’s supposed to. If it doesn’t - you need to fix that first. It should look like a list ie: “201.153.17.177”, “218.61.140.54”, “219.146.109.23”}

Let us know how you make out.

I finally got it after the kind help and assistance everybody provided. Thank you very much.

I also had to add an “& return” after the string construction in the repeat loop to get proper record delimiting in the text file.

As a long-time VBA and VB.net programmer and FileMaker scripter, I sometimes have trouble with AppleScript syntax.

Steve

Hi sdrenker,

I’m using Jaguar, but you can try this:


set t to "201.153.17.177
218.61.140.54
219.146.109.23
60.11.195.165
83.19.206.156
222.80.237.157
86.199.226.61
222.82.28.57
220.93.66.30
59.32.127.192
220.188.79.157
218.78.185.204"
set p_list to paragraphs of t
set new_list to {}
repeat with this_p in p_list
	set d to date string of (current date)
	set s to "text string"
	set new_t to (this_p & tab & d & tab & s)
	set end of new_list to new_t
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
try
	set delimited_text to new_list as string
	set AppleScript's text item delimiters to astid
on error err_mess
	set AppleScript's text item delimiters to astid
	display dialog err_mess
	return
end try
-- write delimited text to file
set u_path to "/Users/kel/Desktop/DelimitedText.txt"
set u_command to "touch " & (quoted form of u_path)
do shell script u_command
set file_spec to (u_path as POSIX file)
set ref_num to (open for access file_spec with write permission)
try
	set eof ref_num to 0
	write delimited_text to ref_num
	write return to ref_num
	close access ref_num
on error err_mess
	close access ref_num
	display dialog err_mess
	return
end try

gl,

I haven’t read this whole thread carefully, but I think the problem is that the try block is inside the Entourage tell block. By itself, this works for me as it should - tab delimited entries of text.

set IP_AddressList to {"201.153.17.177", "218.61.140.54", "219.146.109.23"}

try
	if IP_AddressList is not "" then
		set theFile to choose file name default name "IP Addresses"
		open for access theFile with write permission
		set eof theFile to 0
		repeat with the_IP in IP_AddressList
			write the_IP & (ASCII character 9) to theFile
		end repeat
		close access theFile
	end if
on error
	display dialog "There was an error saving the selection." buttons {"OK"}
end try