AppleScript: Accessing Clipboard From the Finder

We're trying to get information from the clipboard in AppleScript. The following AppleScript works with the Scriptable Text Editor.

tell application "Scriptable Text Editor"
activate
select first word of window 1 -- there's text in window 1
copy
set WordOne to clipboard as text
display dialog WordOne
end tell

However, trying something similar in the Finder does not work. We manually copied the name of a folder, then tried to write a script that would display the contents to the clipboard. Although selecting "show clipboard" from Edit menu shows text on clipboard, the following script does not work, the clipboard appears empty:

tell application "Finder"
set ClipBoardData to clipboard as text
display dialog ClipBoardData
end tell

What gets displayed is "" (nothing). If I go to the Scriptable Text Editor and tell it the same thing, it displays the folder name.
There are several AppleEvent Suites, such as Text, Database, Telephony, Finder, and so on that are defined and generally agreed upon by developers. Manipulation of the Macintosh clipboard is covered in the Miscellaneous Apple Event Suite. In that suite are defined the ways that applications should implement Cut, Copy and Paste, undoing and redoing operations, creating Edition Manager publishers, initiating and ending transactions, and other miscellaneous actions. Not all applications support all of the actions in this suite and so do not implement these functions. Some applications implement the suite partially or under the guise of their own custom suite.

The Scriptable Text Editor did not implement the complete Miscellaneous Suite, but Scriptable Text Editor does implement Cut, Copy and Paste as part of what they call the Scriptable TE suite. The Finder did not implement the Miscellaneous Suite or Cut, Copy and Paste as a subset anywhere. This is why your AppleScript gives desired results with Scriptable Text Editor, but not with the Finder.


Jon Pugh saw the need to extend the capabilities of AppleScript back in the days before the Scriptable Finder became available. He has released a package of "Free for noncommercial use" Scripting Additions known as Jon's Commands. The extended commands include:

clipboard info - get a list of data on the clipboard
set the clipboard to - put data on the clipboard
the clipboard - get data from the clipboard

This particular set of commands is so popular that Jon separated the clipboard extensions into a stand alone Scripting Addition called clipboard. This scripting addition can be found on the internet.

Scripting the Finder in Mac OS 8.5

Beginning with Mac OS 8.5 it is possible to manipulate data in the Clipboard with the Finder.

The scripting addition file called "Standard Additions" that's part of Mac OS 8.5 has commands for setting the contents of the clipboard, getting the contents of the clipboard and getting information about the contents of the clipboard.

Examples of how to write AppleScripts that do this are available in Mac OS 8.5's AppleScript help under the topic: "Using the clipboard."

Published Date: Feb 18, 2012