1

I'm trying to create an Apple Automator application that will rename a group of files by removing all text before and including a hyphen ( - ) that will post an error if trying to rename a file with no hyphen.

ex:

input is 001-10312233_033

output is 10312233_033

Then if it ran again on the same file (10312233_033) would produce an error or warning as there is no hyphen.

Any ideas what script in Automator will make this happen?

1

You can use the do shell script from within AppleScript to invoke a standard UNIX CLI such as awk, sed or grep. In this case since you merely want to find all occurrences of *- from the beginning of your string, I'd enlist sed to do this.

Something like this will remove everything up to the first dash (-):

set output to do shell script "echo " & quoted form of input & " | sed 's/.*-//'"

Example

Say we had this AppleScript:

set input to "001-10312233_033"
set output to do shell script "echo " & quoted form of input & " | sed 's/.*-//'"
log "input:  " & input
log "output: " & output

This sets your example string as the variable input. The 2nd line then takes the variable input and echoes the variable to the sed command | sed 's/.*-//'. This will strip everything (.*) up to the first dash, (-), encountered, and replaces it with nothing (//) in sed.

The last 2 commands with the log merely echo the 2 variables, input & output.

Here's the whole thing in action:

$ cat regex.sh
#!/bin/bash

osascript <<END
set input to "001-10312233_033"
set output to do shell script "echo " & quoted form of input & " | sed 's/.*-//'"
log "input:  " & input
log "output: " & output
END

Make sure the script regex.sh is executable:

$ chmod + regex.sh

And we run it like this:

$ ./regex.sh
input:  001-10312233_033
output: 10312233_033

You must log in to answer this question.

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