[ Home Page | AppleScript | HyperCard | Misc. ]

Ben's AppleScript Snippets

This page holds a number of AppleScript examples that may prove useful to AppleScript learners.

Reading a Text file Into an AppleScript Variable

This is one of those things that you'll probably do a lot. It's not too hard in AppleScript but there are a few tricks. Note that there are a wide variety of ways to read from a file, such as reading in a certain number of characters, or until a certain character is encountered. I generally prefer to read the entire file into a variable and work from there for speed and simplicity. Here's how to do it:

set theFile to (choose file with prompt "Select a file to read:" of type {"TEXT"})
open for access theFile
set fileContents to (read theFile)
close access theFile

Writing an AppleScript Variable to a Text File

Another common task. Dead simple in HyperCard, but there are a few tricks in AppleScript. To open and write to a file, use this:

set newFile to new file with prompt "Output file:" default name "My New File"
open for access newFile with write permission
-- if you want to overwrite an existing file use set eof of newFile to 0 first.
write "something useful" to newFile
close access newFile

Drag 'n' Drop Applet Shell

Here is the skeleton of a script designed to function as a 'droplet' (a drag 'n' drop AppleScript application). When double-clicked the script begins executing from the first line. When files/folders are dropped onto the application icon the "on open" handler is called. This example simply passes on the object to "DisplayName", which displays the path name of each selected file or folder (and its entire contents). You can download this script as an AppleScript application here.

displayName(choose file with prompt "Select a file:") --if double-clicked
return -- not needed, but shows that the script stops here when "run"

on open of finderObjects -- "open" handler triggered by drag'n'drop launches
  repeat with i in (finderObjects) -- in case multiple objects dropped on applet
    displayName(i) -- show file/folder's info
    if folder of (info for i) is true then -- process folder's contents too
      tell application "Finder" to set temp to (entire contents of i)
      repeat with j in (temp)
        display dialog j as string -- example of doing something with each item
      end repeat
    end if
  end repeat
end open

Finding the Location of a File

This is a useful process. Often you will want to take a file, process it somehow and create a new file in the same location. There are two general ways to get the location of a file.

First you can use a one line "tell" statement to the Finder to get the file or folder's 'container'. This is very compact, but slow if you are processing a lot of files. Here is an example:

set pathToMe to (choose file with prompt "Display path of:")
display dialog GetParentPath(pathToMe)

on GetParentPath(theFile)
  tell application "Finder" to return container of theFile as text
end GetParentPath

You can also take the file's path name and use delimiters to determine the location component of a file's path. This is fast but complicated. Here is an example:

display dialog GetParentPath((choose file with prompt "Display path of:"))

on GetParentPath(myPath)
  set oldDelimiters to AppleScript's text item delimiters -- always preserve original delimiters
  set AppleScript's text item delimiters to {":"}
  set pathItems to text items of (myPath as text)
  if last item of pathItems is "" then set pathItems to items 1 thru -2 of pathItems -- its a folder
  set parentPath to ((reverse of the rest of reverse of pathItems) as string) & ":"
  (* The above line works better than the more obvious set parentPath to ((items 1 thru -2 of pathItems) as string) & ":"
     because it will not return an error when passed a path for a volume, i.e., "Macintosh HD:", but rather will return ":"
     indicating the desktop is the root of the given path. Andy Bachorski <andyb@APPLE.COM> *)
  set AppleScript's text item delimiters to oldDelimiters -- always restore original delimiters
  return parentPath
end GetParentPath

© Ben Lawson 1998. Feedback to ben@nobleswan.com. Click here to return to my home page.