Wanting help creating an applescript that is run when the contents of a folder changes

applescriptautomatorfolder-action

I would greatly appreciate some assistance with the following.

My scenario:

I have a Mac app that generates RTF files.
I have an Android app that deals with MultiMarkdown/Markdown files.

The Mac app works with projects, and these have any number of files in them. It can automatically synchronise the files in a given project to a folder. Essentially, it will place into the folder a bunch of files whenever it syncs (on opening and closing, and manually). Any changes to that folder are automatically synced to my Android device.

However, I need them to be converted to .mmd files before that happens, and converted back to RTF when the files come in changed (or new) from the Android device.

So here is what I have in mind. What I would like is to set things up in such a way that when files are synchronised from the Mac app to their target folder, those files are automatically converted from RTF to MMD. I already have the scripts that will do the the actual conversion. AND when the MMD files change, they will be automatically converted to RTF files. I also have a script for this.

The questions I have are these:

  1. How can I automatically monitor a folder for changes to its content, and run a script if files change in that folder?
  2. How could I make this work two-ways, without throwing everything into an endless loop? At first I thought I could do the following:

Two-way conversion:

  1. Create two folders on the Mac. One is for RTF files, the other is for MMD files.
  2. Set up the Mac app to sync its data out and in from the RTF folder
  3. Set the the Mac to Android sync system (Dropbox, Bittorrent Sync, etc.) to sync content of MMD folder with a folder on Android device.
  4. When content of RTF folders changes, convert the files to MMD and put output into the MMD folder. AND vice versa.

But, obviously, this will create an endless loop, because each action will trigger the reverse action, endlessly.

One possible way around this endless loop might be this:

  • RTF folder has script R assigned to it. When content changes, script R executes.
  • MMD folder has script M assigned to it. When content changes, script M executes.
  • When a change is detected in RTF folder, the first thing script R does is disable script M from triggering. The last thing script R does before exiting is enable script M again. That way M won't 'notice' that the folder contents were changing whilst R was updating its content with freshly converted files.
  • The same would apply in reverse.

But maybe there's a better way?

I am keen to learn more about scripting, and in the process resolve this problem.

Are there any people with feasible ideas on how to achieve what I am hoping to work out here?

Best Answer

I would create one script that monitors both folders. My language of choice for this is Python. AppleScript is not terribly well-suited to this kind of operation. The basic idea is to listen for FSEvents on the two folders in question, and when one occurs, quickly unschedule the event stream for the other folder, do some conversion and sync operation, then reschedule the previously unscheduled event stream.

The completed script, downloaded here, with modified versions of those scripts you showed me. The MacFSEvents installation is now automatic: https://www.dropbox.com/s/oylvewk90ezwb3s/File%20Sync.app.zip?dl=1

Here are the steps to use it:

Extract the applet, right click on it and select "Show Package Contents", open Contents, and Resources, then edit the plist inside with your RTF and MMD folder pairings. You will be able to specify as many pairs as you need to keep synchronized with each other, just stick to the same format as what I used in the plist: One array per pair, each containing two items, the first item being the path to the RTF folder, the second being the path to the MMD folder. You'll see this when you open the plist.

This should work without any issues. The folders will now be watched for any sort of modification, and will have the files converted and transferred to the other folder.