Vscode API: read clipboard text content

I’m currently trying to write an extension for Visual Studio Code, and I can’t manage to understand how to read the clipboard content.

The VSCode API specifies this method:

readText ():Thenable<String>

Following what I read about Thenable, I should be able to get the clipboard’s text like that:

var clipboard_content = vscode.env.clipboard.readText().then((text)=>text);

But all I manage to get is a Promise { pending } object.

What I would like to get is the clipboard content as a string

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Basics mistake.

Because you use promises (async) and want async/await (linear) functionality.

It should be (promises, async code):

vscode.env.clipboard.readText().then((text)=>{
    clipboard_content = text; 
    /* code */
});

or (sequential code)

let clipboard_content = await vscode.env.clipboard.readText(); 
/* code */

PS.: In JS, you should use camelCase instead of snake_case when naming variables and functions. This is one of the recommendations of JavaScript Standard Style

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply