Simple Scripts: Drag and Drop

  1. Talking Clock
  2. Simple Scripts
  3. Other Things You Might Do

When you drop a file onto a program, that program gets a message explaining what happened. You can let your AppleScripts accept those messages with the “open” handler. Make a script with nothing but:

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

end tell

display dialog theName

end repeat

end open

Save this as an Application called “Uploader”. Then, drop a few files on it. It should name each file in a dialog box.

You could use this feature to “tell” an FTP program such as Fetch or Interarchy to upload files automatically to your web site. When we do our upload, we’ll want to make sure we don’t upload things accidentally that we don’t want. Usually, we just want to upload images and text files (html files are usually text). Change your Uploader script to:

property allowedTypes : {"JPEG", "TEXT", "HTML"}

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

copy the file type of item anItem to theType

end tell

if theType is not in allowedTypes then

display dialog theType & " (" & theName & ") is not allowed for upload."

end if

end repeat

end open

We created a property (very much like a variable) that contains our allowed types. If the file is not an allowed type, we display a warning message. This allows you to add that type to the list of allowed types if you wish to.

Finally, we need to tell Fetch to upload the file. You may wish to look at Fetch’s dictionary.

property allowedTypes : {"JPEG", "TEXT", "HTML"}

--replace the following with your host’s information

property remoteHost : "hostname.example.com"

property remotePass : "password"

property remoteUser : "username"

property basePath : "/path/to/upload/"

on open (theItems)

repeat with anItem in theItems

tell application "Finder"

copy the name of item anItem to theName

copy the file type of item anItem to theType

end tell

if theType is in allowedTypes then

copy basePath & theName to fullPath

tell application "Fetch"

activate

store anItem host remoteHost path fullPath user remoteUser password remotePass with sftp

end tell

else

display dialog theType & " (" & theName & ") is not allowed for upload."

end if

end repeat

end open

The same script should work with Interarchy.

We’re using an “if” structure in this script. The “if” structure performs everything between “if” and “end if” depending on certain conditions. Often, there will be one set of statements before an “else” for if the condition works out, and another set of statements after an “else” for if the condition does not work out.

If “theType” is in the list of “allowedTypes”, we work with Fetch. Otherwise, we inform the user that this is not an allowed type.

  1. Talking Clock
  2. Simple Scripts
  3. Other Things You Might Do