Posted on 11 May 2008 at 17:35

Permalink to the post entrySwamped

My sincere apologies for the lack of posts. I’ve been swamped with litigation lately. I should be back with lessons on scripting FileMaker soon. I also plan on increasing the frequency of the posts because of the long delay.

Please stay tuned.

Posted on 21 April 2008 at 7:36

Permalink to the post entryApplication Scripting

AppleScript shines at bossing around your applications. Not only can you use AppleScript boss around individual applications, you can also use it as a conduit to transfer data from one application to another. A common example involves transferring data from a database application such as FileMaker Pro to a word processor such as Microsoft Word. You might also transfer data from a database to a spreadsheet application and then to your word processor. Or read data from a text file into a spreadsheet to produce a report or into Timeline to produce a timeline. As you can imagine, the possibilities are endless. (Update: Pete Summerill, the Mac Litigator, has produced a script that combines Adobe Acrobat Pro and Journaler. If you use these apps, take a look.)

In the next few posts, we’re going to build a database in FileMaker Pro and learn how to script it using FileMaker Pro’s AppleScript dictionary. We’ll create a simple database with some fields and use AppleScript to access those fields. Once we have the data in AppleScript, we can send it to another application. We’ll send our data to Microsoft Word 2004 to create a letter.

Administrative: I’d like to ramp up the frequency of the posts, but I don’t want to move too quickly so as to leave new scripters behind. I’d like to hear your feedback regarding the frequency of the posts. Would you like to see more posts more often or do you like the current one post per week rate? Please leave your thoughts in the comments.

(more…)

Posted on 13 April 2008 at 18:54

Permalink to the post entryReadin’ and Writin’

In our last lesson, we learned about file objects. While it’s helpful to know the properties of a file, you often want to read the information from inside the file or write data to the file. AppleScript provides support for reading and writing to a file in the Standard Additions osax:

File Read Write Dictionary

Let’s create a new file using AppleScript, then write some data to it. We’ll then read the data from our new file. Here’s the code for creating a new file to which we can write data:

set f to open for access file ((path to desktop as text) & "test.txt") with write permission
write "This is a test" to f
close access f
Let’s take a closer look at it. Midway into Line 1, we tell AppleScript to create a new file on the desktop named “test.txt”. The shortcut path to desktop returns an alias to the desktop. Here, we coerce the alias to text so that we can append the name of our file, here, “test.txt”. Remember, too, that if we want to create a new file, we must use the word file explicitly because alias objects must already exist in the file system for us to use them. Note that if you have an existing alias object, you can still write to and read from that alias object.

Once we have our new file, we have to open the file for access with permission to write to it. These commands do not launch any applications to open the file. Instead, AppleScript opens the file internally as a stream of data. You must also explicitly tell you that you want to write to the file or else AppleScript will raise an error. When AppleScript opens the file, it returns a file reference number. We set this reference number to the variable f (for file) so that we can more easily reference our file later on.

In Line 2, we tell AppleScript to write the text “This is a test” to our file.

In Line 3, we close access to our file. You should always explicitly close access to any open files or you may have trouble later.

To read data from a file, we simply tell AppleScript to read from our file:

read file ((path to desktop as text) & "test.txt")

There you have it. You can now read and write data to files. Please note that this is most effective for text files, including XML files, but not binary files such as Word files. We’ll explore how to read and write Word files and other binary files in later lessons.

(more…)

Posted on 6 April 2008 at 14:23

Permalink to the post entryFiles and Aliases

In this episode, we’ll continue working with elements of the Finder. As we’ve learned, the Finder contains different elements such as disks and folders. Disks contain folders so we say that folders are elements of disks. Inside folders, we might find other folders or we might find files. Files, then, are elements of folders and share many of the same properties of folders such as name and size.

Files are also classes of the Finder. In fact, the Finder has several different classes of files. The two main classes are file and aliases. The difference between these two types of objects is slight, but crucial. An alias refers to an existing file and can maintain the link to the existing file even if you move or rename the file. This is the advantage of using aliases over files. A file object represents a specific file at a specific location on your system. If you need to reference an existing file, you should use alias because the Finder will maintain the link to that file if the file gets moved or renamed.

To create a new file in the Finder, we tell the Finder to make a new file with a specific name, just as we did with folders:

tell application "Finder" to make new file with properties{name: "test.txt"}

This code makes a new file named “test.txt” on your desktop. Now that we have an existing file object, we can get its alias and boss it around:

tell application "Finder" to set testAlias to alias "Macintosh HD:Users:yourusername:Desktop:test.txt"

Before running this code, make sure you have the correct path to the “test.txt” file. For example, if you changed the name of your main hard disk from “Macintosh HD” to “MacMac” and your username is “macdaddy”, then the path would be “MacMac:Users:macdaddy:Desktop:test.txt”. Run this code and look in the Result window. You should see the word “alias” in blue as the first word. This tells you that you now have an alias object. Remember, you can only use alias objects to refer to existing file objects. If you want to create new files, the you need to use the “make new file with properties” command.

In our next lesson, we’ll look at how to read data from and write data to a file.

(more…)

Posted on 30 March 2008 at 19:18

Permalink to the post entryMaking Things

When we last discussed the Finder, we looked at the different elements that make up the Finder’s object model: disks, folders, and files. As you recall, each element may contain zero or more other elements and those elements have properties that we can access. For example, a disk element contains zero or more folders and those folders have a name property that we can access. We can also make new elements. Here’s a quick example:

tell application "Finder"
    make new folder with properties {name: "My Folder"}
end tell

Open Script Editor, type in this code, and press the “Run” button. You should see a new folder named “My Folder” on your desktop. You might use this code to generate a new folder when you open a new matter. Let’s take a closer look at the code.First, we include our tell statement to tell AppleScript that we want to boss around the Finder. Then we send the Finder a message, “Hey, Finder, make a new folder and name the folder ‘My Folder’”. To let the Finder know about our properties, we send the Finder a record of properties. A record is an unordered collection of labeled properties. Here, we set the labeled property, “name”, to “My Folder”. We could have set other properties, too. To discover what properties we can set, we need to look at the Finder’s Dictionary. Here’s the Dictionary entry for the folder element:

Dictionary entry for Folder element

Whoa! There’s no properties listed! But the dictionary does list the elements—in the inheritance chain. Click on the word “container” to see the properties of containers:

Container element of Finder

OK, so we see a few properties, but several are not available and the others are designated as “r/o”. This means that the properties are “read-only” which means we only read the value of the property, can’t set it when we make something new. Let’s look further up the inheritance chain. Click on the word “item” in the first line of the dictionary entry:

Item element of Finder

Ah, here we go. Look at the first property listed. It’s our “name” property that we set when we told the Finder to create our new folder. If you wanted to set more properties, you would add the labeled properties to your record that you send to the Finder:

make new folder with properties {name: "My Folder", label index: 1}

In the next post, we’ll look at files. (more…)