Watching a folder, then running a script

Discussion in 'Mac Programming' started by macstatic, Jan 31, 2016.

  1. macstatic macrumors 6502a

    Oct 21, 2005
    I believe it's possible to "watch" a specific folder using Automator, and when a file appears in that folder further action can proceed, but I don't understand exactly how to get this done.

    What I want is a "Scanned images" folder to be watched, and when a new image is scanned and appears in that folder a script with several terminal commands are run (basically calling the command line application Exiftool to create an EXIF date/time tag (usually missing from scanned images) and copy the OSX file Creation date to this tag) and finally rename the file by adding the date/time to the filename (based on the newly created EXIF time/date tag) in the format YYYYMMDD-HHMMSS_originalfilename).

    This is done by selecting "Folder Action" as a new Automator file, isn't it, then select the folder to be watched, right (as seen below)?
    Screen Shot 2016-01-31 at 21.41.44.png

    I suppose the next step is to define what should be done when a file enters that folder, which would probably be "Run shell script", but since I don't know much about that part yet I decided to experiment with "Rename Finder items" where I defined the renaming process and pressed the Automator "Run" button and expected the action to start, but it only seems to run once then stop, and no file is being renamed if I put it in the folder.
    So what am I doing wrong, and when done configuring, how do I actually get the script to start and run all the time? Should this be a "Login item" (System preferences-Users & groups-Login items)?
    I'm using OSX 10.9.5.
  2. Red Menace macrumors 6502

    May 29, 2011
    Littleton, Colorado, USA
    A Folder Action workflow is what you want. The items added to the folder will be passed to the workflow - a list of items is passed even if there is only one item, so if you are going to use a shell script or AppleScript action you will need to handle each of the items in the list. Also note that you should not do things such as renaming an item while it is still in the watched folder, since that will trigger the workflow again.
  3. macstatic thread starter macrumors 6502a

    Oct 21, 2005
    You mean, because it gets a new name it will be treated as a new file, then have it run through the whole procedure again and creating an endless loop?
    Does that apply if I change the file itself as well (i.e. add the EXIF tag I mentioned)?

    Perhaps my best option would be to create (or use an existing one in OSX) a "Scans -temporary" folder which the Automator script watches, and when a file appears it gets copied over to a separate "Scans -complete" folder where the actual EXIF data is added and filename changed, or am I missing the point?

    But how do I start the Automator action? Is it actually pre-defined to start in OSX just by creating a "Folder action" in the first place?
  4. Red Menace macrumors 6502

    May 29, 2011
    Littleton, Colorado, USA
    I don't know about EXIF data, but renaming a file definitely triggers the action again - regular tags and permission changes don't. Changing the file itself shouldn't trigger a folder action, but note that some applications will do things like rename an existing file, save a new file with that name, and then delete the old file.

    Any currently running folder action script is interrupted, so there isn't anything like an endless loop, the script just doesn't complete. If you need to do anything that would trigger a folder action, such as renaming or duplicating a file, the normal procedure is to move it out of the watched folder somewhere. You can also use a folder inside the watched folder, since folder actions do not go into subfolders (a subfolder can have its own folder actions, though).

    The OS is what watches the file system, and triggers any attached folder action scripts or workflows. The scripts or workflows are kept in the ~/Library/Scripts/Folder Action Scripts and ~/Library/Workflows/Applications/Folder Actions folders respectively, and are managed with the Folder Actions Setup service.
  5. chown33 macrumors 604

    Aug 9, 2009
    Why reinvent the wheel from scratch? Use examples from others.

    Google search terms:
    exiftool folder action

    An example result:

    Find more results by doing the search yourself.

    If you try something you found and it doesn't work, then post again here, providing:
    1) The URL of what you found and tried.
    2) Exactly what error message you got, if any.
  6. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    If I remember rightly, exiftool creates a temp file and then either overwrites or copies it to the location of the source file (see the overwrite_original and overwrite_original_in_place options in the exiftool help page). So I guess that might possibly confuse folder actions. A safer option might be to save the tagged files out to a location which isn't watched by your folder action.

Share This Page