Ok, heres the scoop, this script worked perfectly in OS 9.2.
This script when attached to a folder, allows you to drop a file into the folder and have it add a defined prefix to the file name including commnets to the file. It then moves the file to the desktop, creating a folder on the desktop named comp images. This worked perfectly in 9. I use this with about 70 folders that are in what I call a processing folder.
All the folders are attached to respective File renaming scripts, and it is a way for our Creatives to keep track of where they get there images from. Cool huh.
The problem is in Panther, the prefix gets added several times, sometimes once or twice and as many as 6 times, this doesnt seem to be consistent. So something is flaking. I’m stumped so I’m posting it here for you guys to chew on. Thanks in advance.
Jeff
Current Script. Attach this to a folder and test it out by dragging a file into the folder.
Let me know if you get the same results and what OS you are running it on.
on adding folder items to theFolder after receiving theAddedItems
delay 5
repeat with thisItem in theAddedItems
tell application "Finder"
set theDate to the current date --or any other date
copy theDate to b
set theyear to the year of theDate
set theDay to the day of theDate
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)
set thePrefix to "ABC_"
set itemName to name of thisItem
set newName to (thePrefix & itemName)
set name of thisItem to newName
set the comment of thisItem to thePrefix & itemName & return & monthNum & "/" & theDay & "/" & theyear
if folder "Comp Images" of desktop exists then
set compImages to folder "Comp Images" of desktop
--
if folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages exists then
move thisItem to folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages
else
make new folder at compImages with properties {name:"Comp Images " & monthNum & "/" & theDay & "/" & theyear}
move thisItem to folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages
end if
--
else
make new folder at desktop with properties {name:"Comp Images"}
set compImages to folder "Comp Images" of desktop
--
if folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages exists then
move thisItem to folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages
else
make new folder at compImages with properties {name:"Comp Images " & monthNum & "/" & theDay & "/" & theyear}
move thisItem to folder ("Comp Images " & monthNum & "/" & theDay & "/" & theyear) of compImages
end if
--
end if
end tell
end repeat
Still adding multible prefixes. I end up with abc_abc_abc_test as the file name.
And its not consistent. I’m going to try it on another Mac to see if something is up with my TiBook. Did it work for you?
It’s probable that when you rename the files, they register as new additions to the folder and retrigger the folder action. Try moving the files to the new folder first and rename them there.
Nigel, you were the one that helped me with this script 2 years ago. Man what are the odds. LOL. How are you sir? Hope your doing well.
Do you remember helping me before? Probally not but thanks anyway.
I’m assuming the problem is something in the way OS 10 sees the script as it was written in OS 9. Maybe there’s a change in a handler or something to that effect. But the script works perfectly in OS 9.
But I agree with your theory. The script believes that the file after the prefix has been added is a new file recently added i suspect. But it shouldn’t work that way. The rename script should only kick off on the addition of a file. Hence…
"on adding folder items to theFolder after receiving theAddedItems "
Right? How can I tell it to prefix the file, and then end that part of the script. Maybe a
Delay command, or insert a end somewhere. Hmmmm
In the script I posted, I made the reference to the files that are iterated an alias so all you need to do is to move the files first and then rename them (tested on Mac OS X 10.3.5):
Hi, Jeff. Thanks for your greeting. I’m afraid I don’t recall the script. The short-date stuff in it is similar to what I was using a couple of years ago, but I can’t believe I’d have put that in a Finder tell block or that I’d have put it and the folder checks inside the repeat loop.
There’s quite a lot in OS X scripting that isn’t as straightforward as in OS 9. Jon’s second script will do what you want very nicely, but if you’re going to be adding a large number of items to the folder at any one time, you may prefer something a little faster:
on adding folder items to theFolder after receiving theAddedItems
-- Get a short date in US format.
-- Tested on 10.2.8, so the method for getting the month number isn't Panther-specific.
set theDate to (current date)
set {day:d, year:y} to theDate
copy theDate to b
set b's month to January
tell (y * 10000 + (b - 2500000 - theDate) div -2500000 * 100 + d) as string to set dateStr to text 5 thru 6 & "/" & text 7 thru 8 & "/" & text 1 thru 4
set filePrefix to "ABC_"
set folderName to "Comp Images"
set subfolderName to folderName & " " & dateStr
tell application "Finder"
try
set targetFolder to folder folderName of desktop
on error
set targetFolder to (make new folder at desktop with properties {name:folderName})
end try
try
set theSubfolder to folder subfolderName of targetFolder
on error
set theSubfolder to (make new folder at targetFolder with properties {name:subfolderName})
end try
set oldNames to name of theFolder's items
move theAddedItems to theSubfolder
repeat with thisName in oldNames
set newName to (filePrefix & thisName)
tell item thisName of theSubfolder to set {comment, name} to {newName & return & dateStr, newName}
end repeat
end tell
end adding folder items to
Yes, I believe it was the date stuff you helped me work out a few years back. This code you have enclosed works perfectly for me in 10.3.5.
I’m not sure what was up with Jon’s script but it would do the same thing as my original code. It was adding the prefixes mutlible times, and it wasn’t consistent.
I’ll have to study the code more closely to see where yours differs and what fixed the problem, but in the mean time, many thanks again. : )