-- Set the configuration and read the relevant comments before installing this script. To install, paste this all into Script Editor, edit as needed, and save in ~/Library/Application Support/BBEdit/Scripts. Assign your favorite keyboard shortcut using BBEdit's Scripts palette, which is over in the Window menu. -- Configuration details: THIS IS THE STUFF YOU CHANGE. --Since the folder name has to be hardcoded anyhow, I don't see much point in testing to make sure it exists. Thusly, the script will barf if it's not there. Adjust desired folder name as needed, and make sure it's where we expect it to be before run time. set gbook_folder to (path to home folder as text) & "Documents:Writing:Garbage Book:" --In the future, I may make us silently create the folder, or store the folder as a property and have it ask for the location on first run. Neither of which is at all satisfactory, so it's more likely I'll just leave it as a hard-coded constant. Sorry, world! set gbook_prefix to "" -- in case you want to prefix every page with "gbook" or something. (Which I _thought_ I did, but decided I didn't. Maybe you will, though.) set gbook_stamp to (do shell script "date +\"%Y.%m.%d (%l%p)\"") as text -- Results in something like 2008.11.14 ( 8PM). If you want something different, please to read "man strftime," and be sure to escape any characters that would cause Applescript to hork. set suffix_length to 25 -- The number of characters from the file to append. -- Setup for cleaning the suffix: You probably don't want to change these. property illegalCharacters : {"\\", "/", ":", "*", "?", "\"", "<", ">", "|", "#", "%", "$", ";", "?", " "} ¬ -- this is the list of characters to substitute from inside the filename property substituteCharacter : "_" -- this is the character to substitute for the illegal characters property illegalEnds : {" ", "_", "."} -- this is the list of characters to remove from the start or end of the file -- And here's all the action! tell application "BBEdit" -- Generate the unique suffix so that we can have more than one file a day without making the date call ridiculous. (Plus, I just like knowing something about the contents of the file.) if (count of characters of line 1 of text document 1) < suffix_length then set gbook_suffix to text of line 1 of text document 1 as text else set gbook_suffix to characters 1 thru suffix_length of line 1 of text document 1 as text end if set gbook_suffix to my fixCharacters(gbook_suffix) set gbook_suffix to my fixLast(gbook_suffix) save text document 1 to file (gbook_folder & gbook_prefix & gbook_stamp & " " & gbook_suffix & ".txt") without saving as stationery end tell --These routines all came from http://www.macosxhints.com/dlfiles/fix_illegal_names_scpt.txt, which was written by a MYSTERY PERSON. Thanks, author whose name isn't in the file and whose identity is incredibly difficult to track down! That saves me hella time. on fixEnds(theName) set theName to my fixFirst(theName) -- fix the start of the name set theName to my fixLast(theName) -- fix the end of the name return theName -- pass back the (un)changed name end fixEnds on fixCharacters(theName) repeat with x from 1 to (count illegalCharacters) -- loop through the list of illegal characters if theName contains (item x of illegalCharacters) then -- if this character is in the filename set oldDelims to AppleScript's text item delimiters -- store the current delimiters set AppleScript's text item delimiters to (item x of illegalCharacters) -- set the delimiter to the illegal character set theTextItems to text items of theName -- extract the text without the illegal character as a list of strings set AppleScript's text item delimiters to substituteCharacter -- set the delimiter to be the substitute character set theName to theTextItems as text -- change the list of strings back into a single string set AppleScript's text item delimiters to oldDelims -- change the delimiters back to what they were originally end if end repeat return theName -- pass back the (un)changed name end fixCharacters on fixFirst(theName) set theFirst to first character of theName -- check the first character of the item name if theFirst is in illegalEnds then -- if the first character is in the illegal list set theName to text 2 thru -1 of theName -- remove the first character of the name set theName to my fixFirst(theName) -- make sure we haven't uncovered any more illegals end if return theName -- pass back the (un)changed name end fixFirst on fixLast(theName) set theLast to last character of theName -- check the last character of the item name if theLast is in illegalEnds then -- if the last character is in the illegal list set theName to text 1 thru -2 of theName -- remove the last character of the name set theName to my fixLast(theName) -- make sure we haven't uncovered any more illegals end if return theName -- pass back the (un)changed name end fixLast