FREN

#FF00AA


4 sep. 2009

Timestamped backups in TextWrangler

TextWrangler 3.0 removed the option to hit a File menu command and create a backup of the file you’re editing, replacing it with a global preference of either keeping backups of every single edit you make, or no backup at all.

That’s damn annoying, because I relied on that option as my own primitive CVS, making a backup of a file whenever I was about to make important edits. (Like I said, primitive, I know, but it’s convenient.)

So here’s how you can restore that functionality, using AppleScript. Go to the Scripts menu (the little scroll icon near Help — I think it’s enabled by default?) and select “Open Scripts Folder”; make a copy of an existing script, rename it to something like “00) Make Timestamped Backup.scpt” (the “00)” won’t be displayed, it serves to order the scripts in the menu), double-click the file to open it in Script Editor, and replace its contents with the following:

tell application "TextWrangler"

set fileAlias to (file of document 1 of window 1)set filePath to POSIX path of (fileAlias as text)

end tell

 

tell application "Finder"

set fileName to name of fileAlias

set fileExtension to name extension of fileAlias

set fileBasePath to text 1 thru -((length of fileExtension) + 2) of filePath

 

do shell script "cp " & fileBasePath & "." & fileExtension & " " & fileBasePath & "." & "`date +’%Y-%m-%d.%H-%M-%S’`" & "." & fileExtension

end tell

 

tell application "TextWrangler"

display dialog "Backed up " & fileName & "." buttons {"Ok"} default button 1

end tell

(Make sure you don’t mangle the assortment of double quotes, single quotes and backquotes on that “do shell script” line.)

Now your TextWrangler has a “Make Timestamped Backup” command, ready to use, that will do just that and not ask you any questions; it will just display a confirmation dialog when it’s done. You can also use the Scripts palette (Window menu, Palettes, Scripts) to give it a hotkey.

Want to know when I post new content to my blog? It's a simple as registering for free to an RSS aggregator (Feedly, NewsBlur, Inoreader, …) and adding www.ff00aa.com to your feeds (or www.garoo.net if you want to subscribe to all my topics). We don't need newsletters, and we don't need Twitter; RSS still exists.

Legal information: This blog is hosted par OVH, 2 rue Kellermann, 59100 Roubaix, France, www.ovhcloud.com.

Personal data about this blog's readers are not used nor transmitted to third-parties. Comment authors can request their deletion by e-mail.

All contents © the author or quoted under fair use.