Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
823
18
App Q&A testing by request.
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.
 
See the 'rev' command:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/rev.1.html

Terminal command-line example:
Code:
rev <<<"a man a plan a canal suez"


rev -- reverse lines of a file
DESCRIPTION
The rev utility copies the specified files to the standard output, reversing the order of characters in
every line. If no files are specified, the standard input is read.

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?
 
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?

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".
 
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".

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.
 
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.

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".
 
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".

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
 
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
 
Last edited:
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!)
 

Attachments

  • Screenshot 2014-07-14 17.44.00.png
    Screenshot 2014-07-14 17.44.00.png
    90.4 KB · Views: 660
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
 

Attachments

  • Reverse Droppedfiles.app.zip
    54.3 KB · Views: 678
  • Like
Reactions: partyfart
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.