Re: Newbie question att: Mr Tea
Re: Newbie question att: Mr Tea
- Subject: Re: Newbie question att: Mr Tea
- From: Mr Tea <email@hidden>
- Date: Tue, 13 Feb 2001 15:00:10 +0000
This from Spencer Sunar - dateline 2/13/01 10.38 am:
>
Hello, and thank you Mr Tea for your response, yes I do need to script to
>
'watch' a folder, also all its sub folders too.
Hi again, Spencer.
The script reproduced in this message should do what you need, but first, a
few words of explanation.
To do the name changing stuff, the script borrows a powerful technique that
uses AppleScript's text item delimiters (TIDs) to achieve the find and
replace function. These TIDs are normally set to "", which means,
effectively, nothing at all. After changing them, it's always good practice
to reset them to their original value.
A lot of the information used in the script is saved in the form of
variables. These are words (like theNum and theFile) that have no meaning to
AppleScript except the value that is assigned to them in the script. They
are a convenient and compact way of storing information that you need to
refer to more than once.
This script is written as a folder action, so you can attach it to any
folder you need it to work on (using the contextual menu). For a folder
action to run, the folder it is attached to must be open, *unless* you have
Eric Grant's amazing free extension, Folder Actions Plus, which gets round
this obstacle. You can find it at:
http://home.earthlink.net/~eagrant/
I won't bog you down with more technical info (because I'll probably get it
wrong) but there's one thing you need to watch out for. If the changed
filename is more than 31 characters long, the script will fail. Your
proposed name changes add five characters to the length of each name, so if
the original file names are likely to be longer than 26 characters
(including any spaces and non-alphabetic characters) you will need to
include an additional error catching routine.
Ok. Heres the script.
on adding folder items to theFolder after receiving FileList
tell application "Finder"
copy {"01.", "02.", "03."} to numList
copy {"200x200.", "160x160.", "140x140."} to sizeList
copy AppleScript's text item delimiters to oldDelims
repeat with i from 1 to numList's length
if file type of theFile is "JPEG" then
set theNum to item i of numList
set theSize to item i of sizeList
repeat with theFile in FileList
set TheName to the name of theFile
if theNum is in TheName then
try
set AppleScript's text item delimiters to theNum
set theGlyphs to every text item of TheName
set AppleScript's text item delimiters to theSize
set newName to theGlyphs as string
set the name of theFile to newName
on error
set AppleScript's text item delimiters to oldDelims
end try
end if
end repeat
end if
end repeat
set AppleScript's text item delimiters to oldDelims
end tell
end adding folder items to
Paste this into a new document in ScriptEditor and save it as a compiled
script. It will only affect JPEG files, and you can easily change it to a
droplet by replacing the first and last lines with:
on open fileList
--script
end open
...and saving the changed script as an applet. The droplet will work on
files dropped onto it, but not the contents of folders. Extra stuff is
needed for that
You can add to or change the items in numList and sizeList to suit your
needs, just make sure that both lists have the same number of items in them,
and that the position of the corresponding items in each list matches.
If the numbers and sizes used in your file names are always separated from
the rest of the name by a space, you may want to add a leading space to each
of the items in numList and sizeList as an added precaution against wrong
files being renamed.
--
"Let the pot stand for at least five minutes before serving."
http://home.earthlink.net/~eagrant/Folder-Actions-Plus.sit.hqx