2

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?

3

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.

3
  • Let us continue this discussion in chat. – inspirednz May 20 '15 at 2:27
  • Hi Froggard. Regarding the issue we faced with Pandoc not handling headings on the (MMD >) HTML > RTF conversion... Someone on the Pandoc forums has suggested we need to use XSLT for that. He's offered to put whip up some Perl scripting that will do it. Is that something we could then incorporate into are applet? – inspirednz May 26 '15 at 1:10
  • Most definitely – William T Froggard May 26 '15 at 4:27
1

I'm all for people learning how to script things, but for day to day use and flexibility, reimplementing parts of Hazel by NoodleSoft is less powerful and efficient for a general purpose solution.

Hazel icon small

Again, kudos to Froggard and Inspired Life for the excellent answer and the practical question.

5
  • The primary purpose of me writing scripts for people is to expand my scripting experience and knowledge. I don't personally care if there's an application that can do it, since the fun is in doing it myself, and having it work flawlessly. These days, there is very little I can't script, and since I need to keep my skills sharp for the work that I do, it pays to go the extra mile with these things. I do understand your apprehension though. I've seen some horribly crafted scripts on the Internet. – William T Froggard May 20 '15 at 15:29
  • @Froggard Yes - I'm answering this for all the people that don't yet have the itch. I want dozens of questions on the site asking how to script this particular set of requirements and asking for a specific tool. Python, AppleScript, or even how to set up Hazel rules to do XYZ. I should probably spend the time to solve this and edit that into my post here. Again, DIY is so important and why I voted up the question and the answer . – bmike May 20 '15 at 15:47
  • 1
    Certainly. There is often more than one correct answer, depending on an individual's requirements. – William T Froggard May 20 '15 at 16:04
  • Thanks bmike. I did come across then notion of using Hazel. I use to have Hazel on my machine some years ago... numerous OS upgrades ago. I figured I'd still need to get into having it run appropriate scripts, and also I couldn't immediately see a way I would have it deal with the bi-directional dual folder monitoring required in this situation. Also it's $30 and another app continuously running on my computer just to do this one thing. Seemed like over kill. Mind you, had Froggard not been so generous with his time, I might have resorted to hacking something together with Hazel in the mix. – inspirednz May 20 '15 at 22:10
  • I do appreciate your chiming in with this suggestion though. Someone else with a similar issue to solve may find going the Hazel route is the best solution in their case. – inspirednz May 20 '15 at 22:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .