Ability to copy string to clipboard using api

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
unhott
Inserter
Inserter
Posts: 29
Joined: Tue Jan 09, 2018 3:01 am
Contact:

Ability to copy string to clipboard using api

Post by unhott »

This exists in the game under the blueprint tool—> export string —> copy

But I can’t find anything like it on the api documentation.

I’d like to be able to call something like player.copy_to_clipboard(string)

/c player.copy_to_clipboard(game.help())

User avatar
Therenas
Factorio Staff
Factorio Staff
Posts: 232
Joined: Tue Dec 11, 2018 2:10 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by Therenas »

Being able to do this without explicit user input is kinda risky, as it can lead to data loss. You can overwrite anything the user has in his clipboard, which could be something important. Sure it won't be the case very often, but the potential is there.

SyncViews
Filter Inserter
Filter Inserter
Posts: 295
Joined: Thu Apr 21, 2016 3:17 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by SyncViews »

Lua scripts can do more annoying things, like crash the game. At most maybe adopt the same rules that JavaScript has, and don't allow reading clipboard data from the OS clipboard. If some random webpage I click in a Google search can do it, letting a Factorio mod do it should be OK and is way more secure than a lot of other modding platforms.

Which from recollection is something like:
  • Must be the active tab (focused, same browser tab, so the user is already interacting with it).
  • Can only be content within the page (so no grabbing stuff from elsewhere in the browser, other tabs, etc.).
  • Has to be within the context of certain other events, like a mouse click or keyboard input.
Mainly allowing those "copy to clipboard" type buttons.

adamius
Long Handed Inserter
Long Handed Inserter
Posts: 73
Joined: Mon Mar 19, 2018 9:18 am
Contact:

Re: Ability to copy string to clipboard using api

Post by adamius »

If a flag was set just before an event handler is called then it would be possible for copy_to_clipboard to detect if it is being called from a GUI event handler. Most reasonable uses for copy_to_clipboard involve an GUI action whose end result is the export of text, eg create a list of train stops as a CSV or some other report or data structure (eg blueprints already do this).

Have a look at the TodoList mod. It really needs a export function that can actually do copy-to-clipboard. This is a reasonable use-case.

Irritating places to allow clipboard: on_tick, on_n_tick, on_init, on_load etc.

User avatar
_CodeGreen
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Sat Mar 05, 2022 11:30 am
Contact:

Re: Ability to copy string to clipboard using api

Post by _CodeGreen »

I've found myself wanting this feature as well. I want to be able to make my own custom exchange string, like blueprints and such, however the closest we can get is only selecting the text for the player to manually copy.

Perhaps a reasonable compromise would be an api to open the vanilla copy text gui, if it's still the case that it's too risky to allow direct clipboard manipulation.
My Mods | If you can't make it perfect, make it adjustable

User avatar
SupplyDepoo
Filter Inserter
Filter Inserter
Posts: 286
Joined: Sat Oct 29, 2016 8:42 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by SupplyDepoo »

+1

Console commands should also be able to copy to the clipboard (easier than using log).

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by FuryoftheStars »

While I can certainly see the utility of this, I'm not sure if I'd want a mod to be able to "just do it". Maybe if there's was a specific, new UI button introduced who's caption could not be changed and who's use was only for this? In that way, the player would know and have full control over if and when it sends something to their clipboard. But being able to arbitrarily send the info to their clipboard without their knowledge or consent? Absolutely not.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
SupplyDepoo
Filter Inserter
Filter Inserter
Posts: 286
Joined: Sat Oct 29, 2016 8:42 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by SupplyDepoo »

A special button would work for most use cases, but wouldn't always be ideal. For example, if I wanted to make an "Export Area" tool, it should simply let me draw a rectangle and copy what is found in that area to my clipboard. A console command should also not require extra cursor motion and a click in order to send something to the clipboard.

Another idea to ensure knowledge and consent:
  • The player.copy_to_clipboard function has two parameters: 1. the string to be copied 2. a descriptive name for it
  • When called for the first time, a popup appears, very similar to the blueprint export popup, but with the title "Copy <descriptive name> to clipboard", and additionally a checkbox (unticked) labelled "Allow automatic clipboard writing for <mod name>". You can click the "Copy" button, and if you want also tick the checkbox.
  • When automatic copying is allowed by the player, there can be a flying text or chat notification with the text "<descriptive name> copied to the clipboard" whenever the function is called. (There could also be an interface setting, or even a per-player mod setting that is not changeable by the mod, to disable that.)
  • Mods have to declare in info.json that they want to be able to send text to the clipboard. Without this the function does nothing when called. When it's properly declared, the mod manager can show a checkbox to allow/disallow automatic copying, and it's defaulted to disallow.
This kind of permissions system could also be used for other things like blueprint library manipulation.

Also, since some people use clipboard history software, there should be some indications/warnings for heavy write activity, like >100kB per second.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2530
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Ability to copy string to clipboard using api

Post by FuryoftheStars »

If the game has a distinction between a manually entered console command and something a mod can automatically do, then sure, of course. (I've never actually checked if a mod can execute a console command directly or not.)

Sometimes, though, I have something sitting in my clipboard that's semi important to me that I just haven't had the chance to paste somewhere, yet. Sometimes, too, it's currently the only existing copy, or would require some time and hassle to get back to the original. If a mod had the ability to send something to my clipboard, overwriting it without my performing a specific action that I knew would cause that (clicking a UI button, pressing ctrl+c, click+dragging with a tool, typing a console command, or whatever), I would be pretty miffed if that happened, and pissed if it kept happening.

So my point is, no matter what method gets introduced, if it gets introduced, it needs to be something that requires explicit, obvious consent from the player. But the only way I can see that working is if the send to clipboard function is not exposed to us directly, and rather the ability to present the tool/UI element to the player is, without the ability mod side to completely overwrite the caption (thus not being able to remove a portion of the name or description that explicitly states in some fashion "send to OS clipboard") and/or just execute it, because otherwise that defeats the point. These tools/UI elements could potentially have an event they fire off, presenting the data that will be sent to the clipboard before it actually is so we could do some processing with it, but having direct access to a send to clipboard api should not happen because then there's no reliable way to control if/when it gets called.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

User avatar
_CodeGreen
Long Handed Inserter
Long Handed Inserter
Posts: 60
Joined: Sat Mar 05, 2022 11:30 am
Contact:

Re: Ability to copy string to clipboard using api

Post by _CodeGreen »

FuryoftheStars wrote:
Tue Feb 20, 2024 11:44 am
Sometimes, though, I have something sitting in my clipboard that's semi important to me that I just haven't had the chance to paste somewhere, yet. Sometimes, too, it's currently the only existing copy, or would require some time and hassle to get back to the original.
If you're on windows, you can turn on clipboard history with Win + V, and you can paste from the past few things you've copied, instead of only having one slot available. It's a lot nicer not having to worry about overwriting things I might want to paste, because I can always just press that keybind and get it from a few copies ago.
My Mods | If you can't make it perfect, make it adjustable

Post Reply

Return to “Modding interface requests”