Drip-feed Files into Folder Using Hazel & KM

Hi everyone. I’m trying to setup Keyboard Maestro with Hazel to turn my mac into a “robot” that runs KM macros using info from .rtf files… I’ve asked in the Hazel forum but haven’t found a solution yet. Does anybody have any ideas how to get this working?

I’m trying to create a mac “robot” that runs macros when instructions are sent to it via email. The instructions are sent as .rtf file attachments. I want to create a drip-feed system for processing .rtf files that are emailed to my mac. I’ve setup my email to automatically download the attachments into my download folder. I’ve also set hazel to monitor the download folder for the incoming .rtf file (I have all of this working.) I have Keyboard Maestro setup to read the lines of the .rtf files into variables and launch the macro. I just don’t know the best way to process the .rft files with keyboard maestro in the order they were downloaded… and in a way that doesnt kick off another macro until the currently running macro is finished. I hope this makes sense.

Lets say for example that I send a .rtf file to the “robot” to run a specific keyboard maestro macro… and then I send another .rtf file before the current macro is finished running… how do I setup Keyboard Maestro to process the .rtf files in the order they were received? So that the new file doesn’t kick off another macro until the current macro has finished running? These macros are using mouse clicks, etc… so only one macro can run at any time… Or if you have any other ideas of how this could be done please let me know… I really appreciate any other suggestions you may have how to get this working. Thank you so much!

Aaron

Other than the general idea of moving files from mail to a folder to processing by a KM macro, the processing that your macro would do is not clear. Could you post a non-private example or explanation?

Anyway -- why not have Mail or Hazel trigger your macro for you (via AppleScript for example)? Your chain of events might be a bit fragile -- Mail has to notice something and trigger an action -- then Hazel has to notice something and trigger an action -- then KM has to notice something and trigger an action. Sequences like this can fall apart. So if you can have Mail or Hazel trigger the macro your solution might be more robust.

Thank you for your suggestions! I’ve eliminated several steps of the sequence per your suggestions. Heres what I have setup now.

I have a form setup on a webpage. The form has several boxes for adding info and a submit button (like any typical email signup form.) The form has input boxes for the variables that the macro will need to run. For example Name, Address, Email, etc. When the form is submitted, it sends the info to zapier, which creates a new .rtf file with the info and saves to a folder in my google drive account. The google drive folder is synced with my mac inside my documents folder.

I have hazel setup to monitor that google drive folder. When a new file is added, hazel renames the file to runme.rtf and triggers the macro using applescript. The macro is setup to read the content of the readme.rtf into variables using filepath, and start a series of macros using the variables.

Basically the macros go to several websites and create an account on each website using the info. This is used to signup clients and employees to different web services that my company uses. The macros also setup settings inside several marketing software tools that we use. The macros are very long and complex and take about 25 minutes to run through them all completely.

Because the macros are so complex, I have a final macro that runs at the end which deletes all variables, clears clipboards and clears the browser cache… then deletes the runme.rtf file.

How do I make Keyboard Maestro wait until the current series of macros has finished running before starting off another series of macros with the next .rtf file?

If a second or third .rtf file is added to the google drive folder before the macro has finished running, hazel will rename them, because the runme.rtf file would still exists in the destination location… so now they would be called runme-1 runme-2 etc… and they wouldn’t run because the filepath would be wrong.

I tried setting up using 2 folders… one called queue and another called processing…
All files come into queue… so if more came in they would just be held there… and i wanted to have hazel only move the next file once processing folder had 0 files… but hazel cant run conditions on the destination folder, so that wont work.

I also tried to setup Keyboard Maestro to monitor the processing folder and only run once the folder has 0 files… problem with this is that it doesn’t remember to run them later… so a new file comes in while the macros are running it just ignores it… I need to find a way to have it run the .rtf files one by one, in the order they were received, from the least recent file to the most recent. So that its like a drip-feed system for the .rtf files.

I hope this makes sense. I really appreciate you taking the time to help me with this. Any other suggestions would be much appreciated!

Aaron

Notice: I have NOT thoroughly read your entire post, so I may have missed something. Here I am addressing just this one issue.

One approach is to set a Semaphore Lock action (KM Wiki) at the start of the process to indicate the macro status. For example:

At the start of the macro triggered by Alfred:

This will queue, but keep from running, any further triggers of the macro until the current instance has completed, or you unlock the Semaphore.

Since the main Macro (the one triggered by Alfred) fires several other macros, which will run synchronously, the main macro will not complete until all of the submacros have completed. At which point the semaphore will be cleared (unlocked), and the next instance in queue can then execute.

Does this make sense?

Thank you so much! Yes this helps. Im almost there… now I just need a way to choose a file without knowing the full path… is there a way to use Move or Rename File without knowing the name? I tried using “for each item in collection” but it fails because it needs a full path to the source file to move… how do i get it to move “each item in the collection” without knowing their exact name and filepath? if more than one file gets added to the folder before the macro can process them then they will get renamed to avoid duplicates… so if I add a file called runme.rtf and then I add another file called runme.rtf before the first one has been processed and deleted, it will be renamed to runme-1.rtf etc… is it possible to move using just the first 5 letters… for example any files that begin with runme? so that all files get moved without having to know the exact name? Thanks again for any suggestions…

heres my macro in text

For Each Item in the Collection Execute Actions
The direct contents of folder ‘/Users/Aaron/Documents/Automation/Queue’
Execute the Following Actions:
Pause Until All Conditions Met
Nothing exists at path ‘/Users/Aaron/Documents/Automation/Processing/runme.rtf’
Move or Rename a File
To: /Users/Aaron/Documents/Automation/Processing/runme.rtf
Notify on failure.

Wow, I really need to get Keyboard Maestro 8 out :wink:

Put a Semaphore Lock at the start of the macro and the second instance will wait until the first instance is finished before continuing.

However you will still have a problem with the Hazel side, since you will need to make Hazel wait to rename the file.

Can you trigger the macro with Hazel and use Keyboard Maestro to rename the file, passing the file path in as the trigger value? That way you can do something like this:

Semaphore Lock
Move File %TriggerValue% to runme.rtf
… process file
Delete runme.rtf

You don't need to do this with my solution, assuming the uploaded file as a unique name, each one that comes in will trigger a macro with the name of the file, and then Keyboard Maestro will wait until processing all previous ones is complete and then process the next one (in order, because Semaphore Locks release in the order they were asked for).

THANK YOU! Your solution worked perfectly… Semaphore Locks were the key.

That's great! I'm glad your problem is solved.

Would you mind posting your final solution so others can benefit?

See

I wrote a similar macro to read commands from a text file stored on Dropbox and process those commands. I hadn't needed to do semaphore locking because I never sent commands that quickly. Thought I'd post it here in case any one is interested in my solution.

I have multiple computers so the name of the control text file has to start with the name of the computer. I move the control file immediately after reading it into the clipboard, not sure if that's fast enough to avoid needing semaphores.

For security reasons I only allow specific commands be executed (i.e. can't upload a bash shell script and run that). Also because most of my commands are one shots only the first line of the text file is actually processed.

This script is designed to be controlled via a Workflow I've written on iOS (BTW Peter - Workflow integration in next Keyboard Maestro?) and mostly what I want to do is launch applications that don't seem to be running - for example if I want to remote my Mac but TeamViewer isn't running, it can launch TeamViewer for me. Or if I'm having problems with TeamViewer it can stop it, then restart it.

Basically the way it works is there a master macro, it triggers on the addition of a file to a drop box folder. It loads the contents into the clipboard and then based on the contents runs another macro. I'm not going to upload every macro that gets called but a couple to show how they work.

I have a macro that gathers various pieces of system information, including the public IP address of the computer, and the status of various applications. This information is then pushed to my iPhone via the Pushover notification service (I have a plug-in for this, it's on GitHub.)

The other macro I'm posting is one for controlling an application. Applications can be stopped, started or restarted.

Keyboard Maestro “Remote Commands” Macro

Remote Commands.kmmacros (6.8 KB)

Keyboard Maestro “Send Computer Status” Macro

Send Computer Status.kmmacros (2.8 KB)

Keyboard Maestro “GetSystemInfo” Macro

GetSystemInfo.kmmacros (11 KB)

Keyboard Maestro “TeamViewer Controller” Macro

TeamViewer Controller.kmmacros (12 KB)