I have a folder with different 100 rtf-text-files in a folder.
I would like to have Automator to take the content of one text-file and send out it out via mail once a day to the same mailing-addresses – without sending the same text twice.
Because I am a newbie and don’t find a way to filter out only one rtf file with Automator’s predefined actions, I think I have to create a script.
What I’ve got so far:
New Application:
- Find Finder-Item: I chose the folder with the files. Condition: File extension is rtf —> now I’ve got all (3 test-)rtf’s as result.
(*)
- Open Finder-Item: Open with default application
- Get Contents of TextEdit Document
- New Mail Message
What this workflow does: It opens all rtf-files in TextEdit. Then a new Mail-Window opens and displays the content of the last opened textfile. Of cause it does’t send it.
I think I have to include a step/script where the (*) is, so Automator has a specific rtf-file to pass to the next steps.
Can you please help me to find this step or show me how to create a script? Or maybe you know another solution?
Best Answer
When wanting to randomly choose from a group of files, to send the contents thereof as an email, while ensuring the content from each file is used only once, requires maintaining a list of filenames from which the content has already been sent. There are quite a few different ways this list can be created, accessed and maintained.
Staying away from Automator and using AppleScript for the entire process, the easiest would be as an AppleScript
property
variable, however an AppleScriptproperty
variable gets reset if the script is recompiled in Script Editor and or saved again after it's been originally saved.This is not necessarily a problem, if once the script has been properly configured to meet the needs and is functioning properly during testing, it is then saved as an application and not modified or saved again after the initial run of it having been saved as an application. If that might be an issue, then the list will need to be saved as a disk file. (Personally, I'd choose to use the disk file.)
Saving the list of filenames from which the content has already been emailed as a disk file can also be done in a number of ways. You essentially need to pick one of the possible ways and code it accordingly.
The following AppleScript code examples, shows two ways to accomplish the overall task. The first, using an AppleScript
property
variable and the second using a disk file, to maintain the list.In the first example AppleScript code, there are some variables which you'll need to set:
thisFolder
- The location of the folder containing the 100 files.theRecipientName
- The Name of the Email Recipient.theRecipientAddress
- The Email Address of the Email Recipient.theSubject
- The Subject of the Email Message.In the second example AppleScript code, in addition to the variables shown above:
theAlreadySentListFilename
- The location and name of the file containing the filenames from which the content has already been sent.The remainder of the script is coded in a manner (tokenized) to work with these variables and others set within the script. Also, the
tell application "Mail"
block of code is coded to send the message automatically, however it has been commented out, until testing is done and or if not wanting to automatically send. Uncomment accordingly.To use this script, in Script Editor, create a new document and save it as an application in /Applications, naming it as, e.g. 100 Messages to Send.app.
Next, copy and paste the example AppleScript code below into the 100 Messages to Send.app in Script Editor and hit the Compile button on the Toolbar.
Modify the variables, mentioned earlier above.
Save the app and test from within Script Editor.
Once done with testing, hit the Compile button on the Toolbar to reset the
theAlreadySentList
AppleScriptproperty
variable to an empty list, then save and close the 100 Messages to Send.app so as not to reset thetheAlreadySentList
AppleScriptproperty
variable once production mode begins.First example AppleScript code:
In this second example AppleScript code, a disk file is used to store the list of filenames from which the content has already been sent as an email message.
To use this script, in Script Editor, create a new document and save it as an application in /Applications, naming it as, e.g. 100 Messages to Send.app. This is done so you can add a plain text file to the application bundle to use for the list of filenames already sent. (I prefer to keep related support files within the application bundle.)
Having saved the document as an application, now open the application bundle (in Finder, right-click Show Package Contents) to Contents/Resources/ and create the, e.g. Sent Messages Filename List.txt file as an empty file, and then close the application bundle.
Next, copy and paste the example AppleScript code below into the e.g. 100 Messages to Send.app in Script Editor and hit the Compile button on the Toolbar.
Modify the variables, as mentioned earlier above.
Save the app and test from within Script Editor.
Once done with testing, clear the entries from the Sent Messages Filename List.txt, so it's an empty file when running the app for the first time in production mode.
Second example AppleScript code:
NOTE: If you are going to use Plain Text files instead of the Rich Text files, then the entire
tell application "TextEdit"
block of code can be replaced with the following two lines of code:You can also change the comment above it,
-- # Get the content of the Rich Text file.
, swapping Rich with Plain.