There is an Action Step to define a Template Tag (name and a value).
In a script draft.setTemplateTag
defines a new Template Tag (name and value).
Would it be possible that setTemplateTag
can not only be used to define a new Template Tag but also to change the value of an existing Template Tag which has been created in an Action Step? Meaning the value of the Template Tag in the Action Step would get changed. (Since this would affect the action maybe action.setTemplateTag
would be more appropriate than draft.…
.)
It would be working similarly to app.getClipboard
and app.setClipboard
.
My use case: I have a list of static buttons and I would like to have an additional dynamic one called “Last used”. By changing the value in the Action Step “Last Used” would be valid no matter when this action was used again.
I’m not sure I understand the use case.
Is there a reason you are using the Define Template Tag action step at all and not dynamically determining the value needed in your script? If your script calls setTemplateTag
for the same tag name in a script that comes after a Define Template Tag step using the same tag name, it would override the value – thus replacing it, is that all you need?
My specific use case: The action contains of a list of Bookends references like {Kaufman, 2021, #297372}. The list is stored (and maintained) in a Define Template Tag. It holds a number of these references, divided by pipes.
When I launch the action the script creates buttons for each reference to pick from (and it does more, which is of no relevance here though). I would like the action not just to process the chosen reference (like inserting it into the present draft) but also to set [[LastUsed]] to the chosen reference. But [[LastUsed]] would not be a Template Tag defined inside of the script but defined in an Action Step. And the script would change the value in the Action Step.
Generally speaking, what I am looking for is a way for a script to save a value for the next time the action it belongs to is launched. My first idea was to create a new “Last Used” draft, maybe located in the Archive. But it would be more elegant—because drafts would be just drafts and not helper files for actions—and less confusing for potential other users of the action.
You cannot alter the content of an action from script.
You can, however, store values elsewhere for use. As you mentioned, storing in an archived draft is a good option, but you can also use files via the FileManager
object.
A local file will be invisible and not synced across devices, and would not be visible to the user, so would work well for this case. I would suggest using JSON for this, and the FileManager has convenient functions to read-write Javascript objects as JSON…something like:
// create an object with any settings you want to store for the action
let options = {
"lastUsed": "MY-LAST-USED-VALUE"
}
// setup the file name to use, could be anything
const fileName = "/action-settings.json"
// create a FileManager to use
let fm = FileManager.createLocal()
// write the values to a file to save
fm.writeJSON(fileName, object)
// read the values for use in the action
let values = fm.readJSON(fileName)
if (values) { // will be undefined if file does not yet exist
var lastUsed = values["lastUsed"]
}
else {
var lastUsed = "SENSIBLE DEFAULT"
}
Thank you for the suggestion. I will have to look into that as I have no experience with JSON whatsoever.
But in my opinion it would be great if that was possible one day. Not just for my specific use case.