find/replace in comments of file?

Ok, folks. Sure could use a nudge on this. Here’s the scenario:
I’m recieving image files from various sources and they are entering specific, pre-determined information in the comments of each file. Each file’s comments contains various entries in the following form:
Dept698, MIC961, Style000785423619
However, in each entry using the "Dept"prefix, there are actually supposed to be 4 digits, so the above is incorrect. It SHOULD read: “Dept0698”.
My problem is, every dept number NEEDS to be four digits. But I have sources who aren’t entering dept numbers correctly with 4 digits, but are using the incorrect 3-digits.
SO, what I need to do is find all occurrences of “DeptXXX” and enter a leading zero so that all entries reading “DeptXXX” now read “Dept0XXX”. Note: There are often multiple “Dept” entries in one comments field, so I need to change all instances.
I could probably easily do this with find/replace commands in, say, Tex-Edit, but I’m not sure the best way to approach it in the Finder/file comments fields. I looked into Akua Sweets’ text suite, but I’m not sure “munge” would be the way to go.
Could someone give me a nudge here? Many thanks in advance.

: Ok, Marc/Rob. I’ve extracted the portion that does the grunt work form Marc’s
: fantastic example. (I’m going to be incorporating it into another script).
: It’s working in this form, but only on the FIRST instance of
: “Dept” in the comments, but it DOES run through all the files in
: a selection.

: So, how do I extend it to act on ALL instances of “Dept” in the
: comments?

: It LOOKS like it should be doing this in the “repeat with i from 1 to
: count commList” line, but it doesn’t happen. What am I missing?

I noticed that you’re not allowing for the two different kinds of possible delimiters within the comment, a single comma or a comma and a space. Your list won’t split on the second type of delimiter the way you have it set up, so it would only have one item in the list if the comment was delimited by the second type. If that one item starts with “dept” it’ll modify it and leave the rest. In the script I published I did some manipulation to turn the two character delimiters to a single comma, split the text into a list based on commas, and then rebuilt the text at the end inserting the two character delimiters.
Marc K. Myers

[2/1/02 12:34:38 AM]

Need more info on the reliability of those entering the comments…
Is the information always separated by commas (if yes, turning it into a
list becomes a possibility)? Can you always count on a department
entry to be “DeptXXX” or “DeptXXX” - (7 or 8 characters in length)?
Later,

Gladly!

Separation: Yes. We can count on the separation being commas. Most of the time it’ll be a comma and a space ", ", sometimes just a comma. But there’s almost always a comma atleast. 99.9% of the time.
Of course, I only need to change the ones that say DeptXXX. If they already have a 4-digit DeptXXXX entry, no need to touch it.
Does this help?

Yup, but I see that Marc is way ahead of me on this, so I’ll wait to see if you
need further info. :slight_smile:
– Rob

This goes well beyond a nudge, but I got carried away. I did leave you the testing to do, as I only tested it on a single file. It’s set up to process any combination of files and folders you drop on it and will get at files no matter how deeply buried. I had to go through a bit of fuss manipulating the comment entry delimiters, since they could start out being either a comma or a comma and a space. It will deal with entries of fewer than 7 characters by padding with more zeros. On output it standardizes the delimiters as a comma and a space.

on open (itemList)
        set od to AppleScript's text item delimiters
        try
                repeat with anItem in itemList
                        if last character of (anItem as text) is ":" then
                                procFldr(anItem)
                        else
                                procFile(anItem)
                        end if
                end repeat
                set AppleScript's text item delimiters to od
                beep
                delay 1
        on error m number n
                set AppleScript's text item delimiters to od
                display dialog (n as text) & return & m
        end try
end open
on procFldr(theFldr)
        tell application "Finder"
                try
                        set itemList to items of theFldr as alias list
                on error
                        set itemList to items of theFldr as alias as list
                end try
        end tell
        repeat with anItem in itemList
                if last character of (anItem as text) is ":" then
                        procFldr(anItem)
                else
                        procFile(anItem)
                end if
        end repeat
end procFldr
on procFile(theFile)
        tell application "Finder" to set theComment to comment of theFile
        set AppleScript's text item delimiters to {", "}
        set commList to text items of theComment
        set AppleScript's text item delimiters to {","}
        set commList to (commList as text)
        set commList to text items of commList
        tell application "Finder" to copy commList to origList
        repeat with i from 1 to count commList
                set theEntry to item i of commList as text
                if theEntry starts with "dept" and ¬
                        length of theEntry < 8 then
                        set item i of commList to "Dept" & text -4 thru -1 of ¬
                                ("0000" & (text 5 thru (length of theEntry) of theEntry))
                end if
        end repeat
        if commList is not origList then
                set AppleScript's text item delimiters to {", "}
                tell application "Finder" to set comment of theFile to (commList as text)
        end if
end procFile

Weird. I got Marc’s message via the email sub’, but I don’t see it as a reply in the thread.
Well, anyhoo, Marc, you’re right. It’s WAY more than a nudge, but it works like a champ. Now to pore over your work and LEARN how to do this and why it works. It’s as important to me to learn why as it is to learn the how.

Yeah, I noticed some weird happenings too (on this thread). It’s possible
that Marc deleted the original post, and then resubmitted. If not, then
sumpthin’ bad is going on with the BBS.
– Rob

I “published” the script and then saw the exchange between you and Rob about the possibility of having different delimiters between the entries in the comment field. I had to recode to allow for that so I deleted my original post, fixed the script, and reposted.

AHA! Many, many thanks, Marc!

You’re very welcome. If you’ve got any questions about how it works I’d be glad to try to explain whatever isn’t clear.

Ok, Marc/Rob. I’ve extracted the portion that does the grunt work form Marc’s fantastic example. (I’m going to be incorporating it into another script). It’s working in this form, but only on the FIRST instance of “Dept” in the comments, but it DOES run through all the files in a selection.
So, how do I extend it to act on ALL instances of “Dept” in the comments?
It LOOKS like it should be doing this in the “repeat with i from 1 to count commList” line, but it doesn’t happen. What am I missing?
on run
tell
application “Finder”
set
theFiles to every item in selection
repeat
with thisItem in theFiles set { od , AppleScript 's text item delimiters } to { AppleScript 's text item delimiters , {“,”}}
set theComment to comment of thisItem
set
commList to text items of theComment
copy
commList to origList
repeat
with i from 1 to count commList
set
theEntry to item i of commList as text
if
theEntry starts with “dept” and length of theEntry
set
item i of commList to “Dept” & text -4 thru -1 of (“0000” & ( text 5 thru ( length of theEntry ) of theEntry ))
end if
end repeat
if commList is not origList then
tell application “Finder”
set comment of thisItem to ( commList as text )
end tell end if
end repeat
set AppleScript 's text item delimiters to “”
end
tell
end
run

Well, gentlemen, I think we have a winner!
I’ve got this guy working like a champ on ALL occurences of “Dept” within the comments of a file.
Many, many thanks to both Marc and Rob for your extremely generous help on this.
Be well.
T.J.
tj@macscripter.net