Automator remove characters beginning/end

I want to remove characters at the end of my file. Is it passible with apple scripts in Automator? Can you give me a example or scripts?


I have found this script but it removes numbers at the beginning of a files.. Many thanks!


on run {input, parameters}


repeat with thisFile in input

tell application "Finder"

set fileName to name of (thisFile as alias)

set fileName to (do shell script "echo " & quoted form of fileName & " | sed 's/^[0-9]*//'")

set name of thisFile to fileName

end tell

end repeat


return input

end run

Posted on Oct 9, 2011 6:22 AM

Reply
11 replies

Apr 19, 2017 5:15 PM in response to red_menace

Thanks for the help (can't find your Automator Homepage, btw.)

I used "Get Specified Finder Items" and dragged in my files.

The script took off the 3digit number from the front but left the space

890 filename.mp3 to [space]filename.mp3

I tried different adjustments but since I don't understand the language I can't figure how to get rid of the space. Any suggestions?

Oct 9, 2011 9:44 AM in response to vincent fromamsterdam

To remove numbers at the end of the name (preserving any extension), change your script to:


on run {input, parameters}

repeat with thisFile in input
tell application "Finder"
set {theName, theExtension} to {name, name extension} of thisFile
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers
set name of thisFile to theName & theExtension
end tell
end repeat

return input
end run


I also have a Change Name of Finder Items action on my Automator homepage that can remove characters from a file name - it doesn't use regular expressions, but can strip a specified number of characters from the beginning or end.

Jul 31, 2013 7:42 AM in response to vincent fromamsterdam

Hi

I'm new in this & I want to use the script above where it removes numbers at the begining of the filename in the Automator, however the 1st line below gets stuck


on run {input, parameters}


Ideally I want to use the following workflow


"Ask for Finder Items"

Run the AppleScript above but modify it so I can select the files

Then maybe Reveal Finder Items etc.


Can someone please advise the correct format & flow as been seaching all day but with no luck

Also it would be nice to know how to run the above script on its own, I copied it in AppleScript editor pressed Run & it came with an error!!

Thx

Jul 31, 2013 5:37 PM in response to Alist4ir

The script works for me (and the OP), so without something more specific (like the script used or an error message), there isn't much of a way to tell what the problem is.


The posted scripts were designed to be used in Automator's Run AppleScript action. To use them in the AppleScript editor, remove the {input, parameters} part of the run handler declaration and set the input variable to the items you want to work with.

Aug 1, 2013 4:25 AM in response to red_menace

Thank you for your quick response, as I meantioned I'm very new in this.


I created a workflow in Automator with the Run AppleScript action & pasted the script above, compiled it & when I ran it, it came with the Syntax Error:

No result was returned from some part of this expression.

(the alias was also highlighted in the script)


it didnt offer me to select the files so I guess thats why the error


when you say:

remove the {input, parameters} part of the run handler declaration and set the input variable to the items you want to work with.


What is the exact command / phrase I have to replace it with if I want it to select the files in a specific folder?

Thank you in advance for your help.

Aug 1, 2013 5:13 PM in response to Alist4ir

When using Automator, the Run AppleScript action will need to be supplied items to work with, so you can use something like the Ask for Finder Items or Get Selected Finder Items actions to get files to pass to the script.


To use the script outside of Automator, you can change the beginning of posted script to:


onrun


setinputto (choose filewithmultiple selections allowed)


repeatwiththisFileininput

--


The choose file command will put up a file selection dialog so that you can navigate and select the items you want to use with the script.

Aug 5, 2013 11:11 AM in response to Alist4ir

Each individual action in an Automator workflow (usually) gets its input from the previous action, and sends its output (the result of whatever it does) to the next action for it to use. If using the Ask for Finder Items action right before the Run AppleScript action (the original script should work without modification), it will send a list of items chosen in the file dialog to the script - these input items are passed to the run handler in the parameter named input.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator remove characters beginning/end

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.