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:
- How can I automatically monitor a folder for changes to its content, and run a script if files change in that folder?
- 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:
- Create two folders on the Mac. One is for RTF files, the other is for MMD files.
- Set up the Mac app to sync its data out and in from the RTF folder
- Set the the Mac to Android sync system (Dropbox, Bittorrent Sync, etc.) to sync content of MMD folder with a folder on Android device.
- 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?