< Previous PageNext Page > Hide TOC

Drag and Drop Suite

This chapter describes the terminology in the Drag and Drop suite, which is available starting in AppleScript Studio version 1.2. This suite defines terms for working with drag and drop, including the drag info class to provide information about a drag, as well as events you use to handle dragging, tracking, preparing for a drop, and concluding a drop. For related information, see the pasteboard class.

Initial drag-and-drop support was added in AppleScript Studio 1.2. It provides the ability for various user interface elements to receive drag events. It does not, however, allow for initiating drag operations (other than those already supported by Cocoa classes).

Additional support for drag and drop was added in AppleScript Studio 1.4:

Among the classes that support drag and drop are button, clip view, color well, combo box control,image view, matrix, movie view, popup button, progress indicator, scroll view, slider, stepper, tab view, text field, text view, and view.

For an object to respond to any of the drag-and-drop event handlers (described in “Events”), you must register the drag types that the object can accept. You do this with the register command, using its drag type parameter to supply a list of the supported pasteboard drag types. Possible pasteboard types are listed with the pasteboard class.

The drop event handler is the only required handler for supporting dropped data in AppleScript Studio. However, see the conclude drop handler for information on providing drag and drop for text view and text field objects.

Terminology

The classes and events in the Drag and Drop suite are described in the following sections:

For enumerated constants, see “Enumerations.”

Classes

The Drag and Drop suite contains the following class:

drag info
Plural: drag infos
Inherits from: item
Cocoa Class: ASKDragInfo

Represents information and data for the current drag operation.

A drag info object is passed to each of the drag-and-drop event handlers described in “Events.”

The most useful property of the drag info class is the pasteboard property. It contains the data for the drag, which your application can extract and use in the drop or other event handlers.

Properties of drag info objects

In addition to the properties it inherits from the item class, a drag info object has these properties:

destination window
 Access:read only
 Class:window
 the destination window for the dragging operation
image
 Access:read only
 Class:anything
 the image being dragged
image location
 Access:read only
 Class:point
 not supported (through AppleScript Studio version 1.4); the location of the image being dragged; the location is returned as a two-item list of numbers {left, bottom}; see the bounds property of the window class for information on the coordinate system; for changes introduced in AppleScript Studio version 1.4, see the main discussion for the application class, as well as the coordinate system property of that class
location
 Access:read only
 Class:point
 not supported (through AppleScript Studio version 1.4); the current location in the destination window for the dragging operation, as a two-item list of numbers {left, bottom}; each window has its own coordinate system, with the origin in the lower left corner; see the bounds property of the window class for more information on the coordinate system; for changes introduced in AppleScript Studio version 1.4, see the main discussion for the application class, as well as the coordinate system property of that class
pasteboard
 Access:read only
 Class:pasteboard
 the pasteboard that contains the drag data
sequence number
 Access:read only
 Class:integer
 not supported (through AppleScript Studio version 1.4); the unique identifier for the dragging operation
source
 Access:read only
 Class:item
 the source of the dragged data
source mask
 Access:read only
 Class:enumerated constant from Drag Operation
 defines the type of drag; not supported until AppleScript Studio version 1.4


Events supported by drag info objects

This class is not accessible in Interface Builder, and you cannot connect any event handlers to it.

Examples

The following example shows one way to examine a drag info object in a drop event handler. This handler returns false, canceling the drop, if the pasteboard of the drag info does not contain the “string” type. If it does contain that type, the handler uses the string data from the pasteboard to set the title of the object the data was dropped on and returns true to complete the drop operation.

on drop theObject drag info dragInfo
    set dropped to false
    if "string" is in types of pasteboard of dragInfo  then
        set title of theObject to contents of pasteboard of dragInfo
        set dropped to true
    end if
    return dropped
end drop
Version Notes

The image location, location, sequence number, and source mask properties in this class are not supported, through AppleScript Studio version 1.4.

The drag info class was added in AppleScript Studio version 1.2.

Events

Objects in the Drag and Drop Suite support the following events (an event is an action, typically generated through interaction with an application’s user interface, that causes a handler for the appropriate object to be executed). To determine which classes support which commands, see the individual class descriptions.

conclude drop

Called to complete a successful drop operation.

This handler is only called if both the prepare drop and drop event handlers were successful. You can use this handler, for example, to clean up any state that was set or changed in the prepare drop handler.

The drop handler is the only required handler for supporting dropped data in AppleScript Studio.

Both the text view and text field classes have built-in support for dropped text, provided automatically by the Cocoa classes they are based on (NSTextView and NSTextField). If that’s all the support for dropped text your application needs for objects of these types, you don’t need to do anything extra. However, an application that wants to handle data that is dropped on a text view or a text field, and not allow the default support to handle it, should take these steps:

Syntax
conclude drop reference required
 drag info drag info required
Parameters
reference

a reference to the object whose conclude drop handler is called

drag info  drag info

the information about the drag operation

Examples

When you connect a conclude drop handler to an object in Interface Builder, AppleScript Studio supplies an empty handler template. Your handler can take any necessary steps to deal with the conclusion of the drop that it didn’t take care of in its drop handler. For example, the handler might clean up any state that was set or changed in the prepare drop handler.

on conclude drop theObject drag info dragInfo
    (* Statements to deal with the concluded drop. *)
end conclude drop

See also the description above for this class.

Version Notes

The conclude drop event handler was added in AppleScript Studio version 1.2.

drag

Not supported (through AppleScript Studio version 1.4).

Support for this event handler is planned for a later version of AppleScript Studio.

Syntax
drag reference required
 drag info drag info required
Parameters
reference

a reference to the object whose drag handler is called

drag info  drag info

the information about the drag operation

Version Notes

The drag event handler was added in AppleScript Studio version 1.2, although it does nothing in that version.

drag entered

Called when a user drags the registered type of data into the bounds of the object.

Your application may not need a drag entered handler, as the drop handler is the only required handler for supporting dropped data in AppleScript Studio.

Syntax
drag entered reference required
 drag info drag info required
Parameters
reference

a reference to the object whose drag entered handler is called

drag info  drag info

the information about the drag operation

Examples

When you connect a drag entered handler to an object in Interface Builder, AppleScript Studio supplies an empty handler template. The theObject parameter refers to the object for which a potential drag has entered the object’s bounds. The dragInfo parameter provides access to a drag info object that contains all pertinent information for the drag operation. Your handler can take any necessary steps, such as providing visual feedback that it can accept information from the drag.

on drag entered theObject drag info dragInfo
    (* Statements to deal with the drag entering. *)
end drag entered
Version Notes

The drag entered event handler was added in AppleScript Studio version 1.2.

drag exited

Called when a user drags the registered type of data out of the bounds of the object.

Your application may not need a drag exited handler, as the drop handler is the only required handler for supporting dropped data in AppleScript Studio.

Syntax
drag exited reference required
 drag info drag info required
Parameters
reference

a reference to the object whose drag exited handler is called

drag info  drag info

the information about the drag operation

Examples

When you connect a drag exited handler to an object in Interface Builder, AppleScript Studio supplies an empty handler template. The theObject parameter refers to the object for which a potential drag has exited the object’s bounds. The dragInfo parameter provides access to a drag info object that contains all pertinent information for the drag operation. Your handler can take any necessary steps, such as providing visual feedback that it is no longer waiting to accept information from the drag.

on drag exited theObject drag info dragInfo
    (* Statements to deal with the drag exiting. *)
end drag exited
Version Notes

The drag exited event handler was added in AppleScript Studio version 1.2.

drag updated

Called when a user moves a registered drag type within the bounds of the object.

Your application may not need a drag updated handler, as the drop handler is the only required handler for supporting dropped data in AppleScript Studio.

Syntax
drag updated reference required
 drag info drag info required
Parameters
reference

a reference to the object whose drag updated handler is called

drag info  drag info

the information about the drag operation

Examples

When you connect a drag updated handler to an object in Interface Builder, AppleScript Studio supplies an empty handler template. The theObject parameter refers to the object in whose bounds a drag with a registered type has moved. The dragInfo parameter provides access to a drag info object that contains all pertinent information for the drag operation. Your handler can take any necessary steps, such as providing visual feedback as to where the drag data might be inserted. Note however, that through AppleScript Studio version 1.3, you cannot access the location property of a drag info object, so the ability to determine an insertion point is limited.

on drag updated theObject drag info dragInfo
    (* Statements to deal with the drag updating. *)
end drag updated
Version Notes

The drag updated event handler was added in AppleScript Studio version 1.2.

drop

Called when a user has dropped data with a registered data type onto an object.

You return false from this event handler to cancel the drop operation; otherwise you must return true to signal success for the drop operation.

You can examine the pasteboard property of the drag info parameter to obtain the data for the drop in the requested format. If you use the prepare drop handler to verify that the data you need is present, you will know that data is available when the drop handler is called.

The drop handler is the only required handler for supporting dropped data in AppleScript Studio. However, see the conclude drop event handler for information on providing drag and drop for text view and text field objects.

Syntax
drop reference required
 drag info drag info required
Parameters
reference

a reference to the object whose handler is called to receive the dropped data

drag info  drag info

the information about the drag operation

Examples

The Drag and Drop sample application (distributed with AppleScript Studio, starting with version 1.2) demonstrates how various objects can accept dragged data. The following drop handler, from the file Text.applescript in that application, shows how to accept dragged text data in a text field.

The drop event handler is called when the appropriate type of data is dropped onto the object. All of the pertinent information about the drop is contained in the drag info object (passed in the drag info parameter). In this case, the handler just checks the pasteboard of the drag info object for the “string” data type. If it is present, the handler uses it to set the contents of the passed text field (from the theObject parameter).

on drop theObject drag info dragInfo
    -- We are only interested in the "string" data type
        -- If that type is present, set the contents of the text
        --  field to the contents of the pasteboard
    if "string" is in types of pasteboard of dragInfo  then
        set string value of theObject to contents of pasteboard  of dragInfo
    end if
 
    return true
end drop

By default, the preferred type property for a pasteboard is “string”, which is why the handler above doesn’t set the preferred type. To explicitly set the preferred type of a pasteboard (in this example, the “general” pasteboard) to “string” before getting data from the pasteboard, you could use the following statements:

set preferred type of pasteboard "general" to "string"
if "string" is in types of pasteboard "general"  then
    set myString to contents of pasteboard "general"
Version Notes

The drop event handler was added in AppleScript Studio version 1.2.

prepare drop

Called when a user has dropped the dragged data onto the object.

You return false from this event handler to cancel the drop operation; otherwise you must return true to continue the drop operation.

You can examine the pasteboard property of the drag info parameter to determine, for example, whether it contains data in the format you want.

Your application may not need a prepare drop handler, as the drop handler is the only required handler for supporting dropped data in AppleScript Studio.

Syntax
prepare drop reference required
 drag info drag info required
Parameters
reference

a reference to the object whose handler is called

drag info  drag info

the information about the drag operation

Examples

When you connect a prepare drop handler to an object in Interface Builder, AppleScript Studio supplies an empty handler template. The theObject parameter refers to the object for which a drop is about to take place. The dragInfo parameter provides access to a drag info object that contains all pertinent information for the drag operation. Your handler can do any necessary preparation, such as making sure the necessary data is present for the drop operation to take place. The handler should return false to cancel the drop operation or true to allow it.

on prepare drop theObject drag info dragInfo
    (* Statements to prepare for a drop.
        In this example, only allow drop if string info available.  *)
    if "string" is in types of pasteboard of dragInfo  then
        return true
    else
        return false
    end if
end prepare drop
Version Notes

The prepare drop event handler was added in AppleScript Studio version 1.2.



< Previous PageNext Page > Hide TOC


© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-10-31)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.