Change file extension Applescript - need OS X version

Can anyone tell me how to modify this Applescript to work for OS X?
It is supposed to let you change the file extension of a group of files in a folder.

i.e. I want to change a folder of .jpg’s to .jpeg’s

But when I run it I keep getting this error:

Apple Script Error
Finder got an error: Can’t set name of selection to “abc123.jpeg”


set thePath to choose folder with prompt “Select folder to rename files in:”
set ext to display dialog “Extension to Replace:” default answer “”
set ext to text returned of ext
set extNew to display dialog “Replace With:” default answer “”
set extNew to text returned of extNew

tell application “Finder”
try
set the datlist to ¬
((every file in thePath whose name ends with ext) as alias) ¬
as list – set name criteria
on error number -1700 from f
set datlist to f as list
end try
repeat with dat in datlist --list of alias references to the files
select file dat
set workingName to the name of dat as text
set theOffset to offset of ext in workingName
set chopped to characters 1 through (theOffset - 1) of workingName
set newName to chopped & extNew as text
set name of selection to newName
end repeat
end tell

There is no need to work with the selection.

Replace : select file dat
with : set dat to dat’s content – dereference

Replace : set name of selection to newName
with : set name of dat to newName

Maybe this will work (tested with OS X 10.2.8).

set thePath to choose folder with prompt "Select folder to rename files in:"
set ext to text returned of (display dialog "Extension to Replace:" default answer "")
set extNew to text returned of (display dialog "Replace With:" default answer "")

tell application "Finder"
	try
		set the datlist to files of thePath whose name extension is ext
	end try
	repeat with dat in datlist
		set ext_length to count (get name extension of dat)
		try
			set name of dat to ((text 1 thru -(ext_length + 1) of (get name of dat)) & extNew)
		on error e
			display dialog e
		end try
	end repeat
end tell

– Rob

When i try to use this script to change the extension from mpeg to mpg i get this error.

Finder got an error: Can’t get name extension of document file “AB-ET108 REV.mpeg” of folder “DVD Import Folder” of disk “Raid00”. :confused:

any ideas?

short version :wink:


set thePath to choose folder with prompt "Select folder to rename files in:"
set ext to text returned of (display dialog "Extension to Replace:" default answer "")
set extNew to text returned of (display dialog "Replace With:" default answer "")

tell application "Finder" to set name extension of (files of thePath whose name extension is ext) to extNew

Thank you for your help