-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Is it possible to copy info to clipboard #8730
Comments
Sure, but this will depend on your OS and your display protocol, e.g. on Linux with X11 I use the following in my
|
there are also various scripts that could be adapted, if there isn't already one doing exactly what you want. https://github.com/mpv-player/mpv/wiki/User-Scripts also related #7361. |
Looks like, I have to be more specific because I am not getting there. Could I ask that how to copy Title/File name? |
I don't know what platform you're on so I can't give you a specific answer. |
Oh! Sorry! PC Win 10 x64. |
Put this in input.conf:
then you can copy the filename to your clipboard by hitting shift+c. |
Thanks a lot. |
It works for me. I will not spoonfeed you any further. |
How can I check logs? |
My MPV player location in a different location than C disk. Is it can be cause to not this command work? |
no |
I have to restart and try to implement that command in different PC to get clue what can be cause of this that not to work on my PC. |
Thank you for your time. |
@CounterPillow - now perfectly work. Thank you again. |
Ops, I just realize that some of the video file names copied and some of not. |
Try this: |
I liked, copying all type of file names. Just a with little extras. Video file name is.
Copied/Result of the command: One white space at the end of the file name and additionally empty line.
Or Ditto and one white space at the end of the file name and additionally empty line.
|
Use this: |
Amazing! Thanks a ton. That is what I was looking for. Again and again thank you a lot. |
My questions won't be reach to the end. Since your last comment I tried to google and do it by myself, unfortunately I didn't make it happen. |
|
Great! Thank you! |
If a video file has a |
This will work: But if you want something that works with anything you throw at it (try for example a file with the name |
😄 I mean, simply using PowerShell in the first place would make more sense here..
Should even work with %path%etic %os% |
Using powershell like that doesn't work with more common characters, like |
This command works perfectly. I tried to test for multiple different names which I had a problem with them copying their names, and now they copied without problems.
I am very thankful to you for this! |
Not sure, I didn't test it in mpv to be honest, but depending on what mpv does internally ( Using an example with a full path:
Seems to work in this case. But you're right, strings like NB: This might still work from within mpv, just saying.. |
It’s not, because you’re using a shell. Not all shells behave the same and mpv does not behave like a shell – it just replaces things like
mpv does no “interpretation of shell characters”. The string The problem here is that we run a shell command. Different shells have different special characters which may also be valid name characters. When that happens, depending on the shell (cmd/powershell) and the character, the shell command may fail or produce wrong results (because the special characters were not escaped). So, by using for example nircmd, it doesn't have this issue: If you don't like closed source programs, compile this with fasm (copies command line to clipboard): format PE GUI
entry start
section '.text' code readable executable
start:
call [GetCommandLineW]
mov esi,eax
mov edi,eax
xor ecx,ecx
xor eax,eax
not ecx
cld
repnz scasw
jnz exit
not ecx
cmp ecx,4096
ja exit
lodsw
dec cx
mov edi,esi
cmp ax,0022h
jz @f
mov ax,0020h
@@:
repnz scasw
jnz exit
cmp ax,0020h
jz @f
inc edi
inc edi
dec ecx
@@:
mov esi,edi
mov ebx,ecx
push 4096
push 2
call [GlobalAlloc]
push eax
push eax
push eax
call [GlobalLock]
mov ecx,ebx
mov edi,eax
rep movsw
call [GlobalUnlock]
push 0
call [OpenClipboard]
call [EmptyClipboard]
push 13
call [SetClipboardData]
call [CloseClipboard]
exit:
push 0
call [ExitProcess]
section '.idata' import data readable writeable
dd 0,0,0,RVA kernel_name,RVA kernel_table
dd 0,0,0,RVA user_name,RVA user_table
dd 0,0,0,0,0
kernel_table:
ExitProcess dd RVA _ExitProcess
GetCommandLineW dd RVA _GetCommandLineW
GlobalAlloc dd RVA _GlobalAlloc
GlobalLock dd RVA _GlobalLock
GlobalUnlock dd RVA _GlobalUnlock
dd 0
user_table:
CloseClipboard dd RVA _CloseClipboard
EmptyClipboard dd RVA _EmptyClipboard
OpenClipboard dd RVA _OpenClipboard
SetClipboardData dd RVA _SetClipboardData
dd 0
kernel_name db 'KERNEL32.DLL',0
user_name db 'USER32.DLL',0
_ExitProcess dw 0
db 'ExitProcess',0
_GetCommandLineW dw 0
db 'GetCommandLineW',0
_GlobalAlloc dw 0
db 'GlobalAlloc',0
_GlobalLock dw 0
db 'GlobalLock',0
_GlobalUnlock dw 0
db 'GlobalUnlock',0
_CloseClipboard dw 0
db 'CloseClipboard',0
_EmptyClipboard dw 0
db 'EmptyClipboard',0
_OpenClipboard dw 0
db 'OpenClipboard',0
_SetClipboardData dw 0
db 'SetClipboardData',0 |
Thanks for this, well, more involved example.. 😅
Yes, but I clearly said that. I don't know what mpv is doing internally and I haven't searched the code base because it's pretty huge, I was just assuming that it's probably using CreateProcess from the Win32 API. And Whereas But again, it might work from within mpv with a simple PowerShell example. I have not tested this for myself (yet), but I have clearly stated this. My whole reason for replying was that it's probably advisable to skip the CMD step in |
Again: doing that is worse, because it will fail if a name contains any of powershell's special characters (it was already suggested: #8730 (comment)) |
Well, I did not scroll all the way up to read comments from months ago, but there's still one issue with that example in the linked comment. The name of the executable for recent versions of powershell is
Which special character would that be? Because, in your own words:
|
Since you don't bother to read the previous comments, let me inform you that we are trying to give a solution for Windows. That's why we are using
Seems that you don't read the recent comments either: #8730 (comment).
Exactly. The problem is not with mpv but with powershell and cmd, and the fact that shells have special characters which are also valid filename characters (and are passed to them unescaped). Is that really so hard to understand, or didn't you read the full text of my comment because it was too "involved"? The command I suggested was for minimizing this issue, but you didn't bother to understand what is doing or why. Instead, you choose to enlighten us with your superior (untested) command (which doesn't work at all btw) and it's now the fourth time that I'm trying to explain to you, that it's not so. |
Well, the example I gave works for me, with the caveat already mentioned earlier, but I feel I'm repeating myself here.. Again, for the umpteenth time, this is not about any special characters as seen by the shell, that was already established earlier. I'm starting to doubt your reading comprehension, because you must've missed your own contradiction here:
Your first implication is using something like If you're so adamant about that this neither works in mpv nor from launch by a shell (I haven't still seen a proof yet here, just saying), you maybe should mention that there are other workarounds, like calling powershell with an base64 encoded command, or simply writing and reading any value to your system Env. |
My poor-man solution was just using a +20yo win32 port of GNU echo:
|
The results might be different. See for example the contents of the clipboard when you run
It’s not something that I believe. It is a fact that the parameters that a program receives with the CreateProcess call are exact, where the ones you type in a shell might not.
I didn’t say that this doesn’t work in mpv - the opposite. It’s not its job to escape the characters for the different shells. Any other program (that doesn’t modify the string when it contains certain characters) can accept the parameter just fine. I already gave examples.
Do you suggest that mpv should do that before calling powershell? Or writing a program that does all that instead of just copying the string directly to the clipboard? |
Imagine thinking |
As I was saying....
As I was saying....
Yes.
"Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can." And we are really expected to believe that mpv can't output base64-encoded string values? Still leaves the other two workarounds:
I mean, this isn't really about powershell, as you've shown with your own example. Although there are not many other cross-platform shells, as far as I know. Open source, even.. |
Maybe there is a misunderstanding here, because I thought you said the complete opposite:
.
Nothing of this belong to the mpv code. That’s what the plugin system is for. My solution was a quick answer to someone asking a question. A proper solution would require a small plugin: require 'mp'
local function copy_filename()
local filename = string.format("%s", mp.get_property_osd("filename/no-ext"))
mp.osd_message(string.format("Copied to Clipboard: %s", filename))
filename = "'"..filename:gsub("'", "''").."'"
mp.commandv("run", "powershell", "set-clipboard", filename);
end
mp.add_key_binding("C", "copy_filename", copy_filename) |
Also, I know this answer is not for me:
..but, there are a few cross-planform and open source shells (elvis, nu, cbsh, xonsh), and if you install msys2 on windows, you can also have bash, dash, fish, mksh, tcsh and zsh. And of course, every shell will have issues if you pass it an unescaped string. It’s obvious (from his example) that @garoto didn’t understand the nature of the problem either, while not missing the opportunity to preach the “You people” about what they need and what to do! 😆 |
I understand the nature of the problem perfectly, since copying shit to the clipboard while using mpv's "run" facilitiy is something I was investigating since the beginning. I just managed to find out that the tiny, 4.5k UPX compressed |
re: powershell Perhaps I just like to hold grudges, but when it was announced and when v1.0 released I was very disapointed since I was expecting MS to actually release an actual nextgen replacement for Imagine my face when browsing related forums and seeing that monstrosity was basically just an A.D. query tool in disguise, all the while using fricking |
If you understand the problem, then you should've known that it comes from cmd, so using a different echo won't make an actual difference. You can achieve the same result with cmd's internal echo: If you don't like surprises though, you should always make sure that a string is properly escaped before passed to a shell. For POSIX shells and PowerShell, it's easy: You enclose it in single quotes and then escape any single quotes that might be in the string (that's what the lua script I posted above does). For cmd it's more complex because you can't use single quotes like that, and you have to escape every special character in the string. But since with (directly using) the
*NIX and Windows are very different operating systems. I'm sure you've heard that "in UNIX everything is a file"? Well, in Windows everything is an API - that's why a shell where everything is an object makes sense. And that's why system administrators can't really write powerful scripts for Windows using the cygwin/msys2 ports of the POSIX shells, despite their advanced text manipulation capabilities. Before powershell, if you wanted to do something advanced from the shell in Windows, you'd also had to write a vbscript/jscript or even a console program, to communicate with the OS. Recent powershell versions have persistent history, and its configuration is a normal powershell script. It's not perfect of course, but either is POSIX (that's why modern shells like fish, elvish, ion, nu and xonsh don't follow the POSIX standard).
The terminal and the shell are different things. In recent Windows versions, a new API called ConPTY was introduced which is similar to the POSIX PTY model. Terminal emulators that use this new API are: Alacritty, Extraterm, Terminus, Wezterm, as well as the new Windows Terminal. |
Thank you so much more for the great script. I modified it for more functionality. Original script owner @bitraid
|
To avoid code duplication: require 'mp'
local function copy_to_clipboard(text)
mp.osd_message(string.format("Copied to Clipboard: %s", text))
mp.commandv("run", "powershell", "-nop", "set-clipboard", "'"..text:gsub("'", "''").."'");
end
mp.add_key_binding("C", "copy_filename", function() copy_to_clipboard(mp.get_property_osd("filename/no-ext")); end)
mp.add_key_binding("Ctrl+c", "copy_path", function() copy_to_clipboard(mp.get_property_osd("path")); end)
mp.add_key_binding("Alt+c", "copy_mediatitle", function()copy_to_clipboard(mp.get_property_osd("media-title")); end) |
Very well, thank you a lot! I want a specific name for each of these
|
It already took a lot of time but can't do it. |
local function copy_to_clipboard(key, message)
local value = mp.get_property(key)
mp.osd_message(message .. ": " .. value)
mp.commandv("run", "powershell", "-nop", "set-clipboard", "'" .. value:gsub("'", "''") .. "'")
end
mp.add_key_binding("C", "copy_filename", function() copy_to_clipboard("filename/no-ext", "Filename") end)
mp.add_key_binding("Ctrl+c", "copy_path", function() copy_to_clipboard("path", "Path") end)
mp.add_key_binding("Alt+c", "copy_mediatitle", function() copy_to_clipboard("media-title", "Media Title") end) |
@guidocella |
Hello there. Hope you're doing well. How do I adopt this script for Linux? I was using
require("mp")
local function copy_filename()
local filename = string.format("%s", mp.get_property_osd("filename/no-ext"))
mp.osd_message(string.format("Copied to clipboard: %s", filename))
mp.commandv("run", "xclip", "-sel", filename)
end
mp.add_key_binding("f", "copy_filename", copy_filename) The message on mpv is correct. But it seems there's problem with Any input is much appreciated. Thank you:) |
Install xclip. |
Thank you for looking into my issue. I appreciate it. I have
|
You are running the command You can show a OSD message with the binding |
Thank you for getting back to me. I appreciate it:) The following doesn't work:
Could you enlighten me as to what went wrong? But your binding works flawlessly. Thank you! |
It is explained in the documentation of |
The script breaks if trying to copy a property that the current file or frame don't has, like ${sub-text} or ${metadata/by-key/Comment}. Do anyone know how to fix?
|
Sorry for posting in a closed thread but I think it best pertains to it instead of creating a new one and has all the various examples used in it so I hope it's allowed. |
I'd like to copy name of the file is playing now. Is it possible to do that? And how can I do that? Any help?
The text was updated successfully, but these errors were encountered: