hluk / copyq-commands

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

Scripting top 10 Clips to FIle #57

Closed kevjustice closed 2 years ago

kevjustice commented 2 years ago

I am trying to export the top 10 clips from the clipboard into a text file that can be read by MATRIC (an android dashboard for Windows). Here's what I've got so far but I'm missing something on how to reference the correct items.

var filePath = 'E:\\VSDev\\matric\\copyq\\clipboard.txt'
function storeClipboard() {
    var f = File(filePath)
    if ( !f.openWriteOnly() ) {
    popup(
        'Failed saving clipboard',
        'Cannot open file: ' + filePath
        + '\n' + f.errorString())
    return;
    }
    selectItems(0,1,2,3,4,5,6,7,8,9)
    for(i=0;i<=9;i++) {
        var item = selectedItemData(i)
        //var text = JSON.stringify(item)
        var text = str(item[mimeText])
        if (text.length>15) {
            linedata = i+1 + ': ' + text.substring(1,15) + '...\n'
        } else {
            linedata = i+1 + ': ' + text + '\n'
        }
        f.write(linedata)
    } 
    f.close()
}
var onClipboardChanged_ = global.onClipboardChanged
global.onClipboardChanged = function() {
    onClipboardChanged_()
    storeClipboard()
}

The goal output is something like:

1: somecopied text 2: some really long ... 3: //image// <=note I have not added code for this yet 4: 123456 etc to 10

hluk commented 2 years ago

selectedItemData() would only return data of the items selected before the command is executed - selectItems() won't affect it.

You could use something like the following instead:

const sel = ItemSelection().selectAll();
const texts = sel.itemsFormat(mimeText).slice(0, 10);
for (const text of texts) {
    ...
}