hluk / CopyQ

Clipboard manager with advanced features
GNU General Public License v3.0
8.26k stars 430 forks source link

External Scripting Issue / Request to Create Script in JavaScript #2552

Closed andrewcincotta closed 6 months ago

andrewcincotta commented 7 months ago

Hello all! I am currently working on a Raycast extension, which you can find here! I have since found the quickest way to display clipboard contents would be through the use of the scripting API, in which a script could be passed on to the terminal command "copyq eval -{script}". I have successfully written a script in python that will get all of the clipboard contents of a given tab, and then save the contents and indexes to a 2d array.

My difficulty arises when I try to move this function into TypeScript. I am still a novice in both TypeScript and JavaScript, but I think recreating the following python script in JavaScript would be a great step into implementing this much needed change into my Raycast extension:

#!/usr/bin/env python3

import subprocess as sp
import json

tab = 'URLs'

script = f"""
tab('{tab}');
var result=[];
for ( var i = 0; i < size(); ++i ) {{
    var obj = {{}};
    obj.row = i;
    obj.mimetypes = str(read("?", i)).split("\\n");
    obj.mimetypes.pop();
    obj.text = str(read(i));
    result.push(obj);
}}
JSON.stringify(result);
"""

p = sp.run('copyq -'.split(), input=script,
           encoding='utf-8', stdout=sp.PIPE, stderr=sp.PIPE)
json_arr = json.loads(p.stdout)

items = []
rows = []
for json_obj in json_arr:
    text = json_obj['text']
    row = json_obj['row']
    text = " ".join(filter(None, text.replace("\n", " ").split(" ")))
    rows.append(row)
    items.append(text)

result_2d_array = [[json_obj['text'], json_obj['row']]
                   for json_obj in json_arr]
for item in result_2d_array:
    print(item)

Essentially, I am looking for someone to assist me in creating a javascript source that runs the above script through the copyq eval command, and then populates items and rows with a list of clipboard contents and their respective index.

hluk commented 6 months ago

Not sure if I understand. Do you need a script to print JSON-formatted text and row for each item in a tab in CopyQ? Something like:

for (var row = 0; row < size(); ++row) {
    const text = str(read(mimeText, row));
    print(JSON.stringify([text, row]));
    print('\n');
}
andrewcincotta commented 6 months ago

My fault this is an old issue. If you're interested in what I'm working on, you can visit this repo!