# Section 1.3. Calculation and Repetition

### 1.3. Calculation and Repetition

Computers are good at calculation and repetition . Humans, on the other hand, are liable to calculate inaccurately, and all the more so in the face of repetitive activity, which can make them careless, bored, and angry . Calculation and repetition on a computer should be performed by the computernot by a human.

Here's an example straight off the Internet, where someone writes : "I want to rename a whole lot of image files based on the names of the folders they're in." One's eyes glaze over at the prospect of doing this by hand. Yet with AppleScript, it's a snap. The task would make a good dropleta little application, written with AppleScript, with a Finder icon onto which you can drop files and folders you want processed . (More details appear in "Applet and Droplet" in Chapter 3 and "Applets" in Chapter 27.) Here's the AppleScript code for such a droplet; you drop a folder or folders onto its icon in the Finder, and it renames all items in each folder using that folder's name followed by a number:

```on open folderList
repeat with aFolder in folderList
if kind of (info for aFolder) is "Folder" then
renameStuffIn(aFolder)
end if
end repeat
end open
on renameStuffIn(theFolder)
set ix to 0
tell application "Finder"
set folderName to name of theFolder
set allItems to (get every item of theFolder)
repeat with thisItem in allItems
set ix to ix + 1
set newName to folderName & ix
set name of thisItem to newName
end repeat
end tell
end renameStuffIn
```

The parameter folderList tells us what was dropped onto the droplet. We process each dropped item, starting with a sanity check to make sure it's really a folder. If it is, we give each item in the folder a new name based on the folder's name along with a number that increases each time.

### 1.4. Reduction

Even when a task doesn't involve repetition and calculation, it may involve many steps. If you can get AppleScript to perform many or all of those steps, you reduce the number of steps you have to perform. This can make for a noticeable improvement in your relationship with your computer, even if you perform this task fairly infrequently. Another advantage of reduction is that you no longer have to remember a sequence of steps; your AppleScript program remembers it for you.

Here's an example involving URLs. Often, working in some application, I see a URL that I'd like to "go to" in the approprate manner. If it's an http URL, my default browser should open and fetch that page. If it's an email address, my email program should create a new message to that addressee. In some applications you can just click a URL and the right thing happens, but many applications provide no such facility, so I have to resolve the URL manually. This means I must look at the URL and decide on the appropriate helper program; then I select and copy the URL; then I somehow start up the helper program; finally, I paste the URL into the appropriate location. In a browser, I must hit Return afterwards, in order to go to that URL; in an email program, I must create a new message first, in order to have something to paste into. This doesn't sound like very many steps, but it's all very annoying, especially in comparison to those applications where the right thing just happens with a single click.

The solution is an AppleScript program. I've assigned it a keyboard shortcut (ways of doing this are discussed in Chapter 2), so the procedure is this: select and copy the URL, then press the keyboard shortcut. That's a significant savings in time and trouble. Here's the script:

```set theProc to (get path to frontmost
application as Unicode text)
tell application "Finder"
activate
delay 1 --

give time for clip to convert from Classic

set theURL to (get the clipboard as string)
end tell
ignoring application responses
try
open location theURL
end try
end ignoring
activate application theProc
```

The switch to the Finder is to force the clipboard contents to convert themselves to a usable form (and the delay is to give this time to happen); this seems to be needed particularly when working in a Classic application. At the end of the script I switch back to the application I was using at the outset. The heart of the script is the open location command, which does the "right thing" with the URL.