Re: Finding and deleting specific extensions.
Re: Finding and deleting specific extensions.
- Subject: Re: Finding and deleting specific extensions.
- From: Christopher Nebel <email@hidden>
- Date: Sun, 14 Sep 2003 23:21:55 -0700
On Sep 14, 2003, at 1:01 PM, Christopher Nebel wrote:
On Sep 14, 2003, at 4:31 AM, John Stewart wrote:
On 9/13/03 at -0700 Team Loco said this
I am trying to see if there is a way to have an apple script that
searches for a specific file extension...
Here is a very simple OS X specific script that covers the basics. It
needs a few bells and whistles added such as checking for .htm files,
recursion for sub-folders and error checking.
Except that it doesn't have any of the mentioned bells and whistles,
and could be written much more simply:
My apologies to Mr. Stewart, who chastised me for commenting on his
script's lack of bells and whistles. For some reason, I mis-read "it
*needs*" as "it *has*" and was perplexed enough to comment. My
mistake. Naturally, my version lacked any of the bells and whistles as
well.
In an attempt to address some of these:
1. Going "deep".
In theory, you can get all the matching files even in sub-folders (and
sub-sub-folders, etc.) simply by saying
every file of entire contents of f whose name extension is "html"
-- assuming f is a folder or disk.
However, this was highly unreliable in classic Mac OS -- it would only
return some of the matching files -- and some people still report
difficulties with it in Mac OS X. To do it yourself, you'll need to
write a recursive handler, which I sincerely hope someone else will
supply.
2. Checking for other extensions (e.g. "htm")
This is pretty simple, except that you have to understand how "or"
works in AppleScript, because it's not the same as in English.
Specifically, "or" joins relational expressions, not values. In
English, you could say "name ends with "htm" or "html"', but in
AppleScript, you have to say 'name ends with "htm" or name ends with
"html"'.
3. Error checking
This particular script doesn't need much, if any -- since you're just
moving stuff to the trash, there isn't much to do in the unlikely event
that it fails. However, if you wanted to notify the user, you could
use a "try" block, something like this:
try
tell application "Finder" to move ...
on error errMessage
display dialog "Oh dear, " & errMessage
end
4. Confirmation
The original question asked (maybe) for a confirmation dialog before
actually deleting anything. The basic answer is "use 'display
dialog'". (See Standard Additions.) The question then is, how do only
do something if the user says so? There are a couple of ways to do
this. First, the straightforward way (watch the line wrap):
display dialog "Are you sure you want to frotz the potrzebie?" buttons
{"No", "Yes"}
if button returned of the result is "Yes"
-- frotz away!
end
The title of the button pressed is returned in the "button returned"
field of a record. (If you had used a text field or asked for a
timeout, there would be other fields, too.) Test it against the value
you want -- "Yes", in this case -- and there you are.
Now, the cheap and sleazy way:
display dialog "Are you sure you want to frotz the potrzebie?" buttons
{"Cancel", "Frotz"}
-- frotz, frotz, frotz.
That's it. Why does this work? Well, there's a special rule in
"display dialog" -- if there's a button named "Cancel" [1], and the
user presses it, "display dialog" will throw a "user canceled" error --
if your script doesn't trap it, then the script will end. (I'm not
sure I really recommend this, since it's a bit obscure, but it's an
interesting trick.)
Putting 2, 3, and 4 together:
set f to choose folder with prompt "Pick a folder to purge of html
files:"
display dialog "Are you sure?" buttons {"No", "Yes"} default button
"No"
if the button returned of the result is "Yes" then
try
tell application "Finder"
-- this is all one line...
move (every file of f whose name extension is "htm" or name
extension is "html") to the trash
end
on error e
display dialog "Whoops, " & e
end
end
--Chris Nebel
AppleScript Engineering
[1] Actually, the magic name depends on the name of the "Cancel"
button in your primary language. For a French user, for example, this
would be "Anuller".
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.