5.6. Picking a File from a Dialog BoxBack 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.
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.
5.6.1. Showing When a File was CreatedAdmit 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
This is the most involved script you've written so far. At first it looks pretty complicated, but it actually works fairly simply:
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.
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. |