Marked scripts: nvALT, Evernote, MarsEdit, Scrivener
Thus far, Marked has had a great reception and has, overall, worked superbly with a wide range of text editors. I frequently get requests for integration with more complex editors, but have been unable to fulfill them because Marked requires an actual text file to watch. To try and fix this situation, I’ve been scripting “watchers” for various applications which embed their files inside of bundles or otherwise obscure the actual content files from Spotlight.
I’ve created scripts for Notational Velocity/nvALT, MarsEdit, Scrivener and Evernote. The scripts will be part of the Marked Bonus pack, but are also available as a separate download here (see the end of the post). I would eventually like to incorporate the functionality directly into Marked, but it will take some time to add the necessary configuration options and make it an elegant fit.
The scripts work by using timestamps and polling with Ruby to detect changes within certain files and directories (varies from script to script)1. The Evernote, nvALT and MarsEdit scripts can be run without arguments (or as LaunchAgents). The Scrivener script requires the path to the .scriv
file for your project as the only argument.
The preview contents are always saved to ‘Marked Preview.md’ in your home folder. When you first start, you may need to create this file manually with a text editor and save it to your home folder. Once the scripts have run once, the file will exist and you can open it in Marked any time you’re working in a “watched” application.
All of this was inspired by this gist by myfreeweb. His method uses fsevents and Python, which is definitely more elegant in many cases. I switched it over to use Ruby and timestamps because I’m pretty lame with Python right now. If you want to take the basic ideas and convert them to use fsevents, please share! I’ll be happy to host and credit (or just link, if you prefer) any submissions.
I’m certainly taking requests for any other editors you’d like to have “watched,” so drop your vote in the comments.
Notes:
For all of these scripts, the easiest way to use them is to put them in a convenient folder (I use ~/scripts
) and run chmod a+x path/to/script.rb
to make them executable. With the exception of the Scrivener script (scrivwatch.rb
), you can then just type the path and script name (e.g. ~/scripts/everwatch.rb
) and hit Enter. They will run and watch for changes in their specific application until you cancel the command by typing Control-c
.
The scripts will create a file in your home directory (modifiable in the script) called ‘Marked Preview.md’. Open that file in Marked; Marked will watch that file for changes that the scripts make.
You can create LaunchAgents for any of these (except, again, Scrivener) and run them automatically in the background if you know what you’re doing. If you don’t, you can still use an app like Lingon to do it.
Evernote (everwatch.rb)
To keep the ‘Marked Preview.md’ file synced with whatever note you’re currently editing in Evernote, start the script by running ~/path/to/everwatch.rb
in Terminal. The script watches for changes to timestamps on any directory in Evernote’s data folder. This shouldn’t need to be adjusted. To update Marked, you’ll need to have “~/Marked Preview.md” open and then hit “Command-S” in your Evernote note. The autosave on Evernote will work, but it takes longer.
The HTML of the note is captured via AppleScript and run through textutil
to remove the HTML formatting. This means that embedded images won’t come through, but those probably would have broken anyway. The script is specifically expecting you to write your notes in Markdown. If you’re not, I’m not sure why you’d want a Marked preview anyway…
Even with “Command-S” there’s still a 4-5 second delay on the update, as it takes a bit for Evernote to write out to the file, the script to poll through and notice the change, the content to be pulled via AppleScript and written to the preview file and then for Marked to pick up on the change there. Considering all of that, 4-5 seconds isn’t too bad. If someone can think of a faster way, I’m certainly open to it.
Scrivener (scrivwatch.rb)
The Scrivener script watches the RTF files that Scrivener keeps within the project as you write. If you set Scrivener’s preferences to auto-save 1 second after you stop typing, Marked will stay pretty snappy on the updates without any further intervention.
To launch (assuming you made the script executable as detailed at the beginning of this section), open Terminal and type path/to/scrivwatch.rb /path/to/YourDocument.scriv
. Example: ~/scripts/scrivwatch ~/Documents/Thesis.scriv
. The script will take it from there and run until you interrupt with Control-c
.
The files Scrivener stores are Rich Text Format, so Marked can’t view their contents directly. The script runs the most recently-edited file through textutil
to convert from RTF to text. That gets passed to the ‘Marked Preview.md’ file for Marked to display. Only the portion of your Scrivener document that you’re currently editing will be displayed. I played with concatenating the whole document for preview, but it’s not easy to determine the order to display all of the individual files in. Again, if anyone has further ideas, please share!
MarsEdit (marswatch.rb)
Thanks to Daniel Jalkut for an assist with this one. It watches the MarsEdit autosave folder for any changes, and then uses AppleScript to get the full contents of the editor (post and continued) when one is detected. Because the autosave can be a bit slow on the draw, it continues updating every second for 10 seconds, whether there’s a change or not. If no more changes are detected within 10 seconds, it chills out in the background until the next one is detected.
Run marswatch.rb
to start polling for changes, open ‘Marked Preview.md` from your home folder and Marked should start updating the preview automatically as you make changes in a MarsEdit post.
Notational Velocity/nvALT (nvwatch.rb)
If you’re using Notational Velocity (or my fork, nvALT), you can tell it to save your notes as text files on your drive. This script will watch these text files for updates, then display the contents of the most recently-edited note. It’s a workable solution, at least until I get better integration worked into nvALT directly.
You need to configure the script to point to your chosen folder for note storage, and if you’re using any unique extension, you’ll need to add to or modify the list in the script. It should be pretty obvious what needs to be set if you look at the top of the script.
Download
I’m sure I forgot some details in this writeup, so if you have any questions, leave a comment and I’ll add information as needed.
The most recent versions of the watcher scripts are now included the the Marked Bonus Pack.
You can find out more about Marked at the Marked website.
-
Related tip: you can use
sudo opensnoop -p PID
(where PID is the PID of the application you’re working with) to view all files that the application touches. It’s a good way to figure out what to watch when working on scripts like these. ↩