When I write the credits for a movie, I prefer to write the credits out as a list in my text editor before I paste them into iMovie. The only problem with doing this is that I need to copy and paste the list line by line. Fortunately, I have found a way to make this much easier using, of course, AppleScript.
Before we start, you might want to download the final script.
To clarify, the goal of this tip is to take a list of items (separated by carriage returns) and paste each item into different text boxes in iMovie (although, you could easily use it for other things.)
Step 1: Create a list of items you want to paste into text boxes. Break up each item in the list with a carriage return. (If you have items that are multiple paragraphs, the script can be adjusted to fit that.)
Step 2: Open up Script Editor.
Step 3: First, we need to identify the list. So type:property the_list : "
and paste in the list. Add a second double-quote to close it off. If there are any double-quotes in your list, escape them by adding a "\" right before it.
Step 4: Now that we've identified the list, we need to get every text item of the_list. To do this, use AppleScript's text item delimiters. AppleScript's TID's tell AppleScript where to break up the text. The default is "" or nothing. So, to split up the_list, we'll set AppleScript's TID's to a carriage return: set AppleScript's text item delimiters to "
"
Next, we'll break up the_list:set the_text_list to every text item of the_list
This will give us a list of every paragraph in the_list without any carriage returns. Note that whenever we change AppleScript's TID's, we need to set them back again:set applescript's text item delimiters to ""
Step 5: Now it gets pretty easy. We need to get the first item on the list, make the clipboard be that item, then get rid of the first item. (This way, the new first item is the old second item.) We need to keep in mind error checking, so we'll use something like this:set the_text_item to item 1 of the_text_list
try
set the_text_list to items 2 thru -1 of the_text_list
on error err
log err
set the_text_list to "End of list."
end try
set the clipboard to the_text_item
Here's a line by line rundown of this snippet:
Line 1: get the first item on the list. (This is always item 1.)
Line 2: when we use try, if something breaks then the script will keep rolling. (See line 4.)
Like 3: This deletes the first item of the_text_list by rebuilding the_text_list to not include it.
Line 4: we use "on error err" when we use "try". When something happens, the script will skip everything else in the "try" statement and go straight to the "on error" statement. "err" in this case is the text of the error.
Line 5: we log err (the text of the error) in case something goes wrong. When we log something, it displays the error in the "Event Log" section at the bottom of the window.
Line 6: The reason we get an error is because there is only one item in the_text_list. When this happens, it's the end of the list. So we simply set the_text_list to "end of list". Next time we run the script, this is what the clipboard will be set to.
Line 7: the obligatory "end try" statement.
Line 8: this is how you set the clipboard's contents. Notice that "the_text_item" is from line 1 of this snippet.
Step 6: Now all we need to do is make the_list be the new list. To do this, type:set AppleScript's text item delimiters to "
"
set the_list to the_text_list as string
set AppleScript's text item delimiters to ""
Once again, we are using AppleScript's TID's. However, this time, we are turning a list into a string. When we do this, AppleScript's TID's are put between each item in the list. Because the_list is a property, its contents will be remembered each time you run the script. (So if we use AppleScript to change the_list to "hello" then it will stay "hello" until we recompile the script at which time it will become whatever the text of the script says it is.)
Step 7: To test out your script, we'll add one more line:return the clipboard
This ends the script and displays whatever the clipboard is in the "Result" section at the bottom of the window. When we run the script multiple times, this should change to each item of the list. Hit Cmd-R to run it. You should be able to cycle through each item on the list. If you want to run it again, hit Cmd-K to recompile the script. Here's what my final script looks like:
Step 8: We've spent all this time making the script, now all we need to do is plug it into a keyboard macro program like Spark. (If you don't have a keyboard macro program already, Spark is a really easy program for use with AppleScript's.)
Step 9: That's it! You should now know how to use AppleScript's text item delimiters, set the contents of the clipboard, and basic manipulation of lists.
[Download the final script]
4/13/09
Quickly Paste Long Lists Using Applescript
Posted by
Oliver
at
Monday, April 13, 2009
Labels: AppleScript
Subscribe to:
Post Comments (Atom)


0 comments:
Post a Comment