Return results in text file?

Ok so i have a simple enough ping script i threw together


set IP_address to ""
set pings to ""
set dialog to display dialog "Enter IP" default answer "192.168.100.11" with title "IP"
set the IP_address to the text returned of dialog
set dialog1 to display dialog "Number of ping's to attempt" default answer "10" with title "Ping! Pong!"
set the pings to the text returned of dialog1
set ping to (do shell script "ping -c " & pings & " " & IP_address)

Now how can i get it to return the results in a text file, for instance save the return results into a text file in

~/Logs/pingresults.txt

Thanks for the help in advance!

tell application "TextEdit"
	make new document at front
	set text of document 1 to "10.224.01"
	# you'll substitute your variable to for the address above.
	set path of document 1 to ((path to library folder from user domain as text) & "Logs:ip.log")
	# fill in the correct path to the log above.
end tell

Hi jzjad,

Something like this:


set t to "hello"
set file_ref to ((path to home folder) as string) & "Logs:pingresults.txt"
set ref_num to (open for access file file_ref with write permission)
try
	write t to ref_num starting at (eof)
	write return to ref_num
	close access ref_num
on error err_msg
	close access ref_num
	display dialog err_msg
end try

gl,

Ok that works, in making the file, and naming it right, of course, i know how to do that, but how to i get it to insert the results from after running the application?
Just like apple script does when running it from the program it gives you the results of the program after its ran.

Just change t to ping


set ping to "hello"
--
set file_ref to ((path to home folder) as string) & "Logs:pingresults.txt"
set ref_num to (open for access file file_ref with write permission)
try
	write ping to ref_num starting at (eof)
	write return to ref_num
	close access ref_num
on error err_msg
	close access ref_num
	display dialog err_msg
end try

I forgot, you might want to use linefeed instead of return, because unix returns newlines:


set ping to "hello"
--
set file_ref to ((path to home folder) as string) & "Logs:pingresults.txt"
set ref_num to (open for access file file_ref with write permission)
try
	write ping to ref_num starting at (eof)
	write linefeed to ref_num
	close access ref_num
on error err_msg
	close access ref_num
	display dialog err_msg
end try

**Edit fixed i simply renamed the file name it was saving as, and it lets me run it as many times as i want, but now i have the question if i can have it name the file with and increasing number from 1 behind it?
Example:
First time ran: pingresults1.txt
Second time ran: pingresults2.txt
So on and so forth.

I should test this before sleeping, but it looks good to me:


property ping_count : 0

set ping to "hello"
set ping_count to ping_count + 1
-- get a mac style path (i.e. colon delimited)
-- first set an index number up to 9999
set this_index to (text -4 thru -1 of ("000" & ping_count))
set file_name to "pingresults" & this_index & ".txt"
set file_path to ((path to home folder) as string) & "Logs:" & file_name
set ref_num to (open for access file file_path with write permission)
try
	write ping to ref_num starting at (eof)
	write linefeed to ref_num
	close access ref_num
on error err_msg
	close access ref_num
	display dialog err_msg
end try

This needs to be run as an application for the ping count to increment. There are other ways to run it though.

gl,

Thanks, for now i have it set with the ip, and results

 set file_ref to ((path to desktop) as string) & IP_address & " pingresults.txt"