How to open a file externally using built-in commands?

Viewed 128

command:revealFileInOS is opening the folder where the file is, instead of opening the file.

Am I using the wrong command?

I also tried the command vscode.open but it opens the file inside of vscode.

I'm not trying to open the file in vscode, i'm trying to open it like if you were just double clicking/opening it.

const uri = vscode.Uri.file(item.originalImagePath);
const args = [uri];
const openFileCommandUrl = vscode.Uri.parse(
    `command:revealInExplorer?${encodeURIComponent(JSON.stringify(args))}`);

const browseFileCommandUrl = vscode.Uri.parse(
    `command:revealFileInOS?${encodeURIComponent(JSON.stringify(args))}`);

1 Answers

I don't think there is a built-in way to open a file in the OS's default application for that file within vscode. See Add ability to open files with the OS's default application: on the Backlog.

But this package looks promising: open, that is a npm link.

  1. npm install open in your extension folder. Make sure you can see it in your package.json devDependencies after you do this.

  2. const open = require("open"); or whatever you want to name it in the file(s) you want to use it in.

  3. await open(<yourFullPathFileNameHere>) so your item.originalImagePath should work there.

Here is how I tested it to have the current editor - containing an image - open that image in the default application:

const filePath = vscode.window.tabGroups.activeTabGroup.activeTab.input.uri.fsPath;
await open(filePath);       // using whatever you named it in the require

It works well for me on W11. There are a few options to use with the open command.

Related