Automator action "Run Shell Script" input

I have a simple shell script which puts away files which are added to a directory. It is set up as a Folder Action/ Automator workflow. You'll notice that I've explicitly named the directory and put it in a variable "root_directory". This works fine, but…

I'd like to make the script portable so that it works on whatever folder it's attached to without me having to explicitly name the directory, but I can't work out how to pass the directory path in to the script.


User uploaded file


My guess is that it has something to do with the Pass input: setting, but this contains the actual filenames rather than the directory.


Can anyone point me to a solution?

Posted on Mar 25, 2018 1:33 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 26, 2018 2:08 PM

A folder action is triggered whenever something is added to a watched folder, with a list of the added items getting passed as arguments to the action. You can use dirname to get the directory portion of a pathname (the containing folder would be the same for all of the passed items), for example:


Run Shell Script { Shell: /bin/bash/ Pass Input: as arguments }:

for f in "$@"
do
  parent="$(dirname "$f")"
  echo $parent  # or whatever
done

Similar questions

6 replies
Question marked as Top-ranking reply

Mar 26, 2018 2:08 PM in response to ScottRankin1960

A folder action is triggered whenever something is added to a watched folder, with a list of the added items getting passed as arguments to the action. You can use dirname to get the directory portion of a pathname (the containing folder would be the same for all of the passed items), for example:


Run Shell Script { Shell: /bin/bash/ Pass Input: as arguments }:

for f in "$@"
do
  parent="$(dirname "$f")"
  echo $parent  # or whatever
done

Mar 25, 2018 5:55 AM in response to ScottRankin1960

I found my own solution…


Passing the Input (filepaths) in as arguments was the key. I then dealt with those arguments thus…


OIFS=$IFS ; IFS=

root_directory=$(dirname "${1}")

for next_file in $@ # a list of the input arguments

do

extension=${next_file##*.}

if [ ! -e "${root_directory}/${extension}" ] # no matching directory

then

mkdir ${root_directory}/${extension}

fi

mv -n "$next_file" "${root_directory}"/"${extension}" # move the file

done

IFS=$OIFS

exit 0


It works nicely.

Mar 25, 2018 7:16 AM in response to ScottRankin1960

Instead of a Run Shell Script action used within an Automator Folder Action workflow, do it in AppleScript which will give you access to the explicit folder name that the files are dropped on. When you save the .scpt from the Script Editor, put it in ~/Library/Workflows/Applications/Folder\ Actions, and then assign this .scpt to your target drop folder.


on adding folder items tothis_folderafter receivingthese_items

set drop_folder to this_folder's POSIX path

do shell script "bash -s <<'EOF' - " & drop_folder's quoted form & "

# AppleScript HERE docs require double-quotes and backslashes to be escaped

# which makes for some harsh syntax reading

OIFS=$IFS ; IFS=

root_directory=\"$1\"

for next_file in $(find ${root_directory} -maxdepth 1 -type f -not -path '*/\\.*' -print0)

do

extension=${next_file##*.}

if [ ! -e \"${root_directory}/${extension}\" ] # no matching directory

then

mkdir ${root_directory}/${extension}

fi

mv -n \"$next_file\" \"$root_directory\"/\"$extension\" # move the file

done

IFS=$OIFS

EOF"

return

end adding folder items to

Mar 25, 2018 7:46 AM in response to VikingOSX

A folder action will only fire if something is dropped into the watched folder, and the folder action script attached to any given folder is passed the paths for the items that have just been dropped into the folder, so the parent path would have to be that of the assigned/watched folder. I would also think that the AppleScript folder action handler does something similar to get the watched folder.

Mar 25, 2018 7:37 AM in response to red_menace

Your solution gets the parent path of each file that is dropped on the folder, but does not get the actual path to the name of the folder that the Folder Action script is assigned too. Unless I read Scott's request incorrectly, he wants to dynamically obtain the name of the folder that the Folder Action is assigned too, in order to avoid hardcoding it.

Mar 25, 2018 8:19 AM in response to red_menace

If I have a folder on my Desktop by the name of Remove_Words, and I drop a file from my Desktop onto that folder, the dirname path reported within the folder action script will be the original source path of the dropped file. There is no reference to the enclosing drop folder name.


One can tell Folder Actions Setup to get the name, or path of its folder action, or its script. If there is only one assigned folder in use, the name of the folder action is the drop folder's name as a list item.

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 action "Run Shell Script" input

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