tell application "System Events" -- Find out if Safari is running if not (exists (application process "Safari")) then activate set dialogText to "Safari is not running." display dialog dialogText buttons {"OK"} default button 1 return end if end tell tell application "Safari" if not (exists (front window)) then activate set dialogText to "No windows are open in Safari." display dialog dialogText buttons {"OK"} default button 1 return else -- This fails because the file doesn't alread exist. I don't think there's an equivalent to "Save As" there's a save as command but it refers to the doc type - ie unicode. --set new_file to alias ((path to desktop as text) & "monkey.html") --save the document of front window in new_file -- save the source in a var (so you can pass it to another app?) set html_source to source of document of front window as text set html_name to name of document of front window as text end if end tell -- it's possilbe that the title of the page contains colons so strip them out (or convert) set html_name to SaR(html_name, ":", "-") -- We shold check an awful lot more here ie, file type, length of file name, does it already exist (if it does it'll get overwritten by default so it's dangerous) -- instead we'll do a dirty hack.. set html_name to html_name & ".html" -- set html_file to (((path to desktop) as text) & html_name) write_to_file(html_source, html_file, false) tell application "TextMate" activate open html_file end tell -- Search and Replace on SaR(aText, asearch, areplace) try set oldAstid to AppleScript's text item delimiters set AppleScript's text item delimiters to asearch set a_text to every text item of aText set AppleScript's text item delimiters to areplace set a_text to (every text item of a_text) as string set AppleScript's text item delimiters to oldAstid return a_text on error return false end try end SaR -- write the file on write_to_file(this_data, target_file, append_data) 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