Action
OpenFileFromWikiLink
UPDATES
about 3 years ago
Added sample iOS root path to help people know where to begin
NOTE: Only to be used with Custom Syntax for [[wiki-style]] links
**NOTE: This action will not appear in the Actions Bar or Actions List by default. This is because it’s meant to be triggered by a Syntax.
This action will try to open a file at a location specified in a [[wiki-style]] link that’s been defined in a Draft Syntax.
It will not work as expected as a regular double-click/keyboard shortcut action.
To allow this action to work, Drafts added 2 critical features:
1. [[wiki-style]]
links
2. Custom syntaxes – specifically the link definitions bit.
With these 2 features, you can create an easy to click link to open a file in the finder or the iOS Files app.
With this action
and a configured syntax
, I was able to get the following basic link:
[[f:test/image1.png]]
To open a finder window at this path:
/Users/<your_user_name>/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Library/Previews/Reference/test/image1.png
WARNING
This involves playing with custom syntaxes. Please review all of the documentation first before proceeding as it can get tricky. I’m assuming you’ve read that and are familiar with changing the syntax and testing in Developer Mode.
How do you do get this work?
Step 1: Configure your syntax
After installing this action, find the linkDefinitions
section of your preferred syntax. It should look something like this:
"linkDefinitions": [
{
"match": "(\\[\\[)(((d|u|s|w|google|wikipedia|bear|url):)?([^\\[]+?))(\\]\\])",
"templates": {
"": "drafts://open?title=[[value]]&allowCreate=true",
"google": "https://www.google.com/search?q=[[value]]",
"wikipedia": "https://en.wikipedia.org/wiki/[[value]]",
"u": "drafts://open?uuid=[[value]]",
"d": "drafts://open?title=[[value]]&allowCreate=true",
"bear": "bear://x-callback-url/open-note?title=[[value]]",
"w": "drafts://workspace?name=[[value]]",
"s": "drafts://quickSearch?query=[[value]]",
"url": "[[value_unencoded]]"
},
"enabled": true,
"captures": {
"value": "5",
"key": "4",
"prefix": "1",
"suffix": "6",
"link": "2"
},
"scopes": {
"value": "text.italic",
"key": "text.bold",
"prefix": "markup",
"suffix": "markup"
}
}
],
With a couple of tweaks, it should be changed to something like this:
"linkDefinitions": [
{
"match": "(\\[\\[)(((d|u|s|w|google|wikipedia|bear|url|f):)?([^\\[]+?))(\\]\\])",
"templates": {
"": "drafts://open?title=[[value]]&allowCreate=true",
"google": "https://www.google.com/search?q=[[value]]",
"wikipedia": "https://en.wikipedia.org/wiki/[[value]]",
"u": "drafts://open?uuid=[[value]]",
"d": "drafts://open?title=[[value]]&allowCreate=true",
"bear": "bear://x-callback-url/open-note?title=[[value]]",
"w": "drafts://workspace?name=[[value]]",
"s": "drafts://quickSearch?query=[[value]]",
"url": "[[value_unencoded]]",
"f": "drafts://runAction?action=OpenFileFromWikiLink&text=[[value]]"
},
"enabled": true,
"captures": {
"value": "5",
"key": "4",
"prefix": "1",
"suffix": "6",
"link": "2"
},
"scopes": {
"value": "text.italic",
"key": "text.bold",
"prefix": "markup",
"suffix": "markup"
}
}
],
The changes I’ve made appear in 2 places:
"match": "(\\[\\[)(((d|u|s|w|google|wikipedia|bear|url):)?([^\\[]+?))(\\]\\])"
Becomes
"match": "(\\[\\[)(((d|u|s|w|google|wikipedia|bear|url|f):)?([^\\[]+?))(\\]\\])
I’ve added an f
to the regex statement. The f
becomes a new key understandable in the syntax. It effectively will be replaced by whatever you define. You define that in the "templates"
section.
That’s what we change next:
"templates": {
"": "drafts://open?title=[[value]]&allowCreate=true",
"google": "https://www.google.com/search?q=[[value]]",
"wikipedia": "https://en.wikipedia.org/wiki/[[value]]",
"u": "drafts://open?uuid=[[value]]",
"d": "drafts://open?title=[[value]]&allowCreate=true",
"bear": "bear://x-callback-url/open-note?title=[[value]]",
"w": "drafts://workspace?name=[[value]]",
"s": "drafts://quickSearch?query=[[value]]",
"url": "[[value_unencoded]]"
},
Becomes
"templates": {
"": "drafts://open?title=[[value]]&allowCreate=true",
"google": "https://www.google.com/search?q=[[value]]",
"wikipedia": "https://en.wikipedia.org/wiki/[[value]]",
"u": "drafts://open?uuid=[[value]]",
"d": "drafts://open?title=[[value]]&allowCreate=true",
"bear": "bear://x-callback-url/open-note?title=[[value]]",
"w": "drafts://workspace?name=[[value]]",
"s": "drafts://quickSearch?query=[[value]]",
"url": "[[value_unencoded]]",
"f": "drafts://runAction?action=OpenFileFromWikiLink&text=[[value]]"
},
Notice the last line that’s been added: "f": "drafts://runAction?action=OpenFileFromWikiLink&text=[[value]]"
Once the syntax is installed and selected, and assuming your path exists, you should be able to click on this link in drafts and have a finder window open up.
Step 2: Configure the Action
The action depends on you having set 2 template tags. 1 for i*OS and 1 for macOS. The action will prepend the path configured in the template tags below, to the path specified in the [[f:wiki-link]] to form a complete absolute path. Since iOS and macOS have different directory structures, the action is smart enough to choose an appropriate path based on the OS being used.
macOS-root-path: This is the path that will be prepended to the link on macOS to create an absolute path
that can be used to open the file.
Example: I use
/Users/<your_user_name>/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Library/Previews/
to get to the Previews folder of Drafts in iCloud Drive
iOS-root-path: This is the path that will be prepended to the link on i*OS to create an absolute path
that can be used to open the file.
Example: I use
private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Drafts/Library/Previews/
to get to the Previews folder of Drafts in iCloud Drive in the Files app.
Steps
-
defineTemplateTag
name macOS-root-path
template -
defineTemplateTag
name iOS-root-path
template private/var/mobile/Library/Mobile%20Documents
-
script
let fileName = draft.content // Get file name from action let system = device.systemName; // Check device - will be macOS or iOS let rootPath = ""; if (system === "macOS"){ let rootPath = draft.getTemplateTag("macOS-root-path"); if (rootPath == null || rootPath.trim().length == 0) { context.fail("Template Tag 'macOS-root-path'. Typically this is defined in the previous step of this action"); } let fullPath = `${rootPath}${fileName}`; let method = "execute"; let script = `on execute(full_path) tell application "Finder" to open full_path as POSIX file end execute`; let runner = AppleScript.create(script); if (!runner.execute(method, [fullPath])) { alert(`Could not open file '${fullPath}' because of: \n` + runner.lastError); context.fail(`Unable to open file '${fullPath}'`); } } else { rootPath = draft.getTemplateTag("iOS-root-path"); if (rootPath == null || rootPath.trim().length == 0) { context.fail("Template Tag 'iOS-root-path'. Typically this is defined in the previous step of this action"); } url = "shareddocuments://" + rootPath + "/" + fileName app.openURL(url, false) }
Options
-
After Success Default Notification Error Log Level Error