Search code examples
automator

How to use paths with spaces in automator shell script?


I have an Automator workflow which should set the CreationDate of the selected file to a date which I want to enter. Unfortunately my path have spaces. Therefore it doesn'w work. I've tried several variants like:

SetFile -d \"$1 12:00:00\" "$@2"
SetFile -d \"$1 12:00:00\" \"$@2\"
SetFile -d \"$1 12:00:00\" '$@2'
SetFile -d \"$1 12:00:00\" \'$@2\'

The path is like follows: /Users/simon/Documents/Steuern/Steuern 2021/Scan_000775.pdf

The shell I use is ZSH with oh-my-zsh installed.

This is the Workflow I have:

  • Ask for Finder-Object
  • Get value of variable
  • Ask for Text input
  • Set value of variable
  • Ask for value of variable
  • execute shell script: "SetFile -d "$1 12:00:00" "$@2""

enter image description here

Can anyone tell me how to write the shellscript to use pathnames with spaces? That would be very nice. Thank you.


Solution

  • If I'm understanding right, you want:

    SetFile -d "$1 12:00:00" "${@:2}"
    

    Explanation: escaping quotes prevents them from acting like quotes (it turns them into normal characters); in this case, you want them to function as quotes, so you shouldn't escape them. Also, "$@2" doesn't get the arguments starting at $2, it gets all of the arguments, and sticks a "2" to the end of the last one. If you want all the arguments except the first, use "${@:2}" instead.