• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Copy multi-line strings to the clipboard on windows

Community Beginner ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

I've been using the following to copy text to the clipboard, but it doesn't handle the new-line character ('\n').  I've tried different combinations of escape characters but haven't found anything that works.

 

 

if (isWindows()) {
    cmd = 'cmd.exe /c cmd.exe /c "echo ' + content + '| clip"';
}
system.callSystem(cmd);

 

 

TOPICS
How to , Scripting

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , May 08, 2020 May 08, 2020

Thanks everyone for the good ideas!  I ended up going with Mylenium's temp file suggestion, but I'm sure the other solutions would just as well.  Here is my finished code - all this for simply copying text to the clipboard geez:

        if (isWindows()) {
            var tempPath = Folder.temp.toString() + "\\cbtemp.txt";
            var tempFile = new File(tempPath);
            tempFile.encoding = "UTF8";
            tempFile.open("w");
            if (tempFile.error != "")
                retu
...

Votes

Translate

Translate
LEGEND ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

Honestly, I don't think this will ever work. You probably need some weird regex ur sub-command loop in your command line string to encapsulate all characters to convert back and forth. The command line itself is essentially not designed to handle multiline text. It would probably more efficient to write your data to a file and retrieve it from there.

 

Mylenium

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

I need the data to end up in the clipboard, but your suggestion to save to a file is good.  I can probably pipe the contents of the file to the clipboard.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

LATEST

Thanks everyone for the good ideas!  I ended up going with Mylenium's temp file suggestion, but I'm sure the other solutions would just as well.  Here is my finished code - all this for simply copying text to the clipboard geez:

        if (isWindows()) {
            var tempPath = Folder.temp.toString() + "\\cbtemp.txt";
            var tempFile = new File(tempPath);
            tempFile.encoding = "UTF8";
            tempFile.open("w");
            if (tempFile.error != "")
                return errTxt + tempFile.error;
            tempFile.write(content);
            if (tempFile.error != "")
                return errTxt + tempFile.error;
            tempFile.close();
            cmd = 'cmd.exe /c cmd.exe /c "type ' + tempFile.fsName + '| clip"';
        }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

You can try utilize powershell for this,

 

var content = "Let say this is your content with spaces and  \n new line goes here \n and another line goes here";

//Check your content.
alert(content);


//Replace all the spaces in your content to the format Powershell Can Understand.
content = content.replace(/ /g,"$([char]0x0020)");

//Replace all the new lines in your content to the format Powershell Can Understand.
content = content.replace(/\n/g,"$([char]0x000a)");

	var isWindows = $.os.indexOf('Windows') !== -1;
    
	if (isWindows) {
        var cmd = 'cmd.exe /c powershell.exe Set-Clipboard '+'"'+content+'"';
	}
	system.callSystem(cmd);

 

Hope this will help.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

Thanks!  This seems like a good solution.  I ended up going with the temp file solution but I'm going to keep this in mind for furture projects.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

There is a way, but I wouldn't really recommend it:

you can replace "\n" in your string with "& echo.", then put parantheses around everything up to the pipe.

So if you want to output "hello\nworld", you would not do it like this:

cmd.exe /c "echo hello\nworld|clip"

But rather like this:

cmd.exe /c "(echo hello& echo.world)|clip"

But probably piping into a file is a better approach.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2020 May 08, 2020

Copy link to clipboard

Copied

Thanks!  I'd seen examples of this arounnd the internet but didn't understand it until you explained it.  This is interesting.  I wish I knew more about the windows command line.  I'm going to keep this in mind for future projects.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines