0
votes

In macOS, I want a folder action to trigger when I place a new file in that folder. The action should grab the filename, not including the path, and use that as the subject, and then attach the file to an email message and send it. Ideally, this would happen behind the scenes as I don't need to see the activity.

I created an Automator script that can grab the file, extract the name, create and send the file. But it's a bit of a kludge. Once I set a variable to the filename, I lose the attachment and have to get the finder item again. Also, it's not working as a Folder Action which is what I really need.

The Automator includes these steps:

  • Get Specified Finder Items
  • Get Folder Contents
  • Filter Finder Items -- I'm only interested in specific files
  • Set Value of Variable --path
  • Run Shell Script -- extract only the filename without the extension --basename "$@" .pdf
  • Set Value of Variable --fileName
  • New Mail Message --Subject: fileName

At this point I no longer can attach the specified file because Automator has 'lost' it, so I have to start over with the Get Specified Finder Items, Get Folder Contents, Filter Finder Items, Add Attachments to Front Message. Finally, Send Outgoing Messages.

What I want to happen is when I place a certain file into a directory, the Folder Action triggers, it looks at the file, and if it meets the filter criteria it emails the file, using only the filename without the extension as the Subject.

1

1 Answers

1
votes

Create an Automator document type that is a folder action, and attach it to the desired folder. Items added to the specified folder will be passed on to the workflow, so you don’t need to use additional actions to get them.

You are already saving the filtered item paths in a variable, you just need to get them back for the Mail action:

  1. Folder Action receives files added to { wherever }
  2. Filter Finder Items
  3. Set Value of Variable { Variable: path }
  4. Run Shell Script
  5. Set Value of Variable { Variable: fileName }
  6. Get Value of Variable { Variable: path } (ignore input)
  7. New Mail Message { Subject: fileName } (passed files are attached)

Automator workflows are designed to work with multiple input items as a batch; dealing with items one at a time would require a script or third party action such as Dispense Items Incrementally.