thejrowe

Q: 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
open someFiles

  tell application "Finder"

  set theList to someFiles

  repeat with aFile in someFiles

  set fName to (name of (info for aFile)) & "_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

Close

Q: Adding a suffix to a file using applescript

  • All replies
  • Helpful answers

  • by Niel,Solvedanswer

    Niel Niel Sep 19, 2016 10:45 AM in response to thejrowe
    Level 10 (313,723 points)
    Mac OS X
    Sep 19, 2016 10:45 AM in response to thejrowe

    Replace the ‘set fName’ line with:

     

    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

     

    (144742)

  • by thejrowe,

    thejrowe thejrowe Sep 19, 2016 10:53 AM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Sep 19, 2016 10:53 AM in response to Niel

    THANK YOU!

  • by Camelot,

    Camelot Camelot Sep 19, 2016 11:45 AM in response to Niel
    Level 8 (47,300 points)
    Mac OS X
    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 open someFiles

      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

  • by VikingOSX,

    VikingOSX VikingOSX Sep 19, 2016 12:27 PM in response to thejrowe
    Level 7 (21,165 points)
    Mac OS X
    Sep 19, 2016 12:27 PM in response to thejrowe

    Or another variation...

     

    property fappend : "_THUMBNAIL."

     

    on open someFiles

         tell application "Finder"

              repeat with aFile in someFiles

                   set x to aFile's name 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

  • by thejrowe,

    thejrowe thejrowe Sep 19, 2016 12:59 PM in response to Camelot
    Level 1 (4 points)
    Mac OS X
    Sep 19, 2016 12:59 PM in response to Camelot

    This is what i needed.