Global objects in Actions AND Key Scripts


#1

I’m having some frustration with what should be a simple script. All I want to do is select all of the text of the line the cursor’s on (unless it’s selected already) and send the (cleaned up) text in that via URL to another app.

It seems to me the editor object (to select the line) is unavailable in Actions and the app object (to open the URL) is unavailable in Key Scripts. Am I wrong? Is there another way?

This is just one simple example, but having two vastly different scripting contexts is very confusing and limiting.

Can anyone help? Thanks in advance


#2

Try adding this as a script step in an action.

if (editor.getSelectedText() == "")
{
editor.setSelectedRange(editor.getSelectedLineRange()[0], editor.getSelectedLineRange()[1]-1);
}
let cb = CallbackURL.create();
cb.baseURL = "mailto:someone@example.com?subject=" + encodeURI(editor.getSelectedText());
cb.open();

I’ve just used opening an e-mail and setting the subject line as the selection as an app URL example.

Hopefully this matches the sort of thing you were asking about.

It shouldn’t make any difference how it is triggered; act menu, keyboard shortcut or extra keyboard row button.

Hope that helps.


#3

With this code, I get:

ReferenceError: Can’t find variable: editor

Note: I am trying this in Drafts 4, I want to see if I can migrate from Editorial to Drafts 5 before I invest in it. Was this a bug, or an improvement in v5 that wasn’t mentioned anywhere?


#4

That was Drafts 5 script. It wouldn’t work with that script on Drafts 4 as it is a rather substantial update to Drafts 5 and you can find many resources online that will guide you through the differences. I’d recommend checking out @agiletortoise’s own Drafts web site getdrafts.com and, whlst it is some months out of date and many things have in fact moved on, @nahumck’s extensive review on Macstories.

I still have Editorial and style use it for bits and pieces, but Drafts 5 is under active development and is incredibly flexible. It is very much my daily driver for working with text outside of my work.

In my opinion, comparing Editorial to Drafts 4 is a false experience in terms of evaluating “Drafts”. Drafts 5 is the here and now. It is free to download, and you can get Pro feature access for just a month for a couple of pounds. My recommendation would be to grab the app, take a month of Pro and try it out that way. Afterwards you would still have Drafts 5 (which can coexist with Drafts 4 by the way) and could still take advantage of a great number of the features without being on the Pro subscription … but with it, you can bend Drafts to your will in so many ways :smiling_imp:

FYI check out the pro features at:

https://getdrafts.com/draftspro


#5

Yeah I’m familiar with 5, and likely to migrate, but I wanted to test my concept with v4. Most of what I found seemed to indicate that was possible.

Surprising this big of a scripting update was never mentioned anywhere?

Edit: Sorry, I should have thanked you for your time and your response. I will take a look at 5 when I get some time.


#6

You can only operate on the editor (text selections, etc.) with key scripts in Drafts 4, which do not have access to non-editor functions. Drafts 5 merged keys into actions so they have all the same capabilities and one consistent scripting interface.

Drafts 4 has only very basic scripting capabilities in either case. Opening URLs from script is not supported, for example.