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

seabasse

macrumors newbie
Original poster
Nov 3, 2019
11
0
I want to remove everything after the last occurrence of a # until right before the file extension. It should work on files both with and without file extensions and not do anything at all if no # is found.

Ex 1.
0000 Title#10.wav should return 0000 Title.wav

Ex 2.
0010 Title 2#113 should return 0010 Title 2

Any clues on how to do it with applescript? I couldn't find any standard choice in automator that could do it.
Thanks!
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
There isn't anything built-in, so you would need to roll your own script or use a third party Automator action. I wrote an Automator action many moons ago, but a stand-alone script would be something like:

Code:
set fileItems to (choose file with prompt "Choose file items to rename:" with multiple selections allowed)

repeat with anItem in fileItems
    set {_, theName, theExtension} to getNamePieces from anItem
    set here to offset of "#" in theName
    if here is not 0 then -- found it
        try
            if here is 1 then error "The operation wasn't completed because the new name would be blank."
            set newName to text 1 thru (here - 1) of theName
            tell application "Finder" to set name of anItem to (newName & theExtension)
        on error errmess -- oops (duplicate, etc)
            log errmess
        end try
    end if
end repeat

to getNamePieces from someItem
    tell application "System Events" to tell disk item (someItem as text)
        set theContainer to the path of container
        set {theName, theExtension} to {name, name extension}
    end tell
    if theExtension is not "" then
        set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
        set theExtension to "." & theExtension
    end if
    return {theContainer, theName, theExtension}
end getNamePieces
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.