Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Save file groups in TextWrangler Apps
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[10,534 views]  

Save file groups in TextWrangler | 4 comments | Create New Account
Click here to return to the 'Save file groups in TextWrangler' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Save file groups in TextWrangler
Authored by: expiring_frog on Dec 01, '07 06:59:56PM
Thanks for the scripts :). They do indeed work (but see below) and are very useful.

There is one small caveat: My hard drive is called "Macintosh HD" (the Tiger/Leopard default), and the saved filelist contain entries of the form:

file:///Macintosh%20HD/Users/myname/...

For some reason TW can't open filenames of this form (I don't know what happens for folks with different HD names). If you have the same problem, add the following line right after each of the two calls to snr in the main tell block:

set afile to my snr(afile, "/Macintosh%20HD", "")

(Replace with the name of your HD as applicable.)

[ Reply to This | # ]

Save file groups in TextWrangler
Authored by: expiring_frog on Dec 06, '07 07:57:50PM

Or even better (this will hopefully work for any HD name):

set afile to my snr(afile, "file:///", "file:///Volumes/")

[ Reply to This | # ]

Save file groups in TextWrangler
Authored by: stevenstromer on Sep 03, '08 02:13:40PM

Unfortunately, I am trying to work on a large number of files on a server whose password contains an @ symbol (I know this isn't the best idea, but I'm not in a position to change it.). When attempting to retrieve the files via SFTP, this causes an error, as it is not encoded properly within the script's code at this point:

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

Can anyone tell me how to encode/escape(?) the characters in the password properly, as held in the variable 'ps' so that the @ symbol will not cause a problem? I'm just not too familiar with AppleScript! It would be a GREAT help.

Thanks!



[ Reply to This | # ]
Save file groups in TextWrangler
Authored by: stevenstromer on Sep 04, '08 11:34:48AM

Solved my own problem. I removed the code querying the user for a password, returning responsibility for querying to the operating system. Revised script 'Open Bookmark Collection.scpt' is as follows:

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, "||")

if length of allfiles is greater than 0 then
set afile to item 1 of allfiles
open location afile
end if
set allfiles to rest of allfiles
repeat with afile in allfiles
open location afile
end repeat
end tell

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

Thanks again to the original script writer. You have made my life worlds easier!



[ Reply to This | # ]