jeff-hykin / macro-commander

💾 📦 ✅ Automate everything in VS code
MIT License
42 stars 8 forks source link

having trouble nesting commands #34

Closed carbtest closed 1 month ago

carbtest commented 2 months ago

For example i want to create a macro that will grab selected lines from editor join them using vscode macro-commander extension vs 1.5.1 by Jeff Hykin how how would I write a macro to

  1. preserve the clipboard
  2. grab selected text from active editor
  3. join the lines
  4. run that command in the active terminal window
  5. restore clipboard

here is a sample of what i have tried


      "workbench.action.terminal.focus",
      // combine javascript and commands
      {
        "injections": [
          //when this is enabled nothing outputs
          //   {
          //     "replace": "$oldClip",
          //     "withResultOf": "vscode.env.clipboard.readText()"
          //   },
         // this one will output the selected text (if its on one line)
           {
            "replace": "$selectedText",
            "withResultOf": "window.activeTextEditor.document.getText(window.activeTextEditor.selections[0])"
          }
          // unsure how to joinlines 
          //{
          //   "replace": "$command",
          //   // "withResultOf": "macroTools.escapeShellArg(window.activeTextEditor.document.getText(window.activeTextEditor.selections[0]))"
          //   "withResultOf": "await vscode.editor.action.joinLines();"
          // }
        ],
        "command": "workbench.action.terminal.sendSequence",
        "args": { "text": " $selectedText\n" }
      }
    ],```
jeff-hykin commented 1 month ago

While you can't nest commands (I'm not sure what you'd want that to do) you definitely can do the task you described.

Here's a macro of mine that might do what you want:

{
"SendLineToTerminal": [
    {
        "javascript": [
            "sharedMacroInfo.prevSelectedText = window.activeTextEditor.document.getText(window.activeTextEditor.selections[0])",
        ],
    },
    // format some python
    {
        "injections": [
            {
                "replace": "$currentLines",
                "withResultOf": "sharedMacroInfo.prevSelectedText"
            },
        ],
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            "text": "$currentLines\n\n"
        }
    },
    "cursorDown",
],
}

I'm not sure why you'd need to join the lines, I'm pretty sure the get-selection returns a string not an array. So you should be able to get

I've also updated "terminalExample2" in the readme.md to better show how javascript and normal commands can be combined.

Send another comment here if it still doesn't make sense