Section 5.1. File Path Boot Camp


5.1. File Path Boot Camp

The one thing AppleScript can't save you from is the fact that files are essentially geeky things. Mac OS X's Unix heritage, while great news for programmers, also means that old Mac fans have to adapt to a few new file conventionshow to name files, what programs to open them with, and so on. Therefore, before you jump headfirst into controlling files with AppleScript, there are a few things you should know:

  • In Mac OS X, files should always have a file extension. A file extension is a short abbreviation added after a period in a file name. Microsoft Word files, for example, end in .doc, while sound files often end in .mp3 or .aiff.

    To Mac OS X (and Windows, for that matter) a file extension reveals what kind of information a file holds. In many cases, a file extension also tells Mac OS X which program should open a file: .doc files open in Microsoft Word, while .psd files open in Photoshop. (Of course, certain types of files can open in several different programs; .jpg files, for example, can open in just about any image-viewing program on the planet.)

    As a general rule, folders should not have file extensions. The exceptions are bundleslittle folders that masquerade as files (Sidebar 2.5), like the .key files that Keynote produces.

    To see what's inside in a bundle, Control-click the bundle in the Finder and select Show Package Contents from the shortcut menu. In the new window that appears, you can sift through the files that comprise the package, discovering, for example, that Keynote "files" are actually made up of dozens of smaller files.

    Up to Speed
    Path Notation

    As described in Section 5.1, a path is a Unix-esque way of describing where a file or folder resides on your hard drive. When you want to specify the lowest level of your hard drive, you simply specify the Unix path / (a single forward slash). Similarly, when you want to refer to an item inside your hard drive, you must begin the item's path with a forward slash.

    However, when specifying a path, folders must also end in a forward slash. That means the path to your Applications folder would be /Applications/ (the first slash to tell Mac OS X to look in your hard drive, and the last slash to tell Mac OS X that Applications refers to a folder).

    When you refer to a file, however, you omit the trailing slash. The path to your Library Fonts Times New Roman file, therefore, would be /Library/Fonts/Times New Roman, with slashes after Library and Fonts (since they're folders) but no slash after Times New Roman (since it's a file).

    When you want to refer to your Home folder, you have two choices. You can specify the folder the normal way, by typing /Users/yourUsername/ (substituting your actual username for yourUsername, of course). Or you can use the convenient Unix shortcut (~/ ), which tells Mac OS X "substitute the actual path to my Home folder here".

    If you want to refer to a file on a disk besides your startup disk, you have to begin your path with /Volumes/. Just follow that with the name of the disk and another slashlike /Volumes/Backup Drive/ for a disk named Backup Driveand the path now refers to your specified disk.

    And something to note if you come from the Windows world: in places where you would have formerly used a backslash (\) in a path nameto identify folders, for exampleuse a forward-slash now. It's just one more instance of how Windows is, well, backward.


  • Although certain programs don't require file extensions, it's still a good idea to use them. That way, if you ever need to send a file to a Windows user, you won't get back an angry email asking you to resend the file with an extension so your recipient can actually open it.

  • A path is a string that tells you how to get to a certain file or folder. Each item in path is separated by a forward-slash (/) in Mac OS Xa by-product of your computer's Unix heritage. That means the path to your Home Desktop folder would be /Users/yourUsername/Desktop/, while the path to your copy of TextEdit would be /Applications/TextEdit.app.

    When you want to play with a path in AppleScript, you can use special type of information called a POSIX file. (POSIX is nerd lingo for "portable operating system interface," which basically means that file paths can be used anywhere, on any computer that supports the POSIX standard. To learn more about POSIX, you can read up on it online at www.satimage.fr/software/en/file_paths.html.) To get the path to your Desktop folder, for instance, you'd write the following, replacing yourUsername with your actual one-word username:

    POSIX file "/Users/yourUsername/Desktop/"

    Still, you'll find that most commands (like choose file, for presenting an Open dialog box [Section 5.6]) use the alias type to refer to files. The alias format separates each folder in a path with a colon, rather than a forward slash. To get the alias to your Desktop folder, for example, you'd write this:

alias ":Users:yourUsername:Desktop:"

Of course, you should replace yourUsername with your actual username. If you don't know what your username is, you can look it up in System Preferences Accounts; you'll find your username in the Short Name field.

  • You can open any file or folder with the open command directed at the Finder. For example, to open Library Desktop Pictures Aqua Blue.jpg (the image that appears behind Mac OS X's login dialog box), you could write:

tell application "Finder"     activate --Bring the Finder forward     open POSIX file "/Library/Desktop Pictures/Aqua Blue.jpg" end tell

You'll notice a couple of oddities when you run this AppleScript. First off, the open POSIX file statement gets changed to open file. Then, all the forward slashes are converted to colons, and AppleScript inserts the name of your hard drive at the beginning of the path string. None of these changes affect what your code actually does; AppleScript just makes these changes so it understands what you're asking it to do.

  • If you prefer to write your code using the more common alias type, you could rewrite the previous script as follows:

    tell application "Finder"     activate     open alias ":Library:Desktop Pictures:Aqua Blue.jpg" end tell

    Either way you write the script, the Aqua Blue image opens and shows up on your screen.



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