0

I'm trying to execute a pretty simple renaming script, but am unable to do so with an Automator workflow and my scripting knowledge is totally inadequate (this is ultimately for use with Hazel). The equivalent that I'm trying to execute in Name Mangler is to:

'remove 2 characters starting at index 1 - counting from start'

and

'remove 2 characters starting at index 2 - counting from end'

Any assistance would be appreciated; AppleScript, Java and Shell are all fine as this will be embedded in Hazel.

2
  • Can you give a few examples of the original file names and the expected new file names?
    – fd0
    Commented Aug 2, 2016 at 10:28
  • How is indexing defined, what is index 1, index 2, etc.? Commented Aug 2, 2016 at 10:47

2 Answers 2

1

UPDATE: I misread your question and originally gave an answer that started at "character 1." If I understand correctly you want to trim off the first two characters, and the last two. Therefore, you want to start at "character 3." I have made the correction here.

Do it this way:

set the_short_name to characters 3 thru -2 of the_original_name as string

That's all there is to it.

Since you're working with Hazel you can get the name of the file this way:

set the_original_name to name of theFile

You may want to pull off more than just two characters since the last four characters are probably ".png" so take off 6 characters, and then add ".png" to the short name.

One more thing: Hazel may think that the renamed file is a "new" file, and consequently run the AppleScript on the renamed file over and over until the name is so short it can't get shorter. The way I'd handle that is I'd move the file out of the watched folder first, and rename it after that. In my case I have a folder called "Renamed Screenshots" on my desktop-- your path is going to be different. Use your own username (not "youruser" as I've put in the line below).

move theFile to (POSIX file "/Users/youruser/Desktop/Renamed Screenshots")
        set the name of theFile to the_short_name & ".png"

That should do it.

0

Here is a one line bash command that will do this for you:

for file in *.png; do ext=${file##*.}; fname="`basename $file .${ext}`" ; len=`expr ${#fname} - 4`; fname="${fname:2:${len}}"; fname="${fname}.${ext}"; mv -- "${file}"  "${fname}"  ;done

This command will separate out the filename from the extension, calculate the length of the new string, then remove the first two characters and last two characters then "move" the original file to the newly named file.

Running this command on some PNG files in a "Test Folder"

Before:

enter image description here

After:

enter image description here

6
  • @user You can change the mv to cp to not overwrite, but that will double your files.
    – Allan
    Commented Aug 4, 2016 at 13:58
  • As I previously said "As written, this answer uses no error checking and has to potential to overwrite files without warning!" and changing mv to cp is IMO not in line with the desired results and the method wanting to employ the OP has stated. Set said the OP's desire, posting code that has the potential to be destructive because it doesn't employ error checking is at the least, bad form. Commented Aug 4, 2016 at 14:46
  • @user3439894 The OP specifically asked for a pretty simple renaming script script. Your comment is pure conjecture at best as to what "was desired." Feel free to support your argument that it's incongruent with what the OP wants; not incongruent with what you want.
    – Allan
    Commented Aug 4, 2016 at 14:58
  • Because I did say IMO I also said "Set said the OP's desire" and the part you're failing to comprehend is your code, as written, is flawed in that it absolutely can overwrite files and employs absolutely no error checking to prevent it! Simple and destructive is not a worthy answer (in this use case anyway). Commented Aug 4, 2016 at 15:06
  • @user3439894 You are free to write your own answer, you know.
    – Allan
    Commented Aug 4, 2016 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .