Section 10.3. Getting File Information


10.3. Getting File Information

Back in Chapter 5, you learned how to use the Finder to gather information about a filelike its modification date, for example . As it turns out, however, there's a much more powerful command for gathering file information: info for. This command has a number of advantages over scripting the Finder:

  • Info for provides a more complete analysis of files. In addition to telling you a file's name, modification date, and size (all things you can get by commanding the Finder), the info for command lets you get a file's displayed name (how the file name actually looks in the Finder) and name extension (the short string that comes after the period in a file's name), among other useful properties.

Lest you think the info for command can access everything about a file, there are actually a few pieces of information you can get only by commanding the Finder. These include a file's comment (its Finder note, as described on [click here]) and a file's owner (the person who created it). Therefore, until Apple updates the info for command to access the same information as the Finder, you'll have to live with using a combination of both methods to access complete file information.

  • The info for command doesn't require a tell statement directed at the Finder. You'll save two lines of code automatically: tell and end tell.

  • The info for command returns its information as a record. Among other things, this means you can use the info for command on a list of files and end up with a database of all those files' information.

Running the info for command is simple enough. You just tell AppleScript where to find the file in question, and let it rip:

info for alias ":Library:User Pictures:Animals:Butterfly.tif"

That script locates your Library User Pictures Animals Butterfly.tif image and gets the file information about it. AppleScript doesn't have a built-in command for displaying records, though, so you'll have to be satisfied with reading the information in Script Editor's Result pane (Figure 10-2).

Figure 10-2. Since AppleScript doesn't have a choose from record command (or something similar for displaying records in a dialog box), you have to read your records inside Script Editor's Result pane. If you don't see this pane, you can either click the Result button at the bottom of your script's window or press -2.


10.3.1. Useful Properties

You could collect information records for the rest of your life using info for, but it wouldn't do you much good. That's because records are only useful for the information inside themand that's especially true for the info for command, which provides a range of unique file statistics.

Although this chapter mainly explores using info for with files you can just as easily use the command with folders.

Here are some of the most useful properties in an info for record:

  • A file's name property is, of course, the name that would appear if you selected an item in the Finder and chose File Get Info (-I). It's a useful property to use with files you pick in Open dialog boxes, for example, so you can store the file's name for later use in your scriptor just display the name in a dialog box:

--Show an Open dialog box: set selectedFile to (choose file) --Get an information record for the selected file: set infoRecord to (info for selectedFile) --Get the name of the selected file: set fileName to the name of infoRecord --Display the file's name in a dialog box: display dialog "The name of the file you chose was " ¬     & fileName --Display a dialog box with the filename

  • The kind property tells you what type of file you're dealing with. Possibilities include TIFF Document (an uncompressed image file), MP3 Audio File (a compressed piece of music [Table 8-1]), or Microsoft Word document. If you want to know at a glance what sort of files you have, checking this property is the easiest way to find out:

set selectedFile to (choose file) set infoRecord to (info for selectedFile) --Find what kind of file you selected: set fileType to the kind of infoRecord --The file's kind --Display the file's kind in a dialog box: display dialog "Congratulations! You just chose a " ¬     & fileType

  • The default application property tells you what program is designated to open the file in question. Rather than just telling you the program's name, though, this property tells you the program's pathwhich is a little excessive if you just want to know whether your Peanut.txt file will open in TextEdit or Microsoft Word, for example.

  • Still, if you want to find out just the name of the program that's designated to open a particular file, you can use this script:

set selectedFile to (choose file) set infoRecord to (info for selectedFile) --Find the program that's supposed to open selectedFile: set defApp to the default application of infoRecord --Get an information record for that program: set programInfoRecord to (info for defApp) --Get the name of that program: set programName to the name of programInfoRecord --Display that program's name in a dialog box: display dialog ¬     "The default program to open that file is " ¬     & programName

For a complete list of information record properties, open the Standard Additions dictionary (Sidebar 3.5) and examine File Commands Classes File Information.

Although there are tons of file properties AppleScript can access, you can only check these propertiesyou can't change them. If you want to edit a file's name, for example, you can't use the info for command; instead, you'd have to command the Finder directly.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net