# File Renaming solution?



## composerguy78 (Nov 4, 2017)

I currently use A Better Finder rename to append a cue code to the front of my stems. However, I am looking for a solution where I can just drag and drop a bunch of stems and then have the droplet ask me what the text to add is eg.1m01. 

Does anyone know of a script or automator app or something that could do this? 

I am looking for a drag and drop solution!


----------



## anp27 (Nov 4, 2017)

composerguy78 said:


> I am looking for a drag and drop solution!



I use the Rename app. It's free.


----------



## Spip (Nov 5, 2017)

I've created a simple service in Automator and assigned it to a shortcut in System Preferences (keyboard>Shortcuts>Services)

1. Get Selected Finder Items
2. Rename Finder items : Add Text

You just have to select the files, type the shortcut and enter the text.

Hope this helps.


----------



## composerguy78 (Nov 6, 2017)

Thanks very much guys! 

What I am really looking for is to save an Automator application like this one spip just very kindly posted as an application I can just drag and drop the files onto from the finder. The issue here is that the text to append is different each time so I would like it to prompt for the text each of time. I don’t think Automator will do that the way I need it to. 

I realize I am being Uber lazy! It would be so nice to eliminate whichever steps I can from my workflow!


----------



## Joakim (Nov 7, 2017)

You inspired me to write a few lua scripts for renaming files but I'm unfortunately on windows.

there is probably a simple solution for mac already but here we go:

i had to use google short links as the forum would not allow me to post dropbox and or github links.

video of drag and drop functionality: https://goo.gl/apXKVz

the lua scripts are wrapped in self containing exe files using a tool called srlua. I tested it on linux as well and it works fine.
https://goo.gl/pjpFqt

if you are comfortable with compiling yourself it shouldn't be too difficult to get it to work. Otherwise maybe automator can be setup to run lua scripts directly or someone with os x could help you.

the scripts can of course be run directly in the lua interpreter but then you can't directly drag and drop your files.

here are the scripts..

prefix:

```
print(#arg .. " files selected.")

io.write("prefix: ")
-- read input for text to input1
input1 = io.read()

for i=1, #arg do
    -- find filename in path string
    tmp = arg[i]:reverse():find("\\")
    if tmp ~= nil then
        file_path = arg[i]:sub(1, -tmp)
        file_name = arg[i]:sub(-tmp+1)
    else
        file_path = ""
        file_name = arg[i]
    end
 
    local old_name = file_path..file_name
    local new_name = file_path..input1 .. file_name
    -- rename file
    status, error = os.rename(old_name, new_name)
    if status ~= nil then
        print("new file: " .. new_name)
    else
        print("failed to rename file. ", error)
    end
end
```

replace:

```
print(#arg .. " files selected.")

io.write("source: ")
-- read input for text to input1
input1 = io.read()

io.write("replace: ")
-- read input for text to input2
input2 = io.read()

for i=1, #arg do
    -- find filename in path string
    tmp = arg[i]:reverse():find("\\")
    if tmp ~= nil then
        file_path = arg[i]:sub(1, -tmp)
        file_name = arg[i]:sub(-tmp+1)
    else
        file_path = ""
        file_name = arg[i]
    end

    local old_name = file_path..file_name
    local new_name = file_path..file_name:gsub(input1, input2, 1)
    -- rename file
    status, error = os.rename(old_name, new_name)
    if status ~= nil then
        print("new file: " .. new_name)
    else
        print("failed to rename file. ", error)
    end
end
```

suffix:

```
print(#arg .. " files selected.")

io.write("suffix: ")
-- read input for text to input1
input1 = io.read()

for i=1, #arg do
    -- find filename in path string
    tmp = arg[i]:reverse():find("\\")
    if tmp ~= nil then
        file_path = arg[i]:sub(1, -tmp)
        file_name = arg[i]:sub(-tmp+1):match("[^%.]+")
        file_ext = arg[i]:sub(-tmp+1):match("[%.]%g+")
    else
        file_path = ""
        file_name = arg[i]:match("[^%.]+")
        file_ext = arg[i]:match("[%.]%g+")
    end
    print(file_name, file_ext)
 
    local old_name = file_path .. file_name .. file_ext
    local new_name = file_path .. file_name .. input1 .. file_ext

    -- rename file
    status, error = os.rename(old_name, new_name )
    if status ~= nil then
        print("new file: " .. new_name)
    else
        print("failed to rename file. ", error)
    end
end
```


----------



## composerguy78 (Nov 7, 2017)

Wow! Thank you Joakim! I was not expecting that. 

I will try to get this to work on my mac - I'll talk with some friends who may know how to edit this for applescript perhaps?


----------



## Joakim (Nov 7, 2017)

composerguy78 said:


> Wow! Thank you Joakim! I was not expecting that.
> 
> I will try to get this to work on my mac - I'll talk with some friends who may know how to edit this for applescript perhaps?



most scripting languages should be capable of this relatively simple task, then the issue is just getting drag and drop to work.


----------



## composerguy78 (Nov 7, 2017)

okay thank you!

F


----------

