2

so I have this apple script here that I used to insert the folder name into the front of all the files in the folder. The script is able to run through every single folder and target all the files and proved to be very useful

    set the_folder to (choose folder)
renamefilesinside(the_folder)

on renamefilesinside(next_folder)
    tell application "Finder"
        set folder_name to name of next_folder & " "
        set folder_contents to every item of next_folder
        repeat with each_item in folder_contents
            if kind of each_item is "folder" then
                my renamefilesinside(each_item)
            else
                set name of each_item to folder_name & " " & name of each_item
            end if
        end repeat
    end tell
end renamefilesinside

Source: https://discussions.apple.com/thread/275167

However, now I have removed a part of the folder names and would like a subtractive effect to be applied to the files, so the script will take the files, look at the parent folder's name, and remove the shared characters.

Unfortunately I know very little about apple scripts so I'm hoping this is an easy adjustment that someone here can make.

Thanks for any help you can give!

1

You can get the file name, then set the text item delimiter to the containing folder name, then put the text items of the file name into a list, which will ignore the delimiter (the folder name), then reset the text item delimiter to "" empty and coerce the name to a string. This is a VERY common Applescript trick.

set the_folder to (choose folder)
renamefilesinside(the_folder)

on renamefilesinside(next_folder)
    tell application "Finder"
        set folder_name to name of next_folder
        set folder_contents to every item of next_folder
        repeat with each_item in folder_contents
            if kind of each_item is "folder" then
                my renamefilesinside(each_item)
            else
                set n to name of each_item
                set AppleScript's text item delimiters to folder_name
                set n to text items of n
                set AppleScript's text item delimiters to ""
                set n to n as string
                set name of each_item to n
            end if
        end repeat
    end tell
end renamefilesinside
2
  • Sorry, I'm not quite following what I need to do with the script. I was hoping the script would work much the same as the initial one- you just select the folder that contains all the folders (I have several) and the files within those folders will be renamed by subtracting the folder name from the file name.
    – shaunxer
    Oct 12 '15 at 17:25
  • There were some errors in the script in potential situations. I have edited the script to improve it. Sorry about that. It now works just like you said. You select a folder once you run the script, and it will then eliminate the folder's name string from any of the files contained in that folder.
    – jweaks
    Oct 12 '15 at 18:31

You must log in to answer this question.

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