Be aware of possible data loss with TextEdit's Save dialog

Dec 15, '06 07:30:04AM

Contributed by: Anonymous

This exercise is intended less as a hint, though there is one included, than as a demonstration of a potentially destructive OS X behavior that pre-OS X Mac veterans may find shocking.

Part I.

This applies to Cocoa Save dialogs in general, but the behavior of TextEdit's dialog is particularly suited for demonstration purposes. Read this entire section before proceeding. First, create a folder on your Desktop, name it MyImportantStuff, and put some copies of files or folders inside (but seriously -- do not put anything important in there!). Next, launch TextEdit, choose Format > Make Plain Text (assuming yours is set to Rich Text as default), and type a few words into the document. Then choose File > Save. In the dialog, give it the name MyImportantStuff, choose the Desktop as the destination, and make sure the If no extension is provided, use .txt checkbox is not selected. Finally -- and this is important -- hit Return twice quickly, simulating too much caffeine or low batteries in your mouse.

Congratulations, your entire MyImportantStuff folder has now been replaced with a single plain text file! Why did this happen? It happened because of the combined effects of Cocoa apps being allowed to overwrite a folder with a file (perhaps related to the need to create package type files like .rtfd), and the fact that Replace is the default option in the confirmation dialog.

Note that this behaviour is shamefully inconsistent with that of the Carbon Save dialog, which has the safer default of Cancel, and does not seem to allow a folder to be overwritten by a file under any circumstances. The pre-OS X Save dialog (eg. in apps running in Classic) is the most user friendly of all -- if a folder is selected, the Save button changes to Open, simplifying navigation.

You can, however, use this behavior to write executable shell scripts entirely within TextEdit...

Part II.

This is the token hint, with an application of a side-effect of Part I. Open a new plain text document in TextEdit, and enter the line of code below. If you are familiar with shell scripts, any suitable code will do:

osascript -e 'tell application Terminal to display dialog Hello World'
Choose File > Save, click the New Folder button, and create a folder named HelloWorld.command. Press Command-Up-Arrow, give the document the name HelloWorld.command, and hit Return twice. Double-click the resulting HelloWorld.command file in Finder, and it should open, which will display the friendly message.

What happened was that when TextEdit replaced the folder with a file, the file retained the executable bit from the folder. Not that you would want to, but you could create all of your shell scripts using TextEdit alone, never having to venture into the command line to chmod +x your scripts. Since TextEdit can create folders from the Save dialog, Info.plist files (which are plain text), and executable scripts, you could even create simple application packages (.app style wrappers for your shell scripts) using nothing more than TextEdit.

Comments (21)

Mac OS X Hints