Highlighted

Copy multi-line strings to the clipboard on windows

Community Beginner ,
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);

 

 

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"';
        }

 

TOPICS
How to, Scripting

Views

689

Likes

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

Copy multi-line strings to the clipboard on windows

Community Beginner ,
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);

 

 

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"';
        }

 

TOPICS
How to, Scripting

Views

690

Likes

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
May 08, 2020 0
Most Valuable Participant ,
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

Likes

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
Reply
Loading...
May 08, 2020 1
Community Beginner ,
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.

Likes

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
Reply
Loading...
May 08, 2020 0
Community Beginner ,
May 08, 2020

Copy link to clipboard

Copied

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"';
        }

 

Likes

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
Reply
Loading...
May 08, 2020 0
Participant ,
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.

Likes

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
Reply
Loading...
May 08, 2020 2
Community Beginner ,
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.

Likes

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
Reply
Loading...
May 08, 2020 0
Enthusiast ,
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.

Likes

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
Reply
Loading...
May 08, 2020 1
Community Beginner ,
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.

Likes

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
Reply
Loading...
May 08, 2020 0