Sunday, June 25, 2017

#1 2006-01-27 08:02:31 pm

Bill Cheeseman
Administrator
Registered: 2006-06-29
Posts: 158

Parallel Processing

Parallel Processing

Prior to Mac OS 8, the Finder was not multi-threaded. As a result, a series of tasks triggered by AppleScript executed in succession, each starting after the previous task finished. Furthermore, computers contained only one processor, and they were much slower than they are today.

The Finder became multi-threaded in Mac OS 8, and of course it and the entire operating system are multi-threaded in Mac OS X. This means that the Finder can simulate performing two or more functions simultaneously. In fact, if you have a multi-processor computer, they really do execute simultaneously. Everything is faster, and it isn't only because processors are faster today.

Nevetheless, you may have to take affirmative steps in your scripts if you want to ensure that they trigger parallel processes simultaneously. The trick is to ignore application responses when telling an application to perform a series of actions. AppleScript normally waits until the application signals that it has completed one task before it moves on to the next task. But if you enclose all of the tasks in an ignoring application responses block, AppleScript will trigger all of them immediately and leave it up to the target application to do the right thing. Here's an illustration.

Make copies of three large files on the desktop and name them "A", "B" and "C", respectively. Very large photos will do nicely, with sizes in megabytes, or even tens of megabytes if you have a very fast computer. Then compile and save two scripts that duplicate these three files from one location to another. In one script, the duplicate commands should simply follow one another in a tell application "Finder" block. The script duplicates the files from the desktop to the startup disk, so open a window on your startup disk where you can watch the copies arrive. When you run the first script, if you have a relatively slow computer you will see the Finder's file copying progress alert once while the first file is duplicated, then it will go away and, after a pause, you will see another progress alert for the next file, and so on. On a fast computer, the progress alerts may never appear if the files aren't large enough, but you'll still see the copies arrive on the startup disk one at a time. Here's the non-threaded script:

Applescript:


tell application "Finder"
duplicate file "A" to startup disk
duplicate file "B" to startup disk
duplicate file "C" to startup disk
end tell

Then drag the three duplicate files from the startup disk to the trash, leaving the originals on the desktop. In the other script, the duplicate commands should be placed in an ignoring application responses block. When you run this script, AppleScript will not wait for the Finder to signal that it is done with one copy before telling the Finder to start the next. Instead, AppleScript will tell the Finder to begin the next copy immediately, and the Finder will launch each copy in a separate thread. On a slow enough machine, you will see several Finder progress alerts come up and once and run simultaneously. Even on a fast computer, you will see the three copies arrive on the startup disk almost simultaneously. Here's the threaded script:

Applescript:


tell application "Finder"
ignoring application responses
duplicate file "A" to startup disk
duplicate file "B" to startup disk
duplicate file "C" to startup disk
end ignoring
end tell


Filed under: Finder

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.064 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)