Renaming Files

I’m a newbie to AppleScript, and this forum has been quite useful in my research in renaming files. I have tried various scripts to rename jpeg files that appears after a droplet action, and I am stuck. The one I’m trying now has a AppleScript Error messages that reads: Can’t get entire contents of “Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output �:look”.

I really appreciate if someone could tell me what I’m doing wrong. Thank you.

The script I have written looks like this.

set SourceFolder to “Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output �:look:”

tell application “Finder”
set files_ to (files of entire contents of “Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output �:look” whose file type is “jpg”)
repeat with file_ in files_
set name_ to name of file_
if name_ begins with “O” then set new_name to “V” + (text 2 thru end of name_)
if exists file new_name_ of “Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output �:look” then
delete file new_name of “Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output �:look”
end if
set name of file_ to new_name
end repeat
end tell :oops:

Does this work?

set SourceFolder to "Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output ƒ:look:"

tell application "Finder"
	set files_ to (files of entire contents of alias SourceFolder whose file type is "jpg")
	repeat with file_ in files_
		set name_ to name of file_
		if name_ begins with "O" then set new_name to ("V" & (text 2 thru end of name_))
		if exists file new_name_ of alias SourceFolder then ¬
			delete file new_name of alias SourceFolder
		set name of file_ to new_name
	end repeat
end tell

– Rob

Hi Rob,

Thanks for cleaning up my script. It ran fine, but the image in the look folder didn’t change. It remained with the letter “O” in front of the name.

ottert :cry:

That’s what happens when I don’t test before posting. :confused: This works for me.

set SourceFolder to alias "Macintosh HD:Users:ottert:Desktop:Image_Processing_2004:(O) ONFIGURE Output ƒ:look:"

tell application "Finder"
	set files_ to files of entire contents of SourceFolder whose file type is "JPEG" and name begins with "O"
	repeat with file_ in files_
		set name_ to name of file_
		set new_name to "V" & text 2 thru end of name_
		if exists file new_name of SourceFolder then delete file new_name of SourceFolder
		set name of file_ to new_name
	end repeat
end tell

– Rob

Hey Rob,

You are the best! The script works stupendeously! I have one last question. Is it possible to turn the look folder to an action folder? This way the jpg files will change the naming without having to manually run the script. I try using the Folder Actions Setup, and nothing happened. Thank you!

ottert

This appears to work. Use Folder Actions Setup to attach the script to any folder that needs to be watched. Note that it will not work on images enclosed in folders which are added to the attached folder. Let me know if you need the script to handle folders as well as files.

on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		repeat with file_ in added_items
			set name_ to name of file_
			if name_ begins with "O" and file type of file_ is "JPEG" then
				set new_name to "V" & text 2 thru end of name_
				if exists file new_name of this_folder then delete file new_name of this_folder
				set name of file_ to new_name
			end if
		end repeat
	end tell
end adding folder items to

– Rob

Very cool Rob! It works beautifully! Thanks so much for helping me out. I’m sure I will need to pick your brain sometime in the future. Thanks again!

ottert :smiley:

Glad to help. :slight_smile: