Scrambling Text

I had a request to write a script that would take a text document with multiple frames and scramble its text. There was a desire to share the document regarding its layout, but not the actual content. While the text could have been replaced by lorem ipsum, this would have been time-consuming, and various styles would have to be recreated.

Here is the initial version of this script, which scrambles the text in a selected frame, which must of course be a text frame. The logic begins with the way that Autoquote takes apart the content of a frame, character by character, but skipping over the characters you're not looking for, leaving everything else, including the markers for character styles, intact.

Here we wish to change all aphabetical characters, but leave numbers, punctuation, carriage returns, and so on unchanged. It uses the python module random to generate a new random small letter and capital letter each time a new alphabetical character is found, then modifies according to the case of the found letter. This isn't like a cryptographic code using some algorithm for the process, it's as random as the random module can generate, so this will not be reversible. This isn't exactly true – there are some times when you can use Undo after running the script to see what I mean. Once you save the document and reopen, it's truly irreversible.

replacetext_v3.py
The next phase was to try not to need to select a text frame, but simply alter all the text in all the text frames and all pages of a document. Here is the final result.

I have added a warning message and an ability to opt out of the script in case you haven't saved the original.

replacetext_v2.py
This is another version which only modifies a page at a time.

Side Effects
The biggest one seems to be that in most if not all cases your text will take up more lines than it did before scrambling. This is due to the random nature of these changes, in which case the occurrence of wide letters is much more common than in the anyone's original text. There could certainly be the possibility of considering character frequencies and making adjustments, but this would reduce randomness somewhat.

Also note that as written this changes only the limited Latin character set, a-z, and A-Z, but you should be able to alter this script to include other character sets. One might also scramble numbers to random numbers if that seemed desirable.