Team LiB
Previous Section Next Section

5.6. Picking a File from a Dialog Box

Back on Sidebar 4.2, you learned how to use the display dialog command to present information onscreen, and how to give feedback to your scripts while they're running. The trouble with that command, though, is that you can't choose a file with it. And when you're using Finder commands, you often want to choose a file for your script to work with.

That's where the choose file command comes in. Rather than having to specify an actual file name in your script, choose file uses Mac OS X's standard Open dialog box, letting you pick the precise file you want to work on (Figure 5-8). That way you can choose a different file for your script to operate on each time it runs.

Figure 5-8. When you use the choose file command, you see the same Open dialog box that you see in other Mac OS X programs. There's only one difference: the choose file dialog box shows normally hidden files (like .DS_Store), too. There are some benefits to this feature: you can see all the Unix configuration files that Mac OS X uses, for example. However, if it bothers you to have all those hidden files clogging up your Open dialog box, just add the without invisibles option to the end of your choose file command.


In its purest form, the choose file command can occupy a line all by itselfdisplaying an Open dialog box but doing absolutely nothing else:

choose file

Of course, it won't do much good just to display an Open dialog box on the screen; the real power comes when your script can figure out what file you chose. AppleScript makes this easy, too:

set selectedFile to (choose file)
(*The selectedFile variable now stores an "alias" 
tell application "Finder"
    open selectedFile
end tell

When run, this script presents an Open dialog box, and then opens whatever file you chose. It's not going to win any programming awards, but it's a start.

If you'd like to pick out a folder instead of a file, use the choose folder command. For more information on these commands, check out the Standard Additions dictionary (Sidebar 3.5).

5.6.1. Showing When a File was Created

Admit it: you've got folders that you haven't cleaned out in monthsor maybe even years. You've let your junk accumulate, putting off the day you have to sort through it. Now, using AppleScript, you can finally tell how long it's been sitting around, so you can brag to your similarly procrastinatory friends.

When you script the Finder, you have access to the modification date property for everything on your hard drive. To figure out when a file was modified, therefore, you simply have to tell AppleScript which file you want the information for. The choose file command provides the perfect opportunity to enlighten AppleScript as to your file of interest:

set selectedFile to (choose file)
tell application "Finder"
    set modDate to the modification date of selectedFile
end tell
display dialog "That file was last modified on: " & modDate

Still, this script isn't perfect. For one thing, it doesn't give you any perspective, like how many months ago the file was modified. Instead, it just tells you the date the file was modified, which isn't as easy to interpret at a glance.

One of AppleScript's nice features, though, is that you can subtract one date from another. It's a great way to figure out how long ago a file was modifiedin days, months, or even years. Simply edit your script like this:

--Part 1:
set selectedFile to (choose file)
tell application "Finder"
    set modDate to the modification date of selectedFile
    --Part 2:
    set curDate to the current date
    --Part 3:
    if (the year of modDate) 
(the year of curDate) then
        set ageInYears to (the year of curDate) - (the year of modDate)
        display dialog "The file was changed " & ageInYears & " years ago."
    --Part 4:
    else
        if (the month of modDate) 
(the month of curDate) then
            set ageInMonths to (the month of curDate) - (the month ¬
                of modDate)
            display dialog "The file was changed " & ageInMonths & ¬
                "months ago."
        --Part 5
        else
            if (the day of modDate) 
(the day of curDate) then
                set ageInDays to (the day of curDate) - (the day of modDate)
                display dialog "The file is " & ageInDays & "days old."
            else
                display dialog "The file was changed today."
            end if
        end if
    end if
end tell

You can type the symbol by pressing Option-=. Or, if you'd prefer, you can substitute the plain-English phrase is not equal to in place of the symbol.

This is the most involved script you've written so far. At first it looks pretty complicated, but it actually works fairly simply:

  • Part 1: The script presents an Open dialog box, and sets modDate to the date the selected file was modified.

If you want to check when a file was created rather than when it was modified, use the creation date property instead of modification date.

  • Part 2: The current date, as you're running the script, goes into the curDate variable.

  • Part 3: The script checks if the year the file was modified is the same as the current year. If they're not the same, the script sets the ageInYears variable to the difference between the two yearsand a dialog box tells you how many years ago the file was modified.

  • On the other hand, if the file was modified this year, the script proceeds to the next part.

  • Part 4: Now the script checks if the month the file was modified is the same as the current month. If they're different, the script shows a dialog box with the difference in months. Otherwise, if they're the same month, the script proceeds to the next part.

  • Part 5: If the script has gotten this far, you know that the file was modified this month of this year. The only thing left to check, then, is whether the file was last modified today.

  • If it wasn't, the script calculates how many days ago the file was modified. It then displays that information in a dialog box.

  • On the other hand, if the file was last modified today, the script presents a dialog box informing you of that. At this point, every possibility has been covered, and the script ends.

As you'll surely notice, the script is full of nested if statements, which makes it hard to read. Luckily, AppleScript lets you merge an else statement (on one line) with a subsequent if statement (on the following line), creating an else if statement. Here's what the previous script would look like if you linked your else and if statements in that way:

set selectedFile to
(choose file) tell application "Finder" set modDate to the
modification date of selectedFile set curDate to the current date if
(the year of modDate) (the year of curDate) then set ageInYears to
(the year of curDate) - (the year of modDate) display dialog "The
file was changed " & ageInYears & " years ago." else if (the month of modDate) 
(the month of curDate) then set
ageInMonths to (the month of curDate) - (the month of modDate)
display dialog "The file was changed " & ageInMonths &
"months ago." else if (the day of modDate)
(the day of curDate)
then set ageInDays to (the day of curDate) - (the day of
modDate) display dialog "The file is" & ageInDays & "days
old." else display dialog "The file was changed today." end if end
tell

Now the script is much easier to read, and still works exactly the same way.

People use else if statements for all sorts of other tasks, too. For example, you can check the file format of a document in this way: if it's a Word document...else if it's a PowerPoint document...else if it's an Excel file, and so on.

You can also use else if statements to react to the magnitude of something: if there are less than 5 files on the Desktop, leave the files alone...else if there are between 5 and 20 files on the Desktop, copy them to a different folder...else if there are more than 20 files on the desktop, delete them, for example.

    Team LiB
    Previous Section Next Section