2

Im creating an automator service that transcodes all the files within a folder to a compressor preset by running the following shell script:

VAR="/Applications/Compressor.app/Contents/MacOS/Compressor -batchname MyFirstBatch"
    for d in "$@"; do
        filename=$(basename "$d")
        fname="$(dirname "$d")"/"${filename%.*}"
        VAR+=" -jobpath "$d" -settingpath /Applications/Compressor.app/Contents/Resources/Settings/ProRes/proResHQName.setting -locationpath "$fname".mov"
    done
eval $VAR

The problem is that the resulting paths in the string have a blank space instead of backslash and blankspace, so I want to replace every " " in the d variable with a "\ ".

Also, is there any way to omit folders and only consider files?

Thank you.

Here's a screenshot of my workflow (Get specified Finder Items and Get folder Contents are just for testing purpose, I disable them when using it as a service):

Screenshot of Automator Service

  • Please don't use eval like this, it can wind up doing really weird things if any filenames contain shell syntax. Instead, construct the command in an array. See BashFAQ #50 and this question. – Gordon Davisson Jul 4 at 22:39
  • We do not have enough information to determine the reason for the error. Do you have a link to the compressor app? I ran the script and don't see an issue. I replaced the eval with an echo and got: /Applications/Compressor.app/Contents/MacOS/Compressor -batchname MyFirstBatch -jobpath texif -settingpath /Applications/Compressor.app/Contents/Resources/Settings/ProRes/proResHQName.setting -locationpath ./texif.mov – historystamp Jul 5 at 3:20
  • I don't see the reason for the eval. Just run compressor direct. Am I missing something? – historystamp Jul 5 at 3:24
  • Thanks @GordonDavisson for your suggestion. I'll try to implement it with an array and update my post accordingly. – Sergio Villa Kramsky Jul 5 at 3:25
  • Could you specify what the input into this script is supposed to be. I had to guess. :-( – historystamp Jul 5 at 3:25
3

I don't have Compressor.app to test with, but this should work:

fileargs=()
for d in "$@"
do
    [ -f "$d" ] || continue
    filename=$(basename "$d")
    fname="$(dirname "$d")"/"${filename%.*}"
    fileargs+=(-jobpath "$d" -settingpath /Applications/Compressor.app/Contents/Resources/Settings/ProRes/proResHQName.setting -locationpath "$fname".mov)
done

/Applications/Compressor.app/Contents/MacOS/Compressor -batchname MyFirstBatch "${fileargs[@]}"

EDIT: If you want to restrict it to just specific file extensions (.mov, .mp4, and .mxf), you can use this instead:

fileargs=()
for d in "$@"
do
    [[ "$d" =~ [.](mov|mp4|mxf)$ ]] || continue
    [ -f "$d" ] || continue
    fileargs+=(-jobpath "$d" -settingpath /Applications/Compressor.app/Contents/Resources/Settings/ProRes/proResHQName.setting -locationpath "${d%.*}".mov)
done

/Applications/Compressor.app/Contents/MacOS/Compressor -batchname MyFirstBatch "${fileargs[@]}"

Note that with this restriction, the filename is guaranteed to have an extension, so the ${d%.*}.mov trick that @user3439894 suggested is safe to use. Also, is it ok to use the the same -jobpath and -locationpath names? (This'll happen when both the input and output files are .mov.)

  • Escaping spaces is the wrong way to do it; an array (and proper double-quoting) work much better. As for the dirname/basename bit, ${d%.*}.mov behaves weirdly if the filename doesn't have an extension, but there is a "." somewhere up the directory path; splitting it and applying the %.* only to the file part handles this correctly. – Gordon Davisson Jul 5 at 6:47
  • @user3439894 I don't have Compressor, but generally programs like that will work on files even if they don't have an extension. For example, d="/pa.th/to/foo_bar"; echo ${d%.*}.mov prints /pa.mov. Yes, it's a weird case, but I prefer to use robust idioms wherever possible. – Gordon Davisson Jul 5 at 7:10
  • @user3439894 That's a good idea, I'll add it. – Gordon Davisson Jul 5 at 8:06
  • That worked! Thank you! One last thing do you know how can I take into account only .mov, .mp4 and .mxf files? I tried doing multiple conditions to no avail. – Sergio Villa Kramsky Jul 5 at 18:32
  • @SergioVillaKramsky See update. Also, is it ok to use the the same -jobpath and -locationpath names? (This'll happen when both the input and output files are .mov.) – Gordon Davisson Jul 6 at 6:31
0

take into account only .mov, .mp4 and .mxf files

I modified the above script by Gordon Davisson. https://apple.stackexchange.com/a/363864/44531

This may need modification. I didn't understand the -locationpath option. I do not have compressor, so I merely echo the last line. Set debug to false when you get tired of the output. I no longer w-locationpathrite code without debug statements.

#!/bin/bash
# question:
# https://apple.stackexchange.com/questions/363854/help-to-replace-a-blank-space-with-a-backslash-and-blankspace-in-a-variable-in-r
# compressor options:
# https://support.apple.com/kb/PH21248?viewlocale=en_KE&locale=en_KE

debug=true
fileargs=()
for d in "$@"
do
    [ debug ] && echo -e "\nProcessing file ${d}"

    filename=$(basename "$d")
    justname="${filename%.*}" 
    # Works for files with no extension
    fileext=""
    [[ "${filename}" == *"."* ]] &&  fileext="${filename##*.}" 

    #  Process these allowed extensions .mov, .mp4 and .mxf 
    [ "${fileext}" = "mov" -o "${fileext}" = "mp4" -o "${fileext}" = "mxf"  ] || continue

    [ debug ] && echo "filename is ${filename}   fileext is ${fileext}    justname is ${justname}"

    # make sure the file exists.
    [ -f "$d" ] || continue

    dirname=$(dirname "$d")
    [ debug ] && echo "dirname is ${dirname}"
    fname="${dirname}/${justname}"
    [ debug ] && echo "fname is ${fname}"

###
### I do not know what you want for locationpath 
###
 fileargs+=(-jobpath "$d" \
      -settingpath /Applications/Compressor.app/Contents/Resources/Settings/ProRes/proResHQName.setting \
      -locationpath "${fname}.${fileext}.mov")
done

echo /Applications/Compressor.app/Contents/MacOS/Compressor -batchname MyFirstBatch "${fileargs[@]}"

You must log in to answer this question.

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