-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy-text-to-clipboard function for scripts #7361
Comments
Makes sense to me personally. My only regret would be if it is limited to a single operating system - also because I am almost exclusively using linux these days. :-) |
mpv's internal console.lua has some minor clipboard support, but also does it by invoking new processes (see |
At least in Windows, I've been using this lua code to make it work. Probably easy to adapt it to work for macOS and Linux too. local utils = require 'mp.utils'
function set_clipboard(text)
utils.subprocess(
{ args = {
'powershell', '-NoProfile', '-Command', string.format([[& {
Trap {
Write-Error -ErrorRecord $_
Exit 1
}
Add-Type -AssemblyName PresentationCore
[System.Windows.Clipboard]::SetText('%s')
}]], text)
}, playback_only = false
})
end
mp.add_key_binding("ctrl+c", "copy-url-to-clipboard", function()
local text = mp.get_property_native('path', '')
if (text:find("http://") == 1) or (text:find("https://") == 1)
or (text:find("ytdl://") == 1) then
set_clipboard(text)
end
end) |
The whole point of this is wanting something that doesn't have to be adapted for different systems and doesn't involve passing things to a shell. |
Not sure if this is any better, but I found it easier to just write the clipboard contents to a file and read it from there: siikamiika/scripts@6235d22 +1 to the clipboard copy utility function |
You can use the env parameter to avoid janky shell quoting. Not sure how robust it is but it's probably better than that at least. local env = utils.get_env_list()
table.insert(env, "TEXT="..text)
mp.command_native_async({
name = "subprocess",
playback_only = false,
args = {"powershell", "-nop", "-c", "Set-Clipboard -Value $env:TEXT"},
env = env,
}, function(success, result, error)
-- ...
end) edit: nvm this is really flaky and I can't tell why, seems like env passing just makes it fail randomly |
This allows seek-to.lua to accept and paste timestamps from copyTime.lua directly. The format accepted is HH:MM:SS.SSS and it can be invoked with ctrl+v by default. The copy from clipboard code is for Windows only, but it can be modified to accept clipboard on other OS too, see below. Psst... occivink/mpv-scripts#43 Getting data from clipboard is harder than it looks, there isn't a native and unified command to communicate with the clipboard for all OS. Instead, you have to call a function/program specific to your OS through a shell to get your clipboard data. Here is how mpv officially does it: https://github.com/mpv-player/mpv/blob/master/player/lua/console.lua#L619-L672 Here is some issues related to it: `https://github.com/mpv-player/mpv/issues/7361` `https://github.com/mpv-player/mpv/issues/4695` `https://github.com/mpv-player/mpv/issues/4653` (backticks around it so I don't create a reference to all of them)
This allows seek-to.lua to accept and paste timestamps from copyTime.lua directly. The format accepted is HH:MM:SS.SSS and it can be invoked with ctrl+v by default. The copy from clipboard code is for Windows only, but it can be modified to accept clipboard on other OS too, see below. Psst... occivink/mpv-scripts#43 Getting data from clipboard is harder than it looks, there isn't a native and unified command to communicate with the clipboard for all OS. Instead, you have to call a function/program specific to your OS through a shell to get your clipboard data. Here is how mpv officially does it: https://github.com/mpv-player/mpv/blob/master/player/lua/console.lua#L619-L672 Here is some issues related to it: mpv-player/mpv#7361 mpv-player/mpv#4695 mpv-player/mpv#4653
I suppose one could generalize what |
Expected behavior of the wanted feature
Desired feature
I want a function that copies a string containing unicode text to the clipboard, including newlines. This can of course be used to implement #4695 in a script much more gracefully than the solution the responses to it came up with, but I'm not asking for what's described in #4695, just a function for scripts to use.
I last looked for a function like this several months ago. If one was silently added in the meantime without an issue being created for it then this feature request doesn't really mean anything, I guess!
Motivation
I wrote a script that automatically copies subtitles to the clipboard so that they can be access more easily by "clipboard grabbers".
Copying text to the clipboard automatically (not after a hotkey press) is an accessibility design pattern that's started appearing in games, having options to copy onscreen text to the clipboard so that screen readers can verbalize it.
This idea is not limited to disability accessibility, though; it's also used when consuming media that uses very difficult vocabulary or is in foreign languages. Having access to plain text on the clipboard makes looking up words that you don't know extremely easy, to the point where it's becoming increasingly common for people who start learning Japanese to set up clipboard reading systems and mouseover dictionaries when playing games in Japanese just so that they never have to worry about manually looking up kanji.
In turn, that's the motivation for my script. I don't personally need it, but some of my friends did, and they found it helpful.
It has a few problems, though:
The third point is the biggest problem. The script doesn't just work, and it's not possible for it to just work. It needs to be edited if the user is using certain locales for non-unicode programs, and using strange locales is very common among people who are learning Japanese.
It would be much, much simpler, more stable, and more reliable if there were just a function for copying text to the clipboard made available to scripts.
The text was updated successfully, but these errors were encountered: