hluk / copyq-commands

Useful commands for CopyQ clipboard manager.
328 stars 72 forks source link

Detecting double copy #68

Closed marlarius closed 1 year ago

marlarius commented 1 year ago

Is it possible to detect if the same text is copied to the clipboard twice in a row?

I would like to run certains actions, such as opening an url in the browser, if it is copied twice. This is more convenient than eg. assigning a global shortcut as I can just press Ctrl-C twice.

The problem is that CopyQ automatically removes duplicates which is normally wanted. So the second time the clip is copied, nothing happens.

Is it possible to run an action at a point before CopyQ checks if the item already exists?

firefoxOnFire commented 1 year ago

I guess not possible, because when we copy same thing twice. Script doesn't get triggered on the second time. So applying automatic logic is impossible.

marlarius commented 1 year ago

That was my thought but maybe some internal process is happening which can trigger an action. After all, CopyQ at one point has to decide whether the new clip is a duplicate and should be rejected.

hluk commented 1 year ago

You can override the saveData() function with the following command (here is how to add the command to CopyQ):

[Command]
Command="
    var saveDataPrevious = global.saveData
    global.saveData = function() {
      var outputTab = str(data(mimeOutputTab))
      var text = str(data(mimeText))
      if (outputTab && text) {
        tab(outputTab)
        add(text)
      }
      saveDataPrevious()
    }

    global.onClipboardUnchanged = function() {
      onClipboardChanged()
    }"
Icon=\xf0c7
IsScript=true
Name=Always Save Clipboard
marlarius commented 1 year ago

Thanks, @hluk. It seems to work.

However, can you explain what the first part does? The thing is, overriding onClipboardUnchanged alone seems to be enough for my need (detecting a double-copy)

Also, is the attribute IsScript documented somewhere? I can guess that it tells CopyQ that this is a special scripting command, but I would like to read more about the possibilities. I can't find it in the docs.

CopyQ is simply fantastic and has quickly become an indispensable item in my toolbox. I have created many very useful command and would like to share them at some point.

[Command] Command=" global.onClipboardUnchanged = function() { popup(\"clipboard unchanged\") } " Icon=\xf0c7 IsScript=true Name=Detect unchanged

hluk commented 1 year ago

However, can you explain what the first part does? The thing is, overriding onClipboardUnchanged alone seems to be enough for my need (detecting a double-copy)

Ah, I think I misread the problem before. It is OK to just override global.onClipboardUnchanged as you say.

Also, is the attribute IsScript documented somewhere? I can guess that it tells CopyQ that this is a special scripting command, but I would like to read more about the possibilities. I can't find it in the docs.

IsScript flag in the command indicates Script type: https://copyq.readthedocs.io/en/latest/commands-script.html

CopyQ is simply fantastic and has quickly become an indispensable item in my toolbox. I have created many very useful command and would like to share them at some point.

Glad you like it. Happy scripting!