Search code examples
bashshelldirectoryosx-elcapitanautomator

Folder action not triggering shell script


I have some software that exports a file called My Library.bib to a folder called thesis. Assume that the name of the exported file is fixed. Every time I export such a file, I want to:

  1. Delete any old files called MyLibrary.bib if they exist.
  2. Remove the space from this new file so that it becomes the up-to-date MyLibrary.bib.

I've tried making an Automator 'folder action' as follows:

folder action

... However, while the shell script works perfectly if run manually, the folder action itself never appears to trigger.

Folder actions are nonetheless enabled (see below settings), and other folder actions do seem to work.

folder actions setup

Summarily, I just want any files named My Library.bib entering the thesis folder (at any time, automatically) to become renamed to MyLibrary.bib, replacing any existing MyLibrary.bib files. Any ideas what's going wrong, or how else to achieve this? Thanks in advance.


Solution

  • When you use the "Run Shell Script" action, the current directory is the Home folder, not the "thesis" folder.

    So, you must use the cd command to change the current directory

    Informations:

    • The "Get Folder Content" action is useless for what you want to do, you can remove it.
    • The rm command is not necessary, you can use the mv -f to overwrite an existing file

    read firstLine ### get the path of the first dropped item
    myDir=$(dirname "$firstLine") ### get the parent (this folder action)
    cd "$myDir" && if [ -f "My Library.bib" ]; then 
        mv -f "My Library.bib" "MyLibrary.bib"
    fi