Separating copy to clipboard

Viewing 8 reply threads
  • Author
    Posts
  • November 17, 2022 at 4:21 AM #48256

    Chris Ju
    Participant

    Hi,

    i’m trying to copy some variables to clipboard. However, I would like these to be copied separately to the clipboard. I define the variables (var1, …, var9), but when I use the Utils.copyTextToClipboard(var1) … Utils.copyTextToClipboard(var9) command, only the last variable is copied to the clipboard. I’m using Launchbar-App, with which i could see a history of the clipboard.

    I already used a delay script, between the variables/copy-cammand, but that doesn’t helped.

    Does anyone has an idea?

    Thanks
    Chris

    November 17, 2022 at 10:56 AM #48260

    Daniel Leu
    Participant

    Yeah, looks like a bug to me.

    I just run a little script and indeed, only ‘four’ appears in the clipboard history (CopyClip):

    function Add_To_Clipboard() {
    	Utils.copyTextToClipboard("one");
    	Utils.copyTextToClipboard("two");
    	Utils.copyTextToClipboard("three");
    	Utils.copyTextToClipboard("four");
    }
    
    Add_To_Clipboard();

    A workaround would be to put all the variables into a JSON formatted string and copy that to the clipboard.

    November 18, 2022 at 2:18 AM #48264

    Brendan
    Keymaster

    I’m not sure why this isn’t working with a clipboard manager. Maybe it’s going too quickly pushing onto the clipboard. I use Pastebot and it has the same issue. I’ll have to do some digging. Maybe I need to create an artificial delay.

    November 18, 2022 at 2:20 AM #48265

    Brendan
    Keymaster

    Yup. That might be it. I just put in an artificial delay and it seems to store each separate value on the clipboard now. But that’s not a great solution to the problem.

    November 20, 2022 at 2:11 AM #48278

    Chris Ju
    Participant

    Did you put an artificial delay in java script or in the code of TF?

    For the first case, it would be great if you could share the script, because my delay doesn’t work.

    November 21, 2022 at 5:19 PM #48283

    Brendan
    Keymaster

    I just put a sleep(2) or something like that in my Objective-C code just for testing purposes. I took it out already. Maybe there’s something similar with JavaScript.

    November 21, 2022 at 7:35 PM #48284

    Daniel Leu
    Participant

    I got it working with following code, but using while() is not really elegant and just consumes CPU cycles

    function sleep(delay) {
        var start = new Date().getTime();
        while (new Date().getTime() < start + delay);
    }
    
    function Add_To_Clipboard() {
    
    	Utils.copyTextToClipboard("one");
    	sleep(200);
    	Utils.copyTextToClipboard("two");
    	sleep(200);
    	Utils.copyTextToClipboard("three");
    	sleep(200);
    	Utils.copyTextToClipboard("four");
    }
    
    Add_To_Clipboard();

    When I use sleep(100), not all copies are recored. So the delay might depend on the performance of the target computer.

    Re: Javascript, there is setTimeout(), but that’s part of DOM and not ECMAScript. Feature request: add Utils.setTimeout() and maybe Utils.clearTimeout() too. Then I could do

    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    
    for (let i = 0; i < 5; i ++) {
      if (i === 3)
        await sleep(2000);
      console.log(i);
    }
    • This reply was modified 2 years, 1 month ago by Daniel Leu.
    November 21, 2022 at 9:22 PM #48287

    Chris Ju
    Participant

    Thanks, Daniel. That is a workaround until Brendan will “fix” that “issue”…

    November 22, 2022 at 10:05 PM #48290

    Brendan
    Keymaster

    So I was just speaking to my friend who wrote Pastebot and he informed me that when you put something on the clipboard, there’s a changeCount property that gets updated on the NSPasteboard generalPasteboard. Pastebot polls this property to see if it has changed and then it records the value onto their multi-item clipboard.

    It’s possible the frequency of polling isn’t high enough to account for putting things on the clipboard as fast as Tap Forms can do it.

    So it would seem that’s why adding these artificial delays allows it to work because the polling frequency is then within the artificial delay time.

    November 22, 2022 at 11:16 PM #48291

    Daniel Leu
    Participant

    Yep, that makes sense.

    November 22, 2022 at 11:26 PM #48292

    Chris Ju
    Participant

    So this “bug” can only be fixed by the clipboard manager app?

    btw: Using an the artificial delay isn’t so bad… As Daniel already mentioned you only have to change the delay time depending on the speed of the cpu (on my M1 Max it must be at least 500 with the code of Daniel) …

    Thanks for your time and help!

    November 23, 2022 at 1:09 AM #48293

    Brendan
    Keymaster

    It would be much better if the changeCount property was observable. But apparently it’s not. So that’s why they poll it. But you also don’t want to be sitting there polling a variable in a really tight loop all the time either.

Viewing 8 reply threads

You must be logged in to reply to this topic.