Skip to content
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

Closed
Max-Enrik opened this issue Apr 15, 2021 · 57 comments
Closed

Is it possible to copy info to clipboard #8730

Max-Enrik opened this issue Apr 15, 2021 · 57 comments

Comments

@Max-Enrik
Copy link

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?

@CounterPillow
Copy link
Contributor

Sure, but this will depend on your OS and your display protocol, e.g. on Linux with X11 I use the following in my input.conf:

C run "/bin/sh" "-c" "echo -n \"${path}\" | xclip -i -selection clipboard"

@Akemi
Copy link
Member

Akemi commented Apr 15, 2021

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.

@Akemi Akemi closed this as completed Apr 15, 2021
@Max-Enrik
Copy link
Author

Looks like, I have to be more specific because I am not getting there. Could I ask that how to copy Title/File name?

@CounterPillow
Copy link
Contributor

I don't know what platform you're on so I can't give you a specific answer.

@Max-Enrik
Copy link
Author

Max-Enrik commented Apr 16, 2021

Oh! Sorry! PC Win 10 x64.

@CounterPillow
Copy link
Contributor

Put this in input.conf:

C run "powershell" "set-clipboard" "${filename}"

then you can copy the filename to your clipboard by hitting shift+c.

@Max-Enrik
Copy link
Author

Max-Enrik commented Apr 16, 2021

Thanks a lot.
But that doesn't copy anything.

@CounterPillow
Copy link
Contributor

It works for me. I will not spoonfeed you any further.

@Max-Enrik
Copy link
Author

How can I check logs?

@Max-Enrik
Copy link
Author

My MPV player location in a different location than C disk. Is it can be cause to not this command work?

@CounterPillow
Copy link
Contributor

no

@Max-Enrik
Copy link
Author

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.

@Max-Enrik
Copy link
Author

Thank you for your time.

@Max-Enrik
Copy link
Author

@CounterPillow - now perfectly work. Thank you again.

@Max-Enrik
Copy link
Author

Ops, I just realize that some of the video file names copied and some of not.

@bitraid
Copy link

bitraid commented Apr 17, 2021

Try this:
C run cmd /c echo ${filename} | clip

@Max-Enrik
Copy link
Author

I liked, copying all type of file names. Just a with little extras.

Video file name is.

Ashleui.Tedes.video.clip.mp4

Copied/Result of the command: One white space at the end of the file name and additionally empty line.

Ashleui.Tedes.video.clip.mp4 

Or Ditto and one white space at the end of the file name and additionally empty line.

"Ashleui.Tedes.video.clip.mp4" 

@bitraid
Copy link

bitraid commented Apr 17, 2021

Use this:
C run cmd /c echo:|set/p=${filename}|clip

@Max-Enrik
Copy link
Author

Amazing! Thanks a ton. That is what I was looking for. Again and again thank you a lot.

@Max-Enrik
Copy link
Author

Max-Enrik commented Apr 17, 2021

My questions won't be reach to the end.
I have to ask for one more thing. Copying name without .extension would be really awesome.

Since your last comment I tried to google and do it by myself, unfortunately I didn't make it happen.

@bitraid
Copy link

bitraid commented Apr 17, 2021

C run cmd /c echo:|set/p=${filename/no-ext}|clip

@Max-Enrik
Copy link
Author

Great! Thank you!

@Max-Enrik
Copy link
Author

If a video file has a & in it, it doesn't copy a name. Any help?
(I also search about it on google.)

@bitraid
Copy link

bitraid commented Jan 17, 2022

This will work:
C run cmd "/c echo:\"${filename/no-ext}\"|powershell set-clipboard $input.trim('\\\"')"

But if you want something that works with anything you throw at it (try for example a file with the name %path%etic %os%), better write a small program that copies to the clipboard, whatever is on its command line, and use this instead of the shell.

@Hrxn
Copy link
Contributor

Hrxn commented Jan 17, 2022

😄

I mean, simply using PowerShell in the first place would make more sense here..

C run "pwsh" "-w Hidden -nol -noni -nop -c Set-Clipboard" "${filename}"

Should even work with %path%etic %os%

@bitraid
Copy link

bitraid commented Jan 17, 2022

Using powershell like that doesn't work with more common characters, like ` and $, or ' - depending on quoting.

@Max-Enrik
Copy link
Author

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.

C run cmd "/c echo:\"${filename/no-ext}\"|powershell set-clipboard $input.trim('\\\"')"

I am very thankful to you for this!

@Hrxn
Copy link
Contributor

Hrxn commented Jan 17, 2022

Not sure, I didn't test it in mpv to be honest, but depending on what mpv does internally (Win32_Process::Create ?) it should still be somewhat analogous to something launched from CMD.

Using an example with a full path:

"C:\Program Files\PowerShell\7\pwsh.exe" -w Hidden -nol -noni -nop -c Set-Clipboard '${filename}'

Seems to work in this case.

But you're right, strings like %path%etic %os% get interpreted by CMD in this case and this is not what we want, true.
Not sure, but shouldn't be there a way to turn off interpretation of shell characters? Like --?

NB: This might still work from within mpv, just saying..

@bitraid
Copy link

bitraid commented Jan 17, 2022

Not sure, I didn't test it in mpv to be honest, but depending on what mpv does internally (Win32_Process::Create ?) it should still be somewhat analogous to something launched from CMD.

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 ${filename} before running the command.

Using an example with a full path:

"C:\Program Files\PowerShell\7\pwsh.exe" -w Hidden -nol -noni -nop -c Set-Clipboard '${filename}'

Seems to work in this case.

${filename} is not a shell variable – it is going to be replaced with the name of the file by mpv. You should test what happens when it contains special characters.

But you're right, strings like %path%etic %os% get interpreted by CMD in this case and this is not what we want, true. Not sure, but shouldn't be there a way to turn off interpretation of shell characters? Like --?

mpv does no “interpretation of shell characters”. The string %path%etic %os% is being expanded by cmd, because that’s what we chose to run.

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:
C run nircmd clipboard set ${filename}

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

@Hrxn
Copy link
Contributor

Hrxn commented Jan 18, 2022

Thanks for this, well, more involved example.. 😅

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 ${filename} before running the command.

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 ${filename} was just an example, which works without any issues called from CMD.

Whereas %path%etic %os% does not, I was just confirming what you have said earlier.

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 {mpv} -> {cmd} ->{powershell} and to go to pwsh directly, because with CMD it will trip over something like %path%etic %os%, indeed.

@bitraid
Copy link

bitraid commented Jan 18, 2022

My whole reason for replying was that it's probably advisable to skip the CMD step in {mpv} -> {cmd} ->{powershell} and to go to pwsh directly, because with CMD it will trip over something like %path%etic %os%, indeed.

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))

@Hrxn
Copy link
Contributor

Hrxn commented Jan 19, 2022

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 pwsh.exe and not powershell.exe, which will get you the old, Windows only powershell and not the cross-platform one.

Again: doing that is worse, because it will fail if a name contains any of powershell's special characters (it was already suggested:

Which special character would that be?

Because, in your own words:

mpv does not behave like a shell – it just replaces things like ${filename} before running the command.

@bitraid
Copy link

bitraid commented Jan 19, 2022

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 pwsh.exe and not powershell.exe, which will get you the old, Windows only powershell and not the cross-platform one.

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 powershell.exe, which is shipped with Windows.

Which special character would that be?

Seems that you don't read the recent comments either: #8730 (comment).

Because, in your own words:

mpv does not behave like a shell – it just replaces things like ${filename} before running the command

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.

@Hrxn
Copy link
Contributor

Hrxn commented Jan 19, 2022

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.
We're not talking about using this from within a shell.

I'm starting to doubt your reading comprehension, because you must've missed your own contradiction here:

Not sure, I didn't test it in mpv to be honest, but depending on what mpv does internally (Win32_Process::Create ?) it should still be somewhat analogous to something launched from CMD.

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 ${filename} before running the command.

Your first implication is using something like CreateProcessX is not at all comparable to launching from shell, then you
say, the problem is not mpv, it's powershell and cmd, because of special characters which are also valid filename characters and are passed to them unescaped.. so it is analogous to using from shell after all?
You should probably make up your mind here first.

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.

@garoto
Copy link
Contributor

garoto commented Jan 19, 2022

My poor-man solution was just using a +20yo win32 port of GNU echo:

run "cmd.exe" "/d" "/c" "echox.exe -En ${?path:\"${path}\"}|clip" ; show-text "File Path/URL copied to clipboard (${path})"

@bitraid
Copy link

bitraid commented Jan 19, 2022

Your first implication is using something like CreateProcessX is not at all comparable to launching from shell, then you
say, the problem is not mpv, it's powershell and cmd, because of special characters which are also valid filename characters and are passed to them unescaped.. so it is analogous to using from shell after all?

The results might be different. See for example the contents of the clipboard when you run powershell -c set-clipboard "'%path%'" from cmd, and then from mpv console using the run command.

You should probably make up your mind here first.

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.

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) [..]

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.

[..] 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.

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?

@garoto
Copy link
Contributor

garoto commented Jan 19, 2022

Imagine thinking powershell is really something worthwhile to spend time learning, lol. You people need a reality check and stop shilling garbage software from mega-corps.

@Hrxn
Copy link
Contributor

Hrxn commented Jan 20, 2022

The results might be different. See for example the contents of the clipboard when you run powershell -c set-clipboard "'%path%'" from cmd, and then from mpv console using the run command.

As I was saying....

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.

As I was saying....

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.

Yes.

[..] 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.

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?

"Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can."
— Zawinksi's Law of Software Envelopment

And we are really expected to believe that mpv can't output base64-encoded string values?
I want to talk to the manager!!11

Still leaves the other two workarounds:

  1. From mpv, writing to and reading from a system environment variable
  2. mpv to a file and then to clipboard with pwsh -c '(set-clipboard (get-content .\temp.txt))' , works even for bastardi$zed $strings and %path%s $values

Imagine thinking powershell is really something worthwhile to spend time learning, lol. You people need a reality check and stop shilling garbage software from mega-corps.

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..

@bitraid
Copy link

bitraid commented Jan 20, 2022

[..]
As I was saying....

Maybe there is a misunderstanding here, because I thought you said the complete opposite:

Not sure, I didn't test it in mpv to be honest, but depending on what mpv does internally (Win32_Process::Create ?) it should still be somewhat analogous to something launched from CMD.
[...]

.

And we are really expected to believe that mpv can't output base64-encoded string values?
I want to talk to the manager!!11

Still leaves the other two workarounds:

From mpv, writing to and reading from a system environment variable
mpv to a file and then to clipboard with pwsh -c '(set-clipboard (get-content .\temp.txt))' , >works even for bastardi$zed $strings and %path%s $values

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)

@bitraid
Copy link

bitraid commented Jan 21, 2022

Also, I know this answer is not for me:

Imagine thinking powershell is really something worthwhile to spend time learning, lol. You people need a reality check and stop shilling garbage software from mega-corps.

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..

..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! 😆

@garoto
Copy link
Contributor

garoto commented Jan 21, 2022

that garoto didn’t understand the nature of the problem either [...]

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 echo.exe from unxutils was the perfect tools for the job. Granted it needs that property check beforehand (for complicated reasons I imagine) so it can copy any and all chars to the clipboard with a 100% rate of success. All the while requireing zero extra dependecies since unxutils binaries link directly to msvcrt.dll.

@garoto
Copy link
Contributor

garoto commented Jan 21, 2022

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 cmd.exe, something that would be their own version of an emulated nix shell, with persistent history, ASCII-based configuration files, generally a POSIX compilant shell with benefits.

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 conhost.exe for all its terminal shenanigans, lmao.

@bitraid
Copy link

bitraid commented Jan 22, 2022

I understand the nature of the problem perfectly, [...]

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:
run cmd /c echo:|set/p "=${path}"|clip ; show-text "File Path/URL copied to clipboard (${path})"

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 run command we can only add quotes to the string, cmd is a better fit for filenames than powershell (because of the characters that can be passed unescaped). But for the contetns of ${title} for example, an unescaped string might even be dangerous (an attacker could craft a string which would execute arbitrary shell commands).

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 cmd.exe, something that would be their own version of an emulated nix shell, with persistent history, ASCII-based configuration files, generally a POSIX compilant shell with benefits.

*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).

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 conhost.exe for all its terminal shenanigans, lmao.

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.

@Max-Enrik
Copy link
Author

Thank you so much more for the great script.

I modified it for more functionality.
I need to share it here and see if this is right or wrong. So far, I haven't had any issue with this modified script, but it would be great to get some feedback.

Original script owner @bitraid

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

local function copy_path()
    local path = string.format("%s", mp.get_property_osd("path"))
    mp.osd_message(string.format("Copied to Clipboard: %s", path))
    path = "'"..path:gsub("'", "''").."'"
    mp.commandv("run", "powershell", "set-clipboard", path);
end

local function copy_mediatitle()
    local mediatitle = string.format("%s", mp.get_property_osd("media-title"))
    mp.osd_message(string.format("Copied to Clipboard: %s", mediatitle))
    mediatitle = "'"..mediatitle:gsub("'", "''").."'"
    mp.commandv("run", "powershell", "set-clipboard", mediatitle);
end

mp.add_key_binding("C", "copy_filename", copy_filename)
mp.add_key_binding("Ctrl+c", "copy_path", copy_path)
mp.add_key_binding("Alt+C", "copy_mediatitle", copy_mediatitle)

@bitraid
Copy link

bitraid commented Feb 8, 2022

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)

@Max-Enrik
Copy link
Author

Very well, thank you a lot!

I want a specific name for each of these string.format(). I tried something but nothing good. May I ask how to add these names to them?

mp.osd_message(string.format("Filename: %s", filename))
mp.osd_message(string.format("Path: %s", path))
mp.osd_message(string.format("Media Title: %s", mediatitle))

@Max-Enrik
Copy link
Author

It already took a lot of time but can't do it.
Any help would be appreciated.

@guidocella
Copy link
Contributor

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)

@Max-Enrik
Copy link
Author

@guidocella
Thank you so much!

@kohane27
Copy link

kohane27 commented Jun 28, 2022

Hello there. Hope you're doing well.

How do I adopt this script for Linux?

I was using f run "/bin/bash" "-c" "echo ${filename/no-ext} | xsel -ib" but I want some visual indicator of filename being copied.

~/.config/mpv/scripts/copy_filename.lua:

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 mp.commandv("run", "xclip", "-sel", filename), as nothing is copied to clipboard.

Any input is much appreciated. Thank you:)

@guidocella
Copy link
Contributor

Install xclip.

@kohane27
Copy link

kohane27 commented Jun 28, 2022

@guidocella

Thank you for looking into my issue. I appreciate it.

I have xclip in my system:

❯ xclip -version
xclip version 0.13

@guidocella
Copy link
Contributor

guidocella commented Jun 28, 2022

You are running the command xclip -sel $filename, it should be echo -n $filename | xclip -selection clipboard.

You can show a OSD message with the binding f run sh -c 'printf %s "$0" | xclip -selection clipboard' ${filename/no-ext}; show-text 'Copied to clipboard: ${filename/no-ext}'

@kohane27
Copy link

kohane27 commented Jun 28, 2022

@guidocella

Thank you for getting back to me. I appreciate it:)

The following doesn't work:

mp.commandv("run", "echo", "-n", filename, "|", "xclip", "-selection", "clipboard")

Could you enlighten me as to what went wrong?

But your binding works flawlessly. Thank you!

@guidocella
Copy link
Contributor

It is explained in the documentation of run. You need run sh -c '...' to run shell commands.

@KonoVitoDa
Copy link

KonoVitoDa commented Dec 17, 2022

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)

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?

[copy_to_clipboard] stack traceback:
[copy_to_clipboard]     ...it-2207236/portable_config/scripts/copy-to-clipboard.lua:9: in function 'fn'
[copy_to_clipboard]     mp.defaults:230: in function 'fn'
[copy_to_clipboard]     mp.defaults:65: in function 'handler'
[copy_to_clipboard]     mp.defaults:380: in function 'handler'
[copy_to_clipboard]     mp.defaults:510: in function 'call_event_handlers'
[copy_to_clipboard]     mp.defaults:552: in function 'dispatch_events'
[copy_to_clipboard]     mp.defaults:503: in function <mp.defaults:502>
[copy_to_clipboard]     [C]: at 0x7ff72f6f9ba0
[copy_to_clipboard]     [C]: at 0x7ff72f6f8280
[copy_to_clipboard] Lua error: ...it-2207236/portable_config/scripts/copy-to-clipboard.lua:5: attempt to concatenate local 'value' (a nil value)```

@N1ck01
Copy link

N1ck01 commented Jul 31, 2024

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.
It seems not to be but is it possible to get copy to clipboard to work for ${metadata/by-key/ytdl_description}?
I've had no success trying all the various run cmd/powershell binds with changing "${filename}" to "${metadata/by-key/ytdl_description}" or with all the various lua codes with changing to "metadata/by-key/ytdl_description" and to "mp.get_property" where needed so I think...
Apparently there are working ways to copy to clipboard various info like filename, path, media-title, and subtitles, and it would be too bad if ytdl_description couldn't be copied too because it can contain useful info as well.
TY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants