Send text to clipboard for an application like Notepad (C # or Powershell)

I want to be able to send text to the clipboard, in Windows, in the application. For example, I am working on a text file in notepad, and I want to copy a part to a new file. I want to copy it to the clipboard, and then use the hotkey to launch the application or powershell script that sends this copied text to a new instance of Notepad.

How can I achieve this in C # or Powershell?

SOLUTION: Using AutoHotKey

^+c::
Send ^c
Run Notepad
WinWait Untitled - Notepad
WinActivate
Send ^v
return
+5
source share
5 answers

2 , PowerShell, - Autohotkey.

Autohotkey

;) , . :

^#n::
  Run, Notepad
  WinWaitActive Untitled - Notepad2
  Send !e
  Send p
  return

notepad2, Alt + E P. , . - Ctrl + V ( ). . - Autohotkey.

PowerShell

, Notepad2. /c Notepad2 .

, tnp, : ( , PowerShell -sta, )

function tnp {
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [object]
        $InputObject
    )
  begin   { $objs = @() }
  process { $objs += $InputObject }
  end {
        $old = Get-clipboard # store current value
        $objs | out-string -width 1000 | Set-Clipboard
        notepad /c
        sleep -mil 500
        $old | Set-Clipboard # restore the original value
  }
}

function Set-Clipboard { 
  param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)][object]$s
  )
  begin { $sb = new-object Text.StringBuilder }
  process {
    $s | % { 
      if ($sb.Length -gt 0) { $null = $sb.AppendLine(); }
      $null = $sb.Append($_) 
    }
  }
  end { Add-Type –a system.windows.forms; [windows.forms.clipboard]::SetText($sb.Tostring()) }
}

function Get-Clipboard {
  Add-Type –a system.windows.forms
  [windows.forms.clipboard]::GetText() 
}

- :

# gets list of members, opens Notepad2 and pastes the content (members list)
(get-date) | gm | tnp

- - , .

+2

, PowerShell Community Get-Clipboard, . , , , , :

Get-Clipboard > test.txt; notepad test.txt

, test.txt, test.txt .

+2

() :

  • .
  • .
  • .

[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[STAThread]
static void Main()
{
    var p = Process.Start("Notepad.exe");
    p.WaitForInputIdle();
    SetForegroundWindow(p.MainWindowHandle); // this can probably be left out.
    SendKeys.SendWait(Clipboard.GetText());
}

, , , - , :

[STAThread]
static void Main()
{
    var tempFilePath = Path.GetTempFileName();
    File.WriteAllText(tempFilePath , Clipboard.GetText());
    Process.Start("Notepad.exe", tempFilePath);
}
+2

AutoHotKey, ClipWait, , AutoHotKey , Windows

^+c::
Send ^c
ClipWait
Run Notepad
WinWait Untitled - Notepad
WinActivate
Send ^v
return

If you want to use the clipboard only as a temporary means for transferring text (so as not to lose what you previously saved in the clipboard), you can add something like the following:

^+c::
ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
Send ^c
ClipWait   ; Wait for the clipboard to change
Run Notepad
WinWait Untitled - Notepad
WinActivate
Send ^v
Clipboard := ClipSaved   ; Restore the original clipboard.
ClipSaved =   ; Free the memory in case the clipboard was very large.
return
+1
source
    Dim temp = System.IO.Path.GetTempFileName()
    System.IO.File.WriteAllText(temp, Clipboard.GetText())
    Process.Start("Notepad.exe", temp)
0
source

All Articles