May 30, 2020 at 11:24 AM by Dr. Drang
I didn’t renew my Dropbox Plus account when it expired a month or two ago. For several months I’d had both it and a 2 TB iCloud account active at the same time and had been moving my files and my way of working from one to the other. This was mainly due to my shift over the past four years from being Mac-only to Mac-mainly to iPad-mainly. While the Finder is equally adept at managing files in either cloud, Files is much better with files on iCloud Drive.
My concern was with Drafts. There are programmatic ways to save a draft to a specified file in Dropbox—I use a “filelines” system similar to Vim’s modelines in which the file path is embedded in the text of the draft—but sandboxing prevents that working with file paths in iCloud Drive. Here’s a Drafts forum thread in which Greg Pierce (agiletortoise) explains the problem.
As it turned out, my concern was overblown. Shortly after writing this post in which I talk about writing almost everything in Drafts, I started write almost everything that needs to be saved as a file in Textastic. Textastic is file-based and can read and write anywhere in iCloud Drive (or anyplace Files can access) without continual user approval.
Still, I did occasionally miss being able to use filelines to save drafts directly to iCloud Drive. It wasn’t until this past week that I realized there was a way around it by using Scriptable.
Scriptable has a feature called “bookmarks,” which are ways to access files (and folders) outside of the Scriptable sandbox. You create them by choosingin Settings.
Then tap the + button in the upper right corner and choosefrom the popup menu.
You’re now presented with the usual Files-style list of file providers. Choose iCloud Drive.
Finally, you’re asked to choose a name for this bookmark. I chose “iCloud.”
You can now use this bookmark and Scriptable’s FileManager library to access any folder or file in iCloud Drive. The great thing about this is that you’re not limited to just the top level of iCloud Drive—any file or folder at any level in the hierarchy below iCloud Drive is accessible.
With this bookmark defined, I can create a system that looks for an iCloud fileline, a line in a draft that contains
and saves the contents of the draft to a file in the given location. Typically, this line will be in a comment, so it will be
for a Python file or
for a LaTeX file.
The system consists of two parts:
- A Drafts action that gets the fileline, extracts the path, and sends the path and the full draft contents to a shorcut.
- A shortcut that takes the information from the previous step and writes the contents of the draft to the given file.
which extracts the path from the iCloud fileline. If there is no fileline, it stops the action with an alert. If there is a fileline, it creates a JSON dictionary with two entries: the path from the fileline and the full text of the draft. The dictionary is saved to a template tag in Line 17 so it can be passed to the next step of the action.
The second and final step in the Drafts action is this Run Shortcut step,
which passes the dictionary created in the first step to a shortcut entitled “Save to iCloud.”
Here’s the “Save to iCloud” shortcut:
|0||Even though we won’t be using it from the Share Sheet, we define it this way so it can accept the text passed to it from Drafts.|
d. The rest of the script gets the iCloud bookmark, appends the filepath to it, and writes the text of the draft to that file.
If you don’t feel like writing it yourself, you can download the shortcut.
My understanding (based on a post from its developer) is that Toolbox Pro is beta testing a similiar bookmarking system. If that gets released, it may simplify the shortcut.
There is a hitch in this system that’s unlikely to affect me but might affect you. Saving a draft from my iPad to iCloud drive works perfectly, and every time I edit the draft and resave it, the file is overwritten with the latest version of the draft, just as you would expect. But if I edit that draft on my iPhone and save it, a new file gets created in iCloud Drive. If the original file saved from the iPad was
the file saved from the iPhone will be
even though the fileline is still
I assume this has something to do with Scriptable bookmark on the iPhone being somehow different from the bookmark on the iPad, despite them both pointing to the same iCloud Drive location.
As I say, I don’t think this will affect me, as the writing I do on my phone tends to be ephemeral stuff. But it is one of those things that shows how iCloud Drive isn’t quite on the level as Dropbox (or Box or Google Drive or…) when it comes to working with Drafts.