hluk / copyq-commands

Useful commands for CopyQ clipboard manager.
331 stars 73 forks source link

Error Modifying Selected Text #28

Closed ventolinmx closed 2 years ago

ventolinmx commented 4 years ago

Hi

Trying modify-selected-text.ini and getting the following error:

Exception in "Template: Modify..."
Failed to copy to clipboard
Exit code: 4
1. copyq:
2. function modifyText(text)

I would like to remove end of line replacing EOL with space.

hluk commented 4 years ago

It seems that something is blocking the copy operation.

Does it always give the error? What's your OS?

ventolinmx commented 4 years ago

Yes, i always get that error. Haven't been able to use that command. My OS is Debian 10 Buster.

hluk commented 4 years ago

What's your desktop environment? Any chance you're using Wayland and not X11?

ventolinmx commented 4 years ago

It's Gnome 3.30.2 :)

hluk commented 4 years ago

Can you try running copyq copy TEST from terminal to see if it copies "TEST" text to clipboard?

ventolinmx commented 4 years ago

Yes, i get TEST copied to clipboard.

hluk commented 4 years ago

Can you check what's in the clipboard after the error "Failed to copy to clipboard"? You can inspect the clipboard from the main window in CopyQ with Ctrl+Shift+C default shortcut (menu bar: File - Show Clipboard Content).

ventolinmx commented 4 years ago

Nothing, it is empty. A couple of seconds pass from pressing the shortcut of the command to the display of the error notification.

hluk commented 4 years ago

Ah, OK, I think the copy() call (the one without argument) is failing. My guess is that the target app doesn't support copying using Ctrl+C (that's what CopyQ emulates) or the shortcut is not sent properly.

You can try using xdotool utility to send shortcuts instead:

[Command]
Command="
    copyq:
    function modifyText(text)
    {
      // TODO: Modify text here!
      return text.replace('\\n', ';')
    }

    function sendShortcut(shortcut)
    {
        // Wait for keyboard modifiers to be released.
        // Alternative would be \"--clearmodifiers\" parameter for xdotool,
        // but it can behave erratically.
        while (queryKeyboardModifiers().length > 0) {
            sleep(50);
        }

        var result = execute('xdotool', 'key', shortcut)
        if (!result) {
            throw 'Failed send shortcut ' + shortcut
        }
        if (result.exit_code != 0) {
            throw 'Failed send shortcut ' + shortcut + ': ' + result.stderr
        }
    }

    sendShortcut('ctrl+c')

    var text = str(clipboard())
    popup(text)
    var newText = modifyText(text)
    if (text == newText)
      abort();

    copy(newText)

    sendShortcut('shift+Insert')"
GlobalShortcut=ctrl+shift+s
Icon=\xf15b
IsGlobalShortcut=true
Match=/\\/(?:)\\//
Name=Template: Modify Selected Text
Window=/\\/(?:)\\//
ventolinmx commented 4 years ago

Before doing that i would like to try the script. Running it from the menu in order to bypass shortcuts i realized it only replaces the first occurrence. Why is this?

Thank you.

hluk commented 4 years ago

String.replace() function only replaces the first occurrence, unless you use regular expression.

text.replace(/\\n/g, ';')

I fixed the command to do this.

ventolinmx commented 4 years ago

Great!! Now i can paste continuous text from pdfs formatted with line breaks or columns. I'll try the command with xdotool. I just need a little more time to figure that out. Thanks for your support.