Adding a suffix to a file using applescript

I am using a droplet/application to add a suffix to my filenames. i want it to add the word "_THUMBNAIL" before the file extension. Right now it is leaving the file extension, adding _THUMBNAIL and adding the extension again. I know part of it has to do with the "& name extension of aFile" line, but that's the only way i could get it to have the extension after the added text. Here is the script below:


on
opensomeFiles

tell application "Finder"

set theList to someFiles

repeat with aFile in someFiles

set fName to (name of (info foraFile)) & "_THUMBNAIL" & "." & name extension of aFile

set name of aFile to fName

end repeat

end tell

end open


Any help to get this sorted would be great!


Thank you in advance.

Posted on Sep 19, 2016 10:35 AM

Reply
5 replies

Sep 19, 2016 11:45 AM in response to Niel

set fName to ((items 1 thru ((offset of "." in (get name of aFile)) - 1) of (get name of aFile)) as string) & "_THUMBNAIL" & "." & name extension of aFile


This will fail (or, at least, have unexpected results) if there is a '.' anywhere in the file name.


A more complete solution would strip the filename extension rather than assume that the . is the delimiter


on opensomeFiles

tell application "Finder"

set theList to someFiles

repeat with aFile in someFiles

set fn to name of aFile

set fx to name extension of aFile

set basename to characters 1 through -(2 + (length of fx)) of fn as text

set newname to basename & "_THUMBNAIL." & fx


-- now you can rename the file to newname

set name of aFile to newname

end repeat

end tell

end open

Sep 19, 2016 12:27 PM in response to thejrowe

Or another variation...


property fappend : "_THUMBNAIL."


on opensomeFiles

tell application "Finder"

repeat with aFile in someFiles

set x to aFile'sname extension

-- step backwards by 2 to avoid the "."

set newFile to text 1 thru ((offset of x in (aFile as text)) - 2) of (aFile as text) & fappend & x

set name of aFile to newFile

end repeat

end tell

return

end open

Given: Macintosh HD:Users:viking:Desktop:star.png

Result: Macintosh HD:Users:viking:Desktop:star_THUMBNAIL.png

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Adding a suffix to a file using applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.