Reversing Filenames with applescript

Discussion in 'Mac Programming' started by VideoBeagle, Jul 12, 2014.

  1. VideoBeagle macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    I wrote up a quick script to reverse the name of a file.
    I'm pretty pleased that I got it working without coming here.

    Now that it is working, I figured I'd show it, and see if there was a better way to do it, or a way to optimize it.

    Code:
    set thefile to choose file
    
    tell application "Finder"
    	set filename to name of thefile
    	set thePrefix to text 1 thru -5 of filename
    	set thesuffix to text -4 thru -1 of filename
    	set filenamereverse to reverse of characters of thePrefix as string
    	set newname to filenamereverse & thesuffix
    	set name of file thefile to filenamereverse & thesuffix
    end tell
    The next stage is to turn it into a service so I can right click on a file and make it happen. I'm guessing Automater is the best way to do that or could it be done directly in the script.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  3. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #3

    Uh, thanks...but I wanted to reverse the file name, not the file contents...and my script does that. Am I missing something with your suggestion of the rev command?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Yes, you're missing something. Copy and paste the example I posted into a Terminal window. Take note of the output. Predict what would happen if instead of the string I gave, you used the string that's the filename (or suffix) you want reversed.

    The shell notation >>>"string" can also be accomplished with the echo command. This command has the same results as my first example:
    Code:
    echo -n "a man a plan a canal suez" | rev
    
    Again, try different strings and observe the output. Think of it as an experiment, not as a step-by-step recipe.


    Also, your script fails if the suffix length doesn't exactly match the script's builtin (assumed) suffix-length. For example, with the filename "Baubles.pages", your script produces a name of "p.selbuaBages". and "junk.c" produces "ujnk.c".
     
  5. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #5
    Yeah, that part is a known issue.

    Code:
    set theSuffix to text ((offset of "." in filename)) thru -1 of filename
    Would deal with variable length extension but it only works IF theres only one period in the name.
    Still thinking on ways around it.


    I'll have to give more thought to your rev puzzle.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    If (offset of "." in filename) finds the first "." in the filename, then you can reverse the entire name first ("name.with.dots.suffix" becomes "xiffus.stod.htiw.eman" ). Then find the first ".", and split into two parts at that point (i.e. after the first "."). Then re-reverse the suffix ("xiffus." becomes ".suffix") and append to extant reversed "stod.htiw.eman".
     
  7. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #7
    Yep.. That's how I went about it! Yay my solution is the same as someone who knows what they're doing!!!

    Code:
    set thefile to choose file
    
    tell application "Finder"
    	set filename to name of thefile
    	set rfilename to reverse of characters of filename as string
    	set rtheSuffix to text 1 thru ((offset of "." in rfilename)) of rfilename
    	set thesuffix to reverse of characters of rtheSuffix as string
    	set filenamereverse to text ((offset of "." in rfilename) + 1) thru -1 of rfilename
    	set name of file thefile to filenamereverse & thesuffix
    end tell
     
  8. tkermit, Jul 13, 2014
    Last edited: Jul 13, 2014

    tkermit macrumors 68040

    tkermit

    Joined:
    Feb 20, 2004
    #8
    You may want to deal with files that don't have a name extension or have trailing dots in their name. My approach would be something like the following, whereby any trailing dots from the original file name (sans suffix) are removed in order to prevent the renamed file from becoming hidden.

    EDIT (2): a bit more straightforward than what I had originally proposed :

    Code:
    set theFile to choose file
    set {nameOfTheFile, nameExtensionOfTheFile} to the {name of (info for theFile), name extension of (info for theFile)}
    
    if nameExtensionOfTheFile is missing value then
    	set reversedName to reverse of characters of (text 1 thru -1) of nameOfTheFile as text
    else
    	set reversedName to (reverse of characters of (text 1 thru ((length of the nameOfTheFile) - (length of the nameExtensionOfTheFile) - 1)) of nameOfTheFile as text) & "." & nameExtensionOfTheFile
    end if
    repeat while reversedName starts with "."
    	set reversedName to (text 2 thru length of reversedName) as text
    end repeat
    
    tell application "Finder" to set name of theFile to the reversedName
     
  9. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #9
    Huh.. I didn't know you could do that (assign extension from the "info")

    I'll have to look over the code to understand what all you do. (This was always just a learning/can i do it exercise).

    This bit: "name of (info for theFile)" solved the problem I had getting my script to work as a Finder Service, so awesome that.

    (In playing with your code in this form:
    Code:
    set theFile to choose file
    #set {nameOfTheFile, nameExtensionOfTheFile} to the {name of (info for theFile), name extension of (info for theFile)}
    set xten to name extension of theFile
    I got the same error the applescript in automator had been giving me..so a little extra typing, and it works!)
     

    Attached Files:

  10. Stangg macrumors newbie

    Stangg

    Joined:
    Oct 14, 2017
    #10
    OK I wanted to make this work when files are dropped on the Applescript program.

    I used some code from this and a script that takes dropped files, and the result works fine.

    Code:
    on open theFiles
        repeat with theFile in theFiles
            set {nameOfTheFile, nameExtensionOfTheFile} to the {name of (info for theFile), name extension of (info for theFile)}
            
            if nameExtensionOfTheFile is missing value then
                set reversedName to reverse of characters of (text 1 thru -1) of nameOfTheFile as text
            else
                set reversedName to (reverse of characters of (text 1 thru ((length of the nameOfTheFile) - (length of the nameExtensionOfTheFile) - 1)) of nameOfTheFile as text) & "." & nameExtensionOfTheFile
            end if
            repeat while reversedName starts with "."
                set reversedName to (text 2 thru length of reversedName) as text
            end repeat
            
            tell application "Finder" to set name of theFile to the reversedName
        end repeat
    end open
     

    Attached Files:

Share This Page