Here’s my situation:
I have a folder in which I drop .EPS files. Some of the filenames contain HT or HTM at the end (Ex. FilenameHT.EPS or FilenameHTM.EPS)
I’m trying to make a Folder Action that will find the offending files and strip the characters from the end of the name and let others pass through without any changes.
The ultimate goal is: Dropping FilenameHT.EPS into a Folder and having it automatically renamed Filename.EPS within the folder.
Here’s what I have so far: (I started off removing HT and once that worked I would remove HTM)
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
set Trim_HT to "HT" as string
set OldName to name of added_items
set NewName to ""
repeat
if fName ends with Trim_HT then
set NewName to (characters i thru -2 of OldName)
exit repeat
end if
end repeat
end tell
end try
end adding folder items to
I can’t get this to work at all. Can someone advise me on what I’m doing wrong?
seems to me that added_items is going to be a list. it might be a list of only one item, but it’s not going to have a name. and i don’t see where fName comes from.
so something like
tell application “Finder”
repeat with oneItem in added_items
set fName to name of oneItem
–hammer name here
end repeat
end tell
With hot folders and droplets, you might want to think about all the crap that a user could drop there accidentally… files of the wrong type, other folders, etc. I usually include a spitItOut(oneItem) handler to keep unusable junk from piling up in the watched folder
Also, when you rename the files, you’re going to retrigger the script ” possibly repeatedly ” because the Folder Actions watch then thinks that more items have been added to the folder. If you really must rename files in the watched folder, you can switch off Folder Actions temporarily while you do so:
on adding folder items to this_folder after receiving added_items
-- Switch off the folder watching while renaming.
tell application "System Events" to set folder actions enabled to false
tell application "Finder"
repeat with oneItem in added_items
set fName to name of oneItem
if (fName ends with "HT.EPS") then
set name of oneItem to (text 1 thru -7 of fName & ".EPS")
else if (fName ends with "HTM.EPS") then
set name of oneItem to (text 1 thru -8 of fName & ".EPS")
end if
end repeat
end tell
-- Reenable the folder watch.
tell application "System Events" to set folder actions enabled to true
end adding folder items to
This assumes that none of the dropped items has “HT.EPS” or “HTM.EPS” as its entire name. You may also want to watch out for when names might legitimately end with those letter combinations ” eg. “Eight.eps”.
Nigel,
First off, thanks for your help. I tested the script you wrote but it didn’t work for me. Am I missing something? Is the script case sensitive? The file I’m using for testing purposes is “filenameHT.EPS” (but the names of the actual files that will be dropped into the folder will vary in length). I have the folder action turned on under Folder Actions Setup. But when I drop the file into the folder, it is still named "filenameHT.EPS. Is there another step I need to do within OS to enable the Folder Action?
I thought this would be an easy task to undertake, but I’m about to pull my hair out!
slimjim5811
Firstly, of course, Folder Actions have to be enabled. I think there’s a script for doing that that comes with the system. It’s basically the last line in the script I posted earlier. The action script also has to be attached to the folder.
Secondly, the action script has to be in a folder called “Folder Action Scripts” in your user Scripts folder. You can create this folder for yourself if it doesn’t already exist. (It won’t show up in the Script Menu.) Hopefully that’ll solve the problem, as the script’s working OK for me.
Another thing it occurs to me to watch out for is to make sure that two or more files don’t end up with the same name after they’re renamed. I don’t know how likely that is for you, but I thought I’d mention it.
Now we’re getting somewhere! Here’s what I got to work: (it make look bad, but I’m new at this)
set MasterFolder to choose folder with prompt "Select the folder of filenames to trim"
tell application "Finder"
set FolderContents to files of folder MasterFolder
end tell
repeat with thisFile in FolderContents
set thisFilesName to name of thisFile
if thisFilesName ends with "ht.eps" then
set Charcount to count characters of thisFilesName
set newName to (characters 1 thru -7 of thisFilesName & ".EPS") as string
set name of thisFile to newName
end if
if thisFilesName ends with "htm.eps" then
set Charcount to count characters of thisFilesName
set newName to (characters 1 thru -8 of thisFilesName & ".EPS") as string
set name of thisFile to newName
end if
end repeat
Again, this will be a Folder Action, but for testing purposes I’m using a Choose Folder prompt.
This is now my problem. What I’d like to do is simply replace an existing file with the new file, if there’s already a file with the same name.
Is there a way to do this or does the existing file need to be moved to the trash with a shell script?
You need to include the whole of the repeat in the Finder ‘tell’ block, because getting and setting the file names are Finder functions.
Assuming you’ve done that, you can also get the Finder to deal with existing files that have the same name:
tell application "Finder"
-- Blah blah
if thisFilesName ends with "ht.eps" then
set newName to text 1 thru -7 of thisFilesName & ".EPS"
if file newName of folder MasterFolder exists then
delete file newName of folder MasterFolder
end if
set name of thisFile to newName
end if
-- etc.
end tell
I’m now having difficulties turning this AS into a Folder Action.
I think what’s happening is that the rest of my existing script isn’t jiving with the Folder Action line. I’d appreciate if someone to tell me how to make the Folder Action match up to the rest of the script. I believe I need to somehow set the added_items to thisfile, or something like that.
on adding folder items to this_folder after receiving added_items
--set MasterFolder to choose folder with prompt "Select the folder of filenames to trim"
tell application "Finder"
set FolderContents to files of this_folder
repeat with thisFile in FolderContents
set thisFilesName to name of added_items
if thisFilesName ends with "ht.eps" then
set newName to (characters 1 thru -7 of thisFilesName & ".EPS") as string
set name of thisFile to newName
if file newName of folder this_folder exists then
delete file newName of folder this_folder
end if
end if
if thisFilesName ends with "htm.eps" then
set newName to (characters 1 thru -8 of thisFilesName & ".EPS") as string
if file newName of folder this_folder exists then
delete file newName of folder this_folder
end if
set name of thisFile to newName
end if
end repeat
end tell
end adding folder items to
Or should I set MasterFolder to this_folder and change all references from this_folder back to MasterFolder?
on adding folder items to this_folder after receiving added_items
tell application "Finder" to repeat with thisFile in added_items
set thisFilesName to thisFile's name
repeat with currEnd in {"ht.eps", "htm.eps"}
if thisFilesName ends with currEnd then
set newName to (text 1 thru -((count currEnd) + 1) of thisFilesName) & ".EPS"
if file newName of this_folder exists then delete file newName of this_folder
set name of thisFile to newName
exit repeat
end if
end repeat
end repeat
end adding folder items to
kai -
Words cannot describe how grateful I am for the script you wrote…but I’ll try. You are an absolute genius (in my book, anyway). You not only helped me fix the problem with my Folder Action, but you compacted my existing, ugly (but functional) script. I will tell tales of your wisdom and generosity.
Nigel Garvey -
Thank you for your help as well. You originally took my post to heart and pointed me in the right direction. I appreciate the time you spent helping me.
sorry to revive an older thread, but I have a similar (however simpler) situation.
I have several thousand files that all need to have a “0” placed at the beginning of the file name. I’m no scripter, but I do need to have these done in a couple days? Anyone have a quick second to help me out?
BTW, this doesn’t have to be a folder action, it can be a droplet, or simply a script w/ a ‘choose folder’ command.