edit .html file

I want to edit an html file in applescript and was wondering how to do that. I know that you can use curl if the file is on internet but how do you do it if the file is on your computer.

also you can

set theLink to "[url=http://www.google.com]www.google.com[/url]"

but what if you want to make an html file from scratch, it would mean using quotes in the variables but unfortunately, if you use quotes, it closes the quote and opens the next… does anybody know what to do?

Browser: Safari 412
Operating System: Mac OS X (10.4)

to read a local file, just use:

read (choose file)

and navigate to an HTML file when prompted. To create your own HTML from scratch, try this (note how the quotes are escaped with a backslash):

set the_title to (display dialog "Enter your title:" default answer "A Great Resource" buttons {"OK"} default button 1 with icon 1)'s text returned
set the_html to "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
        \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
	<title>" & the_title & "</title>
</head>
<body>
	This is a <a href=\"http://bbs.applescript.net\">great resource</a>!
</body>
</html>"
set the_file to (((path to desktop) as Unicode text) & "mypage.html")
my write_to_file(the_file, the_html, false)
tell application "System Events" to set the_URL to (get URL of (the_file as alias))
tell application "Safari"
	open location the_URL
	activate
end tell

on write_to_file(the_file, the_data, with_appending)
	set the_file to the_file as string
	try
		set f to open for access file the_file with write permission
		if with_appending = false then set eof of f to 0
		write the_data to f starting at eof as (class of the_data)
		close access f
		return true
	on error the_error
		try
			close access file the_file
		end try
		return the_error
	end try
end write_to_file

Jon

Model: PowerBook 1.25MHz
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

thank you, you managed to understand my confused ramblings

Browser: Safari 412
Operating System: Mac OS X (10.4)

could somebody find a way to make this work without all the strange characters in the outputs?



-- this script downloads stories from [url=http://www.fanfiction.net]www.fanfiction.net[/url]. 
set temp to display dialog "What is the story code?" default answer ""
set code to text returned of temp
set code2 to code
set baseDialog to "http://www.fanfiction.net/s/"
set temp to display dialog "number of pages?" default answer "1"
set i to text returned of temp

set t to 1
try
	tell application "Finder" to make new folder at folder "isilo" of folder "books" of folder "tomcool" of folder "Users" of startup disk with properties {name:code}
	--tell application "Finder" to make new folder at folder "originals" of folder "books" of folder "tomcool" of folder "Users" of startup disk with properties {name:code}
	tell application "Finder" to make new folder at folder "computer" of folder "books" of folder "tomcool" of folder "Users" of startup disk with properties {name:code}
end try
set theTitle to get_TITLE(baseDialog, code)
--display dialog the theTitle
tell application "SEC Helper" to show screen message theTitle

repeat i times
	set urlAddress to baseDialog & code & "/" & t & "/"
	
	set the_HTML to my get_html(urlAddress, true, code, t, i, theTitle)
	set t to t + 1
	tell application "SEC Helper" to show screen message theTitle percentage value ((t - 1) / i) duration 2
end repeat
createIndex(code, theTitle, true, i) --creates an index file
renameFolder(code, theTitle) --to be easier to find themstop


on renameFolder(code, theTitle)
	try
		tell application "Finder" to set name of folder code of folder "isilo" of folder "books" of folder "tomcool" of folder "Users" of startup disk to theTitle
	end try
	
	try
		tell application "Finder" to set name of folder code of folder "computer" of folder "books" of folder "tomcool" of folder "Users" of startup disk to theTitle
	end try
end renameFolder

on get_html(this_URL, save_to_disk, code, t, i, theTitle)
	
	set browser_string to "'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9'"
	set the_HTML to do shell script "curl " & (quoted form of this_URL) & " -A " & browser_string
	set code1a to (offset of "<title>FanFiction.Net : " in the_HTML) + 24
	set code1b to (offset of "</title>" in the_HTML) - 1
	set code1 to text code1a thru code1b of the_HTML
	set code2a to (offset of ");</script>" in the_HTML) + 11
	set code2b to (offset of "</span></div>" in the_HTML) - 1
	set code2 to text code2a thru code2b of the_HTML
	set theCode to "<html><head><title>" & code1 & " page " & t & "</title></head>" & "<body bgcolor=#ffffff>" & code2 & "</body></html>"
	set link to "<p><a href=\"b_"
	set link2 to ".html\">chapter "
	set link3 to "</a></p>"
	set t1 to t - 1
	set t2 to t + 1
	set theCode1 to "<html><head><title>" & code1 & " page " & t & "</title></head>" & "<body bgcolor=#ffffff>" & code2 & "<hr width=\"80%\" />" & link & t1 & link2 & t1 & link3 & link & t2 & link2 & t2 & link3 & "</body></html>"
	
	
	if save_to_disk then
		--do shell script "echo " & (quoted form of the_HTML) & " > ~/books/originals/" & code & "/" & t & ".html"
		--display dialog t
		do shell script "echo " & (quoted form of theCode) & " > ~/books/isilo/" & code & "/b_" & t & ".html"
		do shell script "echo " & (quoted form of theCode1) & " > ~/books/computer/" & code & "/b_" & t & ".html"
		
	end if
	return the_HTML
end get_html

on get_TITLE(baseDialog, code)
	set this_URL to baseDialog & code & "/1/" -- if error in title, it is due to ffnet making page unavailable: change value to 2
	tell application "SEC Helper"
		set the_HTML to do http get this_URL
	end tell
	set theTitleStart to (offset of "<title>FanFiction.Net : " in the_HTML) + 24
	set theTitleEnd to (offset of "</title>" in the_HTML) - 1
	set theTitle to text theTitleStart thru theTitleEnd of the_HTML
	return theTitle
end get_TITLE


on createIndex(code, theTitle, save_to_disk, i)
	set theEnd to "</body></html>"
	set head2 to "</title></head><body bgcolor=\"#ffffff\"><div align=\"center\"><h1>"
	set body to "</h1><p></p>"
	set link to "<p><a href=\"b_"
	set link2 to ".html\">chapter "
	set link3 to "</a></p>"
	set head to "<html><head><meta http-equiv=\"content-type\" content=\"text/html;charset=iso-8859-1\"><title>"
	
	
	set theCode to head & theTitle & head2 & theTitle & body
	repeat with z from 1 to i
		set theCode to theCode & link & z & link2 & z & link3
	end repeat
	set theCode to theCode & theEnd
	
	if save_to_disk then
		do shell script "echo " & (quoted form of theCode) & " > ~/books/isilo/" & code & "/" & "index" & ".html"
		do shell script "echo " & (quoted form of theCode) & " > ~/books/computer/" & code & "/" & "index" & ".html"
	end if
end createIndex


Model: PowerBook 15in 1.5Ghz
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)