如何在vscode中将选择内容发送到外部终端?

4

我希望能够在文件中选择文本并将其发送到外部终端。这是否可能?

如果不行,是否可以通过AppleScript实现?


“将其发送到外部终端”是什么意思? - pvg
@pvg 基本上,复制并粘贴即可。 - nachocab
你想要粘贴到哪里?Terminal.app的一个窗口吗?哪个窗口?还是标签页?你想要实现什么目的并不是很清楚。 - pvg
@pvg 是的,iTerm.app 的当前活动选项卡。 - nachocab
如果不是很明智的话,这听起来是可行的。这个答案有一个指向一个操作选择的扩展的指针 https://dev59.com/yVsW5IYBdhLWcg3wQVb2 ,你可以修改它并从那里调用AppleScript(你可以使用shell命令调用AS)。 - pvg
显示剩余2条评论
1个回答

1

最终我使用AppleScript编写了自己的扩展程序来实现这个功能。代码如下:

function activate(context) {
  const runIterm = vscode.commands.registerCommand('run-external.iterm', function() {
    const editor = vscode.window.activeTextEditor;
    let textToPaste;
    if (editor.selection.isEmpty) {
      textToPaste = editor.document.lineAt(editor.selection.active.line).text;
    } else {
      textToPaste = editor.document.getText(editor.selection);
    }
    exec(
      `osascript ` +
        ` -e 'tell app "iTerm"' ` +
        ` -e 'set mysession to current session of current window' ` +
        ` -e 'tell mysession to write text "${textToPaste}"' ` +
        ` -e 'end tell'`
    );
  });

  context.subscriptions.push(runIterm);
}
exports.activate = activate;

我只用扩展名就能让它工作了。我需要对AppleScript做些什么吗? - Atanas Janackovski
@AtanasJanackovski 这个扩展正在生成AppleScript。不需要手动操作。 - nachocab
我无法让它工作。我通过VSCE命令在GitHub上打包了你的repo,安装了它,但是没有任何反应。我做错了什么? - Atanas Janackovski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接