Paste clipboard text to multiple selected text files?

Hello, I was thinking this was more of a feature Directory Opus can do similar to Paste to Folders.

Basically, I copy a lot of text to files I already have created - such as http reference links. Mainly just text files such as (.txt, md, vb, ahk,ini). Lots of my notes are stored on markdown files too, so its great to paste to multiple text files.

Is there a way I can select several files and paste my clipboard to the top or bottom of each of the files?

This script adds the clipboard content to all selected files after the original content. Arrange the itemFile.Write lines, if you want a different order. You can also add line breaks to the files with itemFile.Write('\r\n').

// https://resource.dopus.com/t/paste-clipboard-text-to-multiple-selected-text-files/41787

// 2022-07-24

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (DOpus.GetClipFormat() != 'text') return;

    var clipText = DOpus.GetClip();

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();

        var itemFile = item.Open();
        var origText = itemFile.Read();
        itemFile.Close();

        itemFile = item.Open('w');
        itemFile.Write(origText);
        // enter line
        // itemFile.Write('\r\n'); 
        itemFile.Write(clipText);
        itemFile.Close();
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>AppendClipboard</label>
	<icon1>#clippaste</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/paste-clipboard-text-to-multiple-selected-text-files/41787</instruction>
		<instruction />
		<instruction>// 2022-07-24</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (DOpus.GetClipFormat() != &apos;text&apos;) return;</instruction>
		<instruction />
		<instruction>    var clipText = DOpus.GetClip();</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction />
		<instruction>        var itemFile = item.Open();</instruction>
		<instruction>        var origText = itemFile.Read();</instruction>
		<instruction>        itemFile.Close();</instruction>
		<instruction />
		<instruction>        itemFile = item.Open(&apos;w&apos;);</instruction>
		<instruction>        itemFile.Write(origText);</instruction>
		<instruction>        // enter line</instruction>
		<instruction>        // itemFile.Write(&apos;\r\n&apos;); </instruction>
		<instruction>        itemFile.Write(clipText);</instruction>
		<instruction>        itemFile.Close();</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>


4 Likes

I'm receiving an error in the script output.

 7/24/2022 6:12 AM  Error at line 25, position 9
 7/24/2022 6:12 AM  Invalid procedure call or argument (0x800a0005)

If you didn't change the script, I'd say one of the text files is a bit special... may be write-protected, in use or not a text file. Try different files or post more details.

I've tried it with multiple files and new files as well. (txt, md, ahk, and ini).

The error is pointing to

itemFile.Write(clipText);

Very odd...

Please insert these lines above the one you mentioned and check the log.

    DOpus.Output(item);
    DOpus.Output(typeof clipText);
    DOpus.Output(clipText);
    DOpus.Output('');

Here's the output on a new text file title "TextDocument.txt". The only text within the text file is "This is a test". I copied your sentence to clipboard prior to clicking the button

E:\TextDocument.txt
string
Please insert these lines above the one you mentioned and check the log

 7/24/2022 7:09 AM  Error at line 23, position 3
 7/24/2022 7:09 AM  Invalid procedure call or argument (0x800a0005)

Here is the exact script. Same as yours but with the additional lines you just added.


// https://resource.dopus.com/t/paste-clipboard-text-to-multiple-selected-text-files/41787
// 2022-07-24
function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;
    if (DOpus.GetClipFormat() != 'text') return;
    var clipText = DOpus.GetClip();
    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var itemFile = item.Open();
        var origText = itemFile.Read();
        itemFile.Close();
        itemFile = item.Open('w');
        itemFile.Write(origText);
        // enter line
        // itemFile.Write('\r\n'); 
		DOpus.Output(item);
	    DOpus.Output(typeof clipText);
	    DOpus.Output(clipText);
	    DOpus.Output('');

		itemFile.Write(clipText);
        itemFile.Close();
    }
}

Which versions of Opus and Windows are you using? Googling for the error code brings too many pages (for my taste) about VBScript and WinXP :thinking:

12.24. I can update on my personal computer, but my work licenses are managed under my company and I would need to go through them before doing so. The work one maybe around 12.23 but I will need to confirm

12.23 will fail, 12.24 is the minimum:

The File.Write method can now accept a string to write to the file as well as a blob/array. The string will be UTF-8 encoded.

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Changes/1224.htm

Hurry up and order the update, Jon and Leo have cooked up a lot of goodies for us. Don't miss out on them :slight_smile:

Txt file is not UTF-8 encoding, and error will occur. Simplified Chinese