log webpage contents to a log file

Im looking for a way to copy the text of a webpage (or source of the weboage, does not matter) into a text file.

the script needs to do the following:

  1. go to webpage #1
  2. create a text file in the desktop and copy the text of the webpage
  3. go to webpage #2
  4. open the created text file and ADD the text of the second webpage (it needs to add the text, not replace it)

Any ideas???

This might get you started (lightly tested):

set ptd to path to desktop as text
set fPath to ptd & "source"
set fPathTemp to ptd & "source temp"
set urls_ to {"http://www.apple.com", "http://www.macintouch.com", "http://scriptbuilders.net"}
set urlCount to count urls_

tell application "URL Access Scripting" to download item 1 of urls_ to file fPath

repeat with i from 2 to urlCount
	tell application "URL Access Scripting" to download (item i of urls_) to file fPathTemp replacing yes
	set readTemp to read file fPathTemp
	my write_to_file(readTemp, fPath, true)
end repeat

tell application "Finder" to delete alias fPathTemp

on write_to_file(this_data, target_file, append_data) -- courtesy of Apple Computer
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

Thanks a lot. It worked perfectly. (I just swiched the “url access scripting” to internet explorer and worked better)

I was checking the code and i think I understand it all, but I cant realize whats the role of the last lines:

on write_to_file(this_data, target_file, append_data) -- courtesy of Apple Computer 
   try 
      set the target_file to the target_file as text 
      set the open_target_file to ¬ 
         open for access file target_file with write permission 
      if append_data is false then ¬ 
         set eof of the open_target_file to 0 
      write this_data to the open_target_file starting at eof 
      close access the open_target_file 
      return true 
   on error 
      try 
         close access file target_file 
      end try 
      return false 
   end try 
end write_to_file

because the data is allready inside the “source” file so what are those lines for? and whats the ¬ character for?

thanx again

The last lines are known as a sub-routine, also known as a handler. If you visit unScripted, you’ll find a couple of articles that explain what they are and how they work. If you still have questions after reading the articles, feel free to ask follow-up questions and we’ll do our best to answer them. While not particularly useful in this situation (the code in routine was called only once and could have been included in-line), sub-routines are very useful when you want to use a common block of code more than once within a script.