Feature request: More options for the javascript runner

The ability to run arbitrary javascript code via the new javascript runner is extremely helpful, especially for things that don't really warrant for a plugin. Would it be possible to add a few more features to the runner that would make it even more useful:

For example:
1. A way to save and load scripts: I guess a common scenario involves having some scripts that are used on a regular basis, like for data transformation, merging, and similar things. It would be great if theses scripts could be saved directly in Zotero.

If 1. is possible:

2. A way to upon a script in an external editor.

3. Associate execution mode with a given file (run as async function)

  • Why isn't the code always ran as async? If there's no async code in an async function, it will just return an already-resolved promise, right?
  • It would be great if theses scripts could be saved directly in Zotero.
    To clarify, do you mean that the scripts would be saved internally, or that you'd be able to load/save files stored elsewhere from this window?

    The latter is what you see in Firefox's Scratchpad, but that seems fairly pointless to me when you can just copy and paste.

    The ability to store scripts in Zotero is interesting, though. One model here would be the old keyconfig extension for Firefox/Thunderbird, which let you assign custom code snippets to keyboard shortcuts. (I actually use that in Thunderbird many times a day to open the URLs for Zotero Forums posts, and it's incredibly useful.) The main downside would be the proliferation of code snippets that would likely eventually become obsolete and either break (at best) or cause damage (at worst, though unlikely), whereas extension code generally has a maintainer. But that might be a reasonable trade-off for most of the people who would actually take advantage of this sort of thing.
  • Why isn't the code always ran as async? If there's no async code in an async function, it will just return an already-resolved promise, right?
    We could consider this, but the displayed value is different. In non-async mode, the value of the last expression is shown, so var foo = 1; foo; shows ===>6<=== (number). In async mode, it's the resolved value of the promise, so you have to use var foo = 6; return foo;, which is a bit different from what you might expect from a REPL, Scratchpad, etc. On the other hand, almost all Zotero code where you would actually want to see a return value — as opposed to just triggering some action — probably does need to be async, so maintaining the standard behavior may not be worth it.
  • To clarify, do you mean that the scripts would be saved internally, or that you'd be able to load/save files stored elsewhere from this window?
    I was actually thinking of the first idea: store scripts internally. Being able to assign custom code snippets to keyboard shortcuts sound like a great idea.

    (@dstillman : Sorry for the late reply; I have gotten a email notification for emilianos first comment, but not for your comments. Is that how it should be or are there problems with the notification system?)
  • (You only get a notification for the first comment in a thread since the last time you viewed it, or if someone mentions you. Just click through on the email notification to make sure you're notified of a follow-up message.)
  • Ok, I see. So it is not really possible to follow the discussion via mail only. Good to know.
  • If saving scripts within Zotero is implemented as discussed above, how about including a few well-commented scripts for typical operations right there, as working examples? The users could just create a test library and have a play then.

    Separately, a tutorial would be great too, especially on how not to mess things up.

    Thanks for considering.
  • (I've moved the external scripts discussion to a separate thread.)
  • @dstillman, you wrote "One model here would be the old keyconfig extension for Firefox/Thunderbird, which let you assign custom code snippets to keyboard shortcuts. (I actually use that in Thunderbird many times a day to open the URLs for Zotero Forums posts, and it's incredibly useful.) "

    I tried to install keyconfig in Zotero 5.0.69 using the link on the keyboard shortcuts documentation page. https://www.zotero.org/support/kb/keyboard_shortcut

    It didn't work. Is there a way to install keyconfig on an updated Zotero?
  • edited July 24, 2019
    I don't think you can just install keyconfig in Zotero, and I don't think this is what @dstillman has meant. Instead, I understand his comment as a suggestion for a new feature that would work in a similar way.
  • Yes, that's right.

    I think it was possible to run some version of keyconfig in Zotero at one point, but that may have been only in Zotero for Firefox, or a much older version of Zotero Standalone and/or keyconfig.

    For now, if you use the Run JS window often, I'd just recommend keeping a collection of snippets in a text file.

    (As for keyconfig-like functionality, I don't know when we'd get to it ourselves, but if someone is interested in working on it, we can certainly discuss.)
  • I would love keyconfig-like functionality in Zotero. My current attempts to use it with keyboard alone involve AutoHotKey, but that doesn't work for a number of functions for various reasons.

    I wish I could program a plug-in for this. Since I can't, let me know if there is any way I could help someone's efforts to create this in any way.
  • It would be great if the proposed feature could be integrated with the Zutilo add-on.

    Zutilo includes an adapted version of the Keyconfig extension for setting up its keyboard shortcuts, see here. With Zutilo, it should then be possible to assign keyboard shortcuts for executing scripts.

    I opened an issue on Zutilo's GitHub site for related discussions:
    https://github.com/wshanks/Zutilo/issues/171
  • edited December 14, 2020
    @qqbb: As I say above, the plan here would be to make keyboard shortcuts assignable for the saved scripts. Zutilo doesn't need to do anything if we add this to Zotero itself.

    We'll look into implementing this soon.
  • This all sounds very promising and I am looking forward to any implementation.

    I see two use case here: (1) Someone is starting with a new Zotero script and wants to save it (locally) at some point in order to continue later or to execute it from time to time. (2) Someone has shared a Zotero script which another one wants to open and use as well, e.g. I have some Zotero scripts shared online e.g. for a Wikidata lookup.

    In the second case one could save this script in a special folder within Zotero data directory and then restart Zotero. This would require that all scripts in this special folder are afterwards available within Zotero's Run JS functionality. Would this work? I am not sure if you @dstillman were refering to something else with "external files" in the other thread.
  • edited December 14, 2020
    By external files I meant the sort of open/save-as functionality that the old Firefox Scratchpad had — opening and saving files elsewhere on your computer. I was saying I don't see any point to that. This would just store files in a directory in the data directory. You could still use an external editor to edit them (though we're planning to make the ACE editor we use in the translator editor available here as well).
    one could save this script in a special folder within Zotero data directory and then restart Zotero
    We can do it without requiring a restart (here and in Scaffold), but yes.
  • I was pleasantly surprised by the syntax highlighting in the new "Run JavaScript" editor. Thanks!
  • edited January 19, 2024
    Hello! Did the improvements discussed in this thread ever get implemented? In particular I am looking for a way to run a script on some automatic schedule or on start-up, but I would definitely take a way to save and load script. Thanks!
  • Also interested in this!
Sign In or Register to comment.