(* To get this to work on your machine you will need to set local_domain and site_directory at the very least. The next thing to look for would be any Apache Aliases that may be set up... Then add any extra default index doc's you might use. I've set up index.html, index.htm and index.php. There are plenty of things to improve please feel free. I'd like to hear about where I'm being messy as my knowledge of applescript needs work. There's a couple of places where arrays should be used but I didn't get round to figuring out exactly how their equivalent (lists) worked in applescript. *) set local_domain to "simon.local" set site_directory to "Simons HD:Users:simon:Sites" 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" -- Find out if Safari has a window open 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 -- Get the URL of the page currently displayed in Safari's front window set current_URL to URL of document of front window end if end tell --things could get messy with escaped chars... catch an obvious one.. who know's about the others? set current_URL to SaR(current_URL, "%20", " ") --check to see if the url is alread a file:/// set file_check to subString(current_URL, 1, 8) if (file_check is equal to "file:///") then -- thing just got a little simpler set current_URL to SaR(current_URL, "file:///", "") set volume_check to subString(current_URL, 1, 8) if (volume_check is equal to "Volumes/") then -- the file is on a mounted volume so trim that out as applescript doesn't recognise the directory set current_URL to SaR(current_URL, "Volumes/", "") else -- the file should be on the startup disk set current_URL to ((path to startup disk as text) & current_URL) end if else set check_local to SaR(current_URL, local_domain, "iama.localurlsonoworries") -- check_local WON'T be equal to current_URL if it is a local domain if (check_local is equal to current_URL) then display dialog "This isn't the local domain I'm looking for:" & local_domain & "Would you like to save a new file on the desktop and open it in TextMate?" buttons {"Cancel", "OK"} default button 2 -- saveSource() -- return end if -- convert server Aliases... check httpd.conf for details set current_URL to SaR(current_URL, local_domain & "/books", local_domain & "/misc/books") set current_URL to SaR(current_URL, local_domain & "/php", local_domain & "/misc/php") set current_URL to SaR(current_URL, local_domain & "/photos", local_domain & "/helvector/httpdocs/photo") -- check for querystrings and remove set current_URL to removeTrailing("?", current_URL) -- check for page anchors and remove set current_URL to removeTrailing("#", current_URL) -- replace the http://domain with the site_directory set current_URL to SaR(current_URL, "http://" & local_domain, site_directory) end if -- convert all slashes to colons and isolate the parent (remember fileToShow could actually be a Directory) set current_URL to SaR(current_URL, "/", ":") set current_DIR to removeTrailing(":", current_URL) -- store the file and directory for use by -- you might want to add a try here and then report back any errors in a dialog set fileToShow to alias (current_URL) set dirToShow to alias (current_DIR) (* Watch that we don't accidentally open a directory as TextMate will automatically open this as a project, if there's a lot of files it can take a long long time. So when we find a directory try and find a default document to open then check with the user as to what they want to do. *) if (item -1 of current_URL's items is equal to ":") then -- set tmp to isReal(current_URL, "index.html") if (tmp is true) then set append to "index.html" else set tmp to isReal(current_URL, "index.htm") if (tmp is true) then set append to "index.htm" else set tmp to isReal(current_URL, "index.php") if (tmp is true) then set append to "index.php" else set append to false end if end if end if if (append is false) then display dialog " I've not been able to find any of the following index.html index.htm index.php Would you like to open the directory as a project?" buttons {"Cancel", "OK"} default button 2 else display dialog " Would you like to open the directory as a project or the " & append & " file? " buttons {"Cancel", "File", "Project"} default button 3 -- if Cancel is pressed then the script's automatically exited. set the butt_result to the button returned of the result if (the butt_result is equal to "File") then set fileToShow to alias (current_URL & append) end if end if end if tell application "TextMate" activate open fileToShow end tell -- SUBS on isReal(theURL, append) try set fileToShow to alias (theURL & append) on error return false end try return true end isReal 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 on lastIndexOf(str, longstr) set lof to (offset of ("" & reverse of str's items) in ("" & reverse of longstr's items)) if lof is 0 then return 0 --> not found return -(count str) + 1 - lof end lastIndexOf on subString(the_string, start_index, end_index) return "" & (get characters 1 thru end_index of the_string) end subString on removeTrailing(last_instance_of, the_string) set qs to lastIndexOf(last_instance_of, the_string) if (qs is not 0) then return subString(the_string, 1, qs - 1) else return the_string end if end removeTrailing -- write to 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 -- Save Scource to Desktop and open with TextEdit on saveSource() tell application "Safari" -- 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 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) -- -- might not be in the right scope !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! write_to_file(html_source, html_file, false) -- tell application "TextMate" activate open html_file end tell end saveSource