There will be no hidden weapons here. In fact, I’m going to go so far as to give away this weapon of mass instruction to any crazy scripter who wants it. That weapon is called text item delimiters and, ladies and land lovers, it is a formidable ally as you will soon see.
Text item delimiters, also known as ‘TIDs’, are one of the most powerful tools in an AppleScripter’s arsenal. Powerful because it possesses two very important attributes: speed and flexibility. Meaning, when performing tasks using text item delimiters, you’ll get the job done faster than an MX missile and it’ll do things to make a Navy Seal green with envy.
TIDs are the key to native AppleScript find-and-replace techniques, gathering of special strings within a larger text file and limitless text cleaning abilities. To put TIDs to work in your own string battles, you mostly need to understand that by working with TIDs, you’re changing with the way AppleScript separates or ‘delimits’ text (aka strings). By doing so, we can wield power of which military dictators around the world would be envious.
Let’s get started. In the first example, we’ll simply declare a string all by itself. (See below) (In most real world situations, you’ll be working with text coming from previous commands like file names or read-in text from a file.)
Next, we jump right into TID commands. In this screenshot we’re declaring AppleScript’s text item delimiters to be the character or characters we plan to alter or eliminate. In this case a space or " “. This command is the essence of TID warfare. By setting AppleScript’s TIDs to something, we are making a fundamental change to how AppleScript behaves as a whole. So instead of separating text items with nothing (”“), AppleScript will now separate strings with an underscore (”_") (Note: By default, AppleScript’s text item delimiters are “”, technically an empty string. Or ‘nothing’, if you prefer.)
The actual text item delimiter, as expressed by AppleScript’s current state, comes into play. In line #3 of our code, we get ‘every text item’ from our original string. This causes the current TID to be expressed by effectively using our unwanted string to ‘find’ all items to either side of a delimiter.
Note that the ‘every text item’ command returns a list. This is a good thing. See result window below. We now have a list of the words ‘Dragon’, ‘Monkey’, ‘Tea’ and ‘Party’, sans the spaces which originally separated these words in our original string. Now, we can stop here, if that’s your desire, and do something else with this list. However, for this initial example, I’m going to take things one step further and show you how we use this to do a replace within the original string.
In the next screenshot, we’re now changing AppleScript’s text item delimiter to another character we would like to serve as the replacement: an underscore. (See line four in the image below.)
Finally, we’ll use line #5 to bring it all home. In this command, we’re simply gathering all items in ‘theItemList’,
but we’re also coercing that result to a string so it all comes back as a single string. See our desired result in the result window pictured here:
Now, if you didn’t necessarily want to replace the space characters in our original ‘myString’ string, but simply wanted to strip out the spaces, you could have set AppleScript’s text item delimiters BACK to an empty string in line #5. See below:
This seems like as good a time as any to address a certain minor controversy which is often debated among experienced scripters. Some say that if one forgets to set TIDs back to their original state (an empty string, “”), this will wreak havoc to your operations/machine later. Some insist they’ve never had an issue and don’t ever worry about it.
Here’s my advice: you’ll never get wounded in the bush by being clean, thorough and prepared. So, go ahead and make a habit of setting AppleScript’s text item delimiters back to their original empty string state after each time in your code where you are manipulating them.
By now you’re surely beginning to see the power which text item delimiters represents. I am always amazed at how many tasks can be performed by them and how quickly it can be done.
I’ve prepared an advanced example which I think really illustrates the incredible power of TIDs. This script reads in Safari’s bookmarks file, which is in XML form. If you look at this file in its raw form, it looks a bit intimmidating, at least to me. The script then parses out all URLs, compiles them into a return-delimited list and creates a new text document in TextEdit containing those URLs. Click here to download the script in text form. Pretty cool, huh?
Well, this concludes this edition of unScripted. I hope you’ll work through the exercises above and seriously consider adding text item delimiters to your AppleScript ruck sack. You’ll be a far better Code Soldier for it.