Creating a Drag-and-Drop Script Application


Although the sample script you created is useful, it would be more useful as an application with an icon on your desktop. Then you could drag files that you wanted to sort into folders and drop them on the application’s icon. This would cause the application to run and move the files to their appropriate folders. You wouldn’t have to open Script Editor every time you wanted to sort files into folders, and you could sort more than one file at a time. Applications respond to this type of drag and drop Apple Event, and so can Apple Scripts.

You already know that AppleScript can save a script as an application. With a little extra work, you can make an application with drag-and-drop capability so that you can simply drag files to it to choose them.

Retrieving dropped files

Remember that when you drop a set of icons on an application in a Finder window, the Finder sends that application an Open Documents message that includes a list of the files you dropped on the icon. This message is sent to all applications, even to applications that you create yourself with AppleScript.

You need to tell your script to intercept that event message and retrieve the list of items that were dropped on to the application icon. Place the following statement at the beginning of your script:

on open itemList

Now enter the following statement at the end of your script:

end open

This on open statement enables the script to intercept an Open Documents event message and puts the message’s list of files in a variable named itemList. The end open statement helps AppleScript know which statements to perform when the open message is received. Any statements between the on open and end open statements are performed when the script receives an Open Documents event message.

Save this script by choosing the Save As command from the File menu. From the Format pop-up menu in the Save As dialog, choose the Application option. (You may want to save the script on the desktop, at least for experimental purposes.) If you switch to the Finder and look at the icon of the application you just created, you can see that the icon includes an arrow, which indicates that the icon represents a drag-and-drop application. The application has this kind of icon because its script includes an on open statement.

Processing dropped files

The script won’t be fully operational until you make a few more changes. As the script stands, it places the list of files in a variable, but it doesn’t do anything with that information. If you dropped several files on the application now, the script would still display a dialog asking you to pick a file and then quit, having accomplished nothing.

First, you need to eliminate the script statements that obtain the file to be processed from a dialog. Delete what now are the second and third lines of the script (the ones beginning with the words choose and copy) and replace them with the following:

repeat with x from 1 to the number of items in the itemList copy item x of the itemList to thisFile

Between the end tell and end open statements, which are the last two lines of the script, enter the following statement:

end repeat

Figure 23-9 shows the complete sample script modified for drag-and-drop operation.

click to expand
Figure 23-9: This script application processes items dropped on its icon.

Save the script so that your changes take effect and then switch back to the Finder. You now have a drag-and-drop application that you can use to move certain types of files to specific folders.

Using a repeat loop

In the modified script, AppleScript repeatedly performs the statements between the repeat and end repeat statements for the number of times specified in the repeat statement. This arrangement is called a repeat loop. The first time AppleScript performs the repeat statement, it sets variable x to 1, as specified by from 1. Then AppleScript performs statements sequentially until it encounters the end repeat statement.

In the first statement of the repeat loop, variable x determines which file spec to copy from variable itemList to variable thisFile. The rest of the statements in the repeat loop are carried over from the previous version of the script.

When AppleScript encounters the end repeat statement, it loops back to the repeat statement, adds 1 to variable x, and compares the new value of x with the number of items that were dragged to the icon (as specified by the phrase the number of items in the itemList). If the two values are not equal, AppleScript performs the statements in the repeat loop again. If the two values are equal, these statements are performed one last time, and AppleScript goes to the statement immediately following end repeat. This is the end open statement, which ends the script.

Extending the script

Anytime you want the application to handle another type of file, open the script application in Script Editor, add a conditional that covers that type of file, and save the script.

The script in its drag-and-drop form now no longer functions if you double-click on it. You can modify the script to add in this functionality by placing a copy of the original script at the end of the end open statement.

Tip

You can’t open a script application by double-clicking it because doing so causes the application to run. To open a script application in Script Editor, choose File Open in Script Editor or drop the script application on the Script Editor icon in the Finder.




Mac OS X Bible, Panther Edition
Mac OS X Bible, Panther Edition
ISBN: 0764543997
EAN: 2147483647
Year: 2003
Pages: 290

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net