Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator if/then conditional action possible?

I have a database that ought create a certain file in a certain folder every day. Sometimes it fails to create the file and I want to be notified of this failure, preferably by email.


I thought I could use Automator to specify the folder (via the action 'Get Specified Finder Items') and and filter for the failure (via the action 'Filter Finder Items') by 'Date last modified is not in the last 1 days', and then email me (via actions 'New Mail Message' and 'Send Outgoing Messages'). Finally, the workflow would be saved as a Calendar Alarm to run every day in the Calendar app.


However, the email always gets created regardless of whether the date modified is within the last 1 day or not because there is no conditional (eg. if...then...) clause in the workflow to prevent it. The input for the action New Mail Message is any text or file, and, in the absence of such, it still creates the mail but without any appended text or file.


I only want a new Mail message if the folder's 'date last modified' is not in the last 1 day. How is this possible? Simply stopping the workflow, if the condition is not met, would suffice.

(In case it matters: running macOS 10.13.6 High Sierra)

MacBook Pro

Posted on Dec 3, 2021 3:30 AM

Reply
Question marked as Best reply

Posted on Dec 3, 2021 9:00 AM

Unfortunately Automator has no conditional logic control - even its Loop action is less use than a chocolate fireguard.


I would suggest writing an AppleScript to check the folder and send the mail, then save the script as an AppleScript application, and finally set up a scheduled alert in Calendar to open the script app file, which will then run. It should get the result you need without involving Automator at all.


I shall have a think about a script...

Similar questions

6 replies
Question marked as Best reply

Dec 3, 2021 9:00 AM in response to Sajik NZ

Unfortunately Automator has no conditional logic control - even its Loop action is less use than a chocolate fireguard.


I would suggest writing an AppleScript to check the folder and send the mail, then save the script as an AppleScript application, and finally set up a scheduled alert in Calendar to open the script app file, which will then run. It should get the result you need without involving Automator at all.


I shall have a think about a script...

Dec 3, 2021 10:39 AM in response to Sajik NZ

This AppleScript will check that the modification date of a specified folder, and send a message if it has not been modified in the last day:


set checkFolder to (path to documents folder from user domain as string) & "Manuals:"
tell application "System Events" to set folderChanged to modification date of folder checkFolder
set noUpdate to (current date) - folderChanged > 1 * days
if noUpdate then
	tell application "Mail"
		set theRecipient to "name@host.com" -- recipient's email address
		set recipientName to "name" -- recipient's name
		set theSubject to "File not created"
		set theBody to "The file you wanted has not been created"
		set alertMail to (make new outgoing message with properties {subject:theSubject, content:theBody})
		tell alertMail
			make new to recipient at end of to recipients with properties {address:theRecipient, name:recipientName}
		end tell
		send outgoing message 1
	end tell
end if



You'll need to set your own values for the variables theRecipient, recipientName, theSubject and theBody, and you'll need to set your own path for checkFolder. (For testing purposes I used a folder in my Documents folder called "Manuals" which was last modified more than a day ago.)


Copy and paste the script into a new Script Editor window, make your own modifications (post back if you need help defining the path), click the Hammer button in the toolbar to make sure it compiles, and you should get something like this:



Run it to check it works (you should get a true Result), then save as an Application, not stay-open, no splash screen, anywhere convenient.



Fire up Calendar, create a new daily recurring event, and set a Custom Alert for the event to open the application file.



This will run the script application every day. I think you'll have to be logged in to the Mac, though. TBH I'm not sure if the alert will run if you're not.


Cheers,


H






Dec 4, 2021 3:08 AM in response to Sajik NZ

Sajik NZ wrote:


Do you mean the underlined part, if left verbatim and not replaced with actual file path, would still work?

Yes, on any machine. Although you've inadvertently removed a couple of the spaces - it should be:


path to applications folder from user domain


This is the relevant entry in the Standard Additions library:



So if you're monitoring the main Applications folder you could define it as

path to Applications folder from system domain


Your path construction:


":Applications:Profile for Mac:" etc etc


(beginning with a colon) appears to work fine when scripting System Events, but it wouldn't work if you wanted to script the Finder and many other applications: they require a full path including the name of the system drive, which may vary from machine to machine.


The "path to" construction gets round this. On my machine, if I run the single-line script


path to applications folder from system domain


I get the result


alias "Macintosh HD:Applications:"


If the hard drive was called Big Mac, the same script would result in


alias "Big Mac:Applications:"


It lets you write complex inter-application scripts without needing to know the name of the machine the script will run on, or the name of the user. In your situation it may not be necessary.


(An alias in this context is a reference to a system object, not a Finder alias file/"shortcut".)


Dec 3, 2021 7:12 PM in response to HD

Thank you very much for these clear, concise, and prompt instructions. The script worked perfectly in a test environment which I have transferred to the live environment. Because the backup failure has not occurred in the last few days I can't confirm yet success but I expect it will work. The only difference between the test environment and live environment is the file path, both of which are shown below so you can confirm if it looks okay. The test environment did not use the Users folder


This worked in the test environment Mac. Note that the file path is starting at root ":" : -

set checkFolder to ":Applications:Profile for Mac:Profile Server Folder 3.4:Profile Data:Profile logs:Database Logs:"


This is the live folder file path. Note that it starts at "Users". I don't know if this is the right notation.

set checkFolder to "Users:aobi:Applications:Profile Server Folder KG:Profile Data:Profile logs:Database Logs:"


Thanks you for your interest in this. I wish I were as conversant in AppleScript as you, but I seldem have use for it – or so I am mistakenly think?

Dec 4, 2021 1:24 AM in response to Sajik NZ

Glad it worked 😃


Your live path will work, but only for you. If you need to make the script more portable, then:


set checkFolder to ((path to applications folder from user domain) as string) & "Profile Server Folder KG:Profile Data:Profile logs:Database Logs:"


would not have to be modified for every user. Your choice, though.


Dec 4, 2021 2:32 AM in response to HD

Thank you. I confirm that the live file-path did work. I tested it by purposely preventing the folder from being modified, so the date did not change.


In your script, I did not fully understand the notation whereby you wrote: -


set checkFolder to ((path toapplications folder from user domain) asstring) & "Profile Server Folder KG:Profile Data:Profile logs:Database Logs:"


Do you mean the underlined part, if left verbatim and not replaced with actual file path, would still work?


For this live file-path, the folder that need be monitored is in the home folder's Applications folder but other customers' equivalent folder that need be monitored is typically in the root-level Applications folder. Since, in that case, there is usually no variation to the file path from that point, I suppose a file-path on one Mac will be as good as any other and there be no benefit in writing any kind of 'portability' script, right?

Automator if/then conditional action possible?

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