Save file groups in TextWrangler

Oct 29, '07 07:30:03AM

Contributed by: Anonymous

I've been working with large file sets in TextWrangler recently. There is usually one window with 20 to 30 open documents. Lastly, these documents are on a server. It's a real pain to have to go back on and open each file by hand everytime I close the window, especially since they are in all over the place in different directories. It would be nice to be able to save a set of files and then come back and reopen them all later. So, I wrote two AppleScripts for doing just that.

This AppleScript saves the URL locations (local and remote) of all the documents in a single window.

tell application "TextWrangler"
  set alldocs to every text document of text window 1
  set allfiles to {}
  set str to ""
  repeat with adoc in alldocs
    if is FTP of adoc then
      set ftpinfo to FTP Info of adoc
      set murl to URL of ftpinfo
    else
      set murl to URL of adoc
    end if
    set end of allfiles to murl
    set str to str & "||" & murl
  end repeat
  set filename to (choose file name default name "TextWrangler Docs.txt")
  
  set saver to make new text document
  open saver opening in new_window
  set text of saver to "--docsaver do not edit this line" & str
  save saver to filename
  close saver
end tell

This next script is for opening up the above-created file, and restoring all your files into a single window. If any of the files are remote, then the script will sense that and ask you for your password to that server. Obviously if the file set is on two different servers, then the one password won't work (unless they are the same password!). If the password is wrong, then TextWrangler will just give you a password window for each file. The only problem is that when you type in your password, it doesn't show the dots so you can read your password right there. So just be careful.

tell application "TextWrangler"
  set filename to (choose file)
  open filename opening in new_window
  set allfilesstr to text of text document 1
  close text document 1
  set allfiles to rest of my splitit(allfilesstr, "||")
  set hasFTP to false
  
  repeat with afile in allfiles
    if afile contains "ftp" then
      set hasFTP to true
    end if
  end repeat
  set ps to ""
  if hasFTP then
    set ps to text returned of (display dialog "Type your password of the server your files are on" default answer "")
  end if
  if length of allfiles is greater than 0 then
    set afile to item 1 of allfiles
    set afile to my snr(afile, ":@", (":" & ps & "@"))
    open location afile
  end if
  set allfiles to rest of allfiles
  repeat with afile in allfiles
    set afile to my snr(afile, ":@", (":" & ps & "@"))
    open location afile
  end repeat
end tell

on snr(the_string, search_string, replace_string)
  tell (a reference to my text item delimiters)
    set {old_tid, contents} to {contents, search_string}
    set {the_string, contents} to {the_string's text items, replace_string}
    set {the_string, contents} to {the_string as Unicode text, old_tid}
  end tell
  return the_string
end snr

on splitit(the_string, search_string)
  tell (a reference to my text item delimiters)
    set {old_tid, contents} to {contents, search_string}
    set {arr, contents} to {the_string's text items, old_tid}
  end tell
  return arr
end splitit

This AppleScript features a search (or find) and replace function as well as a string splitter that creates a list out of a string that is split by a specified string.

[robg adds: I haven't tested these scripts.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071024121225545