4 Replies Latest reply: Jan 14, 2014 8:52 AM by mikebw
Avai Level 1 Level 1 (0 points)

I have many files in one folder where the actual name of the file is preceeded by a few characters, and other files in another folder where the actual name of the file is followed by a few characters.  In Windows I could create a batch file in the directory, then when I run the file all other files in its directory are shortened by N characters from either the beginning or the end of the name, depending on what I wrote.

 

Is it possible to do such a thing in AppleScript?  If so, would someone please point me to a resource or give me an example?  Thank you very much in advance.


iMac Quad-Core i5 (Late 2009), Mac OS X (10.6.7)
  • Jacques Rioux Level 4 Level 4 (2,505 points)

    Hi,

     

    Avai wrote:

     

    Is it possible to do such a thing in AppleScript?  If so, would someone please point me to a resource or give me an example?

    Yes, it's possible.

     

    Here a example

    --------

    set nCharacters to 4

    set theFolder to choose folder

    display dialog "remove characters in name :" buttons {"Cancel", "End", "Beginning"} cancel button "Cancel"

    set btn to button returned of the result

     

     

    tell application "Finder"

      repeat with thisFile in (get document files in theFolder)

      try

      tell thisFile to set name to my cutChars(name, btn, nCharacters)

      end try

      end repeat

    end tell

     

    on cutChars(t, b, n)

      if b is "End" then

      return text 1 thru -(n + 1) of t

      else

      return text (n + 1) thru -1 of t

      end if

    end cutChars

     

    ---------------------------------------

  • twtwtw Level 5 Level 5 (4,900 points)

    well, that script doesn't account for file extensions (which I'm assuming shouldn't  be trimmed).  here's a modified version:

     

    set theFolder to choose folder

    set theResult to display dialog "remove characters in name :" buttons {"Cancel", "End", "Beginning"} default answer 0 cancel button "Cancel"

    set btn to button returned of theResult

    set nCharacters to text returned of theResult as integer

    if btn = "End" then set nCharacters to -nCharacters

     

    tell application "Finder"

              repeat with thisFile in (get document files in theFolder)

                        set theFullName to name of thisFile

                        set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}

                        if (count of text items of theFullName) > 1 then

                                  set properName to my trim((text items 1 thru -2 of theFullName) as text, nCharacters)

                                  set revisedName to ({properName, (get last text item of theFullName)}) as text

                        else

                                  set revisedName to my trim(theFullName, nCharacters)

                        end if

                        set AppleScript's text item delimiters to oldTID

                        set name of thisFile to revisedName

              end repeat

    end tell

     

    on trim(t, n)

              if n > 0 then

                        return text (n + 1) thru -1 of t

              else

                        return text 1 thru (n - 1) of t

              end if

    end trim


  • Avai Level 1 Level 1 (0 points)

    Thanks to you both for replying!  twtwtw, your modified version does exactly what I wanted!  You have just saved me so much time & headache.    Cheers!

  • mikebw Level 1 Level 1 (0 points)

    Thanks for sharing this, it really helped me out!