- Microsoft Word supports regular expressions, so a quick Save As to a .txt file, and a straightforward [a-zA-Z0-9] wildcard expression with Find and Replace, does the trick. However, you have to manually overwrite further scrambled text files.
- Open Office works similarly, but again - you must manually delete a text file full of strings of the character 'a'.
- Use Find and Replace twenty-five times.
I have Pages '09, so in this article I discuss using AppleScript to generate a scrambled text file from that application.
How would I do this by hand?
- Save my Pages document as a plain text file using Save As.
- Run a couple command-line programs to process the text.
- Redirect the output to a "upload_me.txt" file.
- Delete the text file that Pages made.
- (Upload to NaNoWriMo's word-counting thing.)
Let's translate this to AppleScript. I will introduce each section and then give some explanations.
You can find the Pastebin source here, and the script file itself on MegaUpload. I encourage you to compile the script for yourself, or to just run it from the script editor.
You can find the Pastebin source here, and the script file itself on MegaUpload. I encourage you to compile the script for yourself, or to just run it from the script editor.
First, we ask the user to choose a .pages document with choose file. The activate keyword shifts focus to the file chooser dialog. We get the name and path of the chosen Pages document, and we return if the user cancels out. Note that the directories are separated by colons, and the presence of the word alias. AppleScript formats paths in two ways, and this is one. The alias path is formatted as an HFS path.
The file chooser repeats until a valid Pages document is chosen. Fortunately, even if a document is simply named "test," internally the file is recognized as "test.pages."
After we harvest the file information, we tell Pages to save the document as a plain text file, and we run cat and sed to process the text proper.
I tried redirection > to a file after sed, but I received error message -10004, or that a "privilege violation" occurred. A Technical Note (referenced below) gives some advice on granting admin-level access, but I'm creating a text file. So I decided to use TextEdit. This is the power of scripting: chaining application abilities together into an automated process.
There are more types than just SLDocumentTypePlainText. There's also
- SLDocumentTypeMSWord,
- SLDocumentTypePDF,
- SLDocumentRichText, and
- SLDocumentRichTextBundle.
We name the new text file by using the ampersand operator to concatenate the file path and name to the ".txt" extension. The POSIX path specifier gives us the path with '/' as the directory separator. This is the second way paths are formatted in AppleScript. If you pass an HFS path to a Unix tool like cat, it will return the error "No such file or directory."
Pages doesn't receive the activate command, but doesn't need to because its job is to simply Save As and close a file. The open command not only opens a document, but returns the document's id. This is very useful; exploit this as much as possible.
With the scrambled text after cat/sed stored in the scramble_text variable, we can now use TextEdit's features to generate a text file.
Question: at runtime, can an AppleScript variable store a string consisting of 50,000 words? I used a Pages document with 101,233 words spanning 278 pages consisting of 431,954 characters (excluding spaces), and the script took about three seconds.
Pages doesn't receive the activate command, but doesn't need to because its job is to simply Save As and close a file. The open command not only opens a document, but returns the document's id. This is very useful; exploit this as much as possible.
With the scrambled text after cat/sed stored in the scramble_text variable, we can now use TextEdit's features to generate a text file.
Question: at runtime, can an AppleScript variable store a string consisting of 50,000 words? I used a Pages document with 101,233 words spanning 278 pages consisting of 431,954 characters (excluding spaces), and the script took about three seconds.
Here we tell TextEdit to create a new document, to populate it with the scrambled text, and to save to Desktop. Now a user can visit NaNoWriMo and upload this text file. Successive uses of this script will overwrite the "upload_me.txt" file, so we no longer have to confirm a file overwrite as with Word or Open Office. Error number 0 indicates success.
And there you have it: a script to make your novel one long shout. Try experimenting with AppleScript, and consider signing up for NaNoWriMo! :)
References
And there you have it: a script to make your novel one long shout. Try experimenting with AppleScript, and consider signing up for NaNoWriMo! :)
References
- Basic File Redirection, Shell Scripting Primer [developer.apple.com]
- Aliases and Files, AppleScript Language Guide
- AppleScript Overview
- Technical Note TN2065: do shell script in AppleScript
- AppleScript Error Codes [lists.apple.com]
- Export as RTFD with Pages [macscripter.net]
- Easily Save TextEdit Documents via AppleScript [hints.macworld.com]
- Simple AppleScript: Create a .txt file [forums.macrumors.com]
Update 11/1/2010. Had to use quoted form for the directory when passing in to the shell script. My file was called "Chapter 1.pages," and the script wouldn't work. The links to MegaUpload and to Pastebin have been updated. See screenshot:
No comments:
Post a Comment