Script to Change Extension

I found an AppleScript on MacScripter here: Change file extension Applescript - need OS X version - #4 by jesmo5 that did almost what I needed. Here’s the original script, by StefanK that was posted in post #5.


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

I modified the script somewhat, to do specifically what I needed. Here it is modified.

try
	set thePath to alias "Users:homer:Desktop:Email Images:"
	set ext to "jpeg"
	set extNew to "jpg"
	
	tell application "Finder" to set name extension of (files of thePath whose name extension is ext) to extNew
end try

try
	set thePath to alias "Users:homer:Desktop:Email Images:"
	set ext to "JPEG"
	set extNew to "jpg"
	
	tell application "Finder" to set name extension of (files of thePath whose name extension is ext) to extNew
end try

try
	set thePath to alias "Users:homer:Desktop:Email Images:"
	set ext to "JPG"
	set extNew to "jpg"
	
	tell application "Finder" to set name extension of (files of thePath whose name extension is ext) to extNew
end try

Now here’s my question. First of all I was thrilled (as a beginner) that I was able to not just modify it, but that it actually worked. But, seeing how I’m using MacScripter to learn more about AppleScript, is there any way to make my script more compact or to combine some of the “try” blocks?

Since all of your renamed images are ending up with the same extension, you can combine the three into one.

set extNew to "jpg"
set extOld to {"jpeg", "JPEG", "JPG"}

tell application "Finder"
	set bloop to (a reference to (files of thePath whose name extension is in extOld))
	if name extension of (bloop) is not extNew then
		set name extension of bloop to extNew
	end if
end tell

About the try statements…

The benefit of having multiple try blocks is that each attempt will get handled independently. If you lump them all together, then if any of them fails —and in your case, that will happen any time the source file’s extension doesn’t match the first test, eg ‘jpeg’— the try block is immediately exited, preventing the subsequent tests from taking place. Having three separate blocks means that it will try each of them regardless of which ones fail. Of course that also means that you would run many unnecessary commands, which you might obviate using a loop or if…then.

One thing to note… each try block has two lines that are common to them all. Those could be put at the top of the script.

And another thing: the ‘JPEG’ attempt is redundant. It turns out that ‘jpeg’ and ‘JPEG’ are identical in this case so you can remove one of them. If you needed them to be handled separately (ie case was important) then you would probably have to handle them a bit differently. By the way, this means that you could remove ‘JPEG’ from my script as well.

1 Like

The reason for the redundancy is that the script below, which I use to change the resolution/size of the image files before I attach them to emails, will not work on a file that has “.JPEG” in capital letters.

do shell script "sips -z 756 1008 " & quoted form of "/Users/homer/Desktop/Email Images/" & "*.jpg"

I’ll work with the script you provided over tomorrow’s mornings coffee. Thank you for the input.

1 Like

I’ve searched here and elsewhere for an example of an “if . . .then” script, with either no luck, or explanations way above my skill level. Could you possibly give me a push in the right direction? Thank you.

Also, your “bloop” script works perfectly and will, if I have no luck with the “if . . .then” script, become what I end up using.

Homer. There are many approaches that can be used, and to a certain extent the approach used is a matter of personal preference. I’ve included my suggestion below.

set theFolder to "Macintosh HD:Users:robert:Working:" --set to desired value

considering case
	tell application "Finder"
		set theFiles to every file in folder theFolder as alias list
		repeat with aFile in theFiles
			if (name extension of aFile) is in {"jpeg", "JPEG", "JPG"} then set name extension of aFile to "jpg"
		end repeat
	end tell
end considering

At times, I need to let go of that “engineering” mentality that requires me to always look for the “best” way to do anything :joy:

Your script works perfectly.

1 Like

I had forgotten that–unless set otherwise with a considering case statement–text comparisons are not case sensitive. So, my script can be simplified as follows:

set theFolder to "Macintosh HD:Users:robert:Working:" --set to desired value

tell application "Finder"
	set theFiles to every file in folder theFolder as alias list
	repeat with aFile in theFiles
		if (name extension of aFile) is in {"jpeg", "jpg"} then set name extension of aFile to "jpg"
	end repeat
end tell

I’ve edited my earlier script to include a considering case statement. This prevents the script from trying to rename files that already have lowercase jpg extensions.

Out of curiosity, how many files does this script loop thru when you run it?
The reason I’m asking is because whenever someone is scripting the Finder to work on large number of files, it is faster to use “System Events” instead.

Here is the “System Events” version.

set theFolder to "Macintosh HD:Users:robert:Working:" --set to desired value

considering case
	tell application "System Events"
		set theFiles to every file in folder theFolder --whose name extension is not "jpg"
		repeat with aFile in theFiles
			set ne to name extension of aFile
			if ne is in {"jpeg", "JPEG", "JPG"} then
				set nm to name of aFile
				set name of aFile to (text 1 thru -(1 + (length of ne)) of nm) & "jpg"
			end if
		end repeat
	end tell
end considering

It is slightly different because in System Events, name extension is read only

** EDIT ** – I did some speed test on a folder of 512 files. Mine was 6 times faster than Finder

This one is even faster…

set theFolder to "Macintosh HD:Users:robert:Working:"

considering case
	tell application "System Events"
		tell every file in folder theFolder to set {theFiles, fNames, exts} to {it, name, name extension}
		repeat with i from 1 to count fNames
			set ne to item i of exts
			if ne is in {"jpeg", "JPEG", "JPG"} then
				set nm to item i of fNames
				set name of (item i of theFiles) to (text 1 thru -(1 + (length of ne)) of nm) & "jpg"
			end if
		end repeat
	end tell
end considering

Thanks for your script, but at most, I’ll never be processing more than a half dozen files.