Many thanks Yvan! My updated code fixed one problem but created another one.
(I was trying to fix the original code for ImageConvert in the Unscripted Forum on this website).
I agree that your code for is much better. It solves the original problem for the ImageConvert script. (i.e. the original code would create converted image files with double extensions (e.g. Image.PNG.JPG) when a user has enabled “Show all filename extensions” in the preferences for Finder. The extension hidden attribute of the Finder really does not work when this option is enabled in the preferences for Finder).
P.S. I turned your code into a subroutine so I can reuse it in other scripts I develop. Below is a sample script using the subroutine.
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}
--Get the artwork file
set theFile to choose file with prompt "Choose art file(s)" of type openTypes without invisibles
set theNewType to "JPG"
runTest(theFile, theNewType)
on runTest(theArt, theType)
display dialog "theArt is " & theArt
set noExtPath to removeExtension(theArt)
set theNewArt to noExtPath & "." & theType
display dialog "theNewArt is " & theNewArt
end runTest
on removeExtension(thisName)
tell application "Finder"
set theExt to (name extension of thisName)
end tell
set thisName to thisName as string
if theExt is "" then
set noExtPath to thisName as string
else
set noExtPath to (text items 1 thru -(2 + (count theExt)) of thisName) as string
end if
return noExtPath as text
end removeExtension
I also created a companion subroutine that replaces the extension. See the sample script below using this subroutine:
property openTypes : {"com.adobe.pdf", "com.microsoft.bmp", "com.apple.pict", "public.png", "com.adobe.photoshop-image", "public.tiff"}
--Get the artwork file
set theFile to choose file with prompt "Choose art file(s)" of type openTypes without invisibles
set theNewType to "JPG"
runTest(theFile, theNewType)
on runTest(theArt, theType)
display dialog "theArt is " & theArt
set theNewArt to replaceExtension(theArt, theType)
display dialog "theNewArt is " & theNewArt
end runTest
on replaceExtension(thisName, newExt)
tell application "Finder"
set theExt to (name extension of thisName)
end tell
set thisName to thisName as string
if theExt is "" then
set noExtPath to thisName as string
else
set noExtPath to (text items 1 thru -(2 + (count theExt)) of thisName) as string
end if
return (noExtPath & "." & newExt) as text
end replaceExtension
I also agree with you that one can use the simpler property statement:
property openTypes : {“com.adobe.pdf”, “com.microsoft.bmp”, “com.apple.pict”, “public.png”, “com.adobe.photoshop-image”, “public.tiff”}
instead of
property openTypes : {“PDF”, “com.adobe.pdf”, “BMP”, “com.microsoft.bmp”, “PICT”, “com.apple.pict”, “PNG”, “public.png”, “PSD”, “com.adobe.photoshop-image”, “TIFF”, “public.tiff”}
I suspect the other unnecessary types were added for readability sake by the original coder, Kevin Bradley.
Again many thanks for improving the code!