在剪贴板文本发送到应用程序如记事本(C#或PowerShell的) [英] Send Text in Clipboard to Application like Notepad (C# or Powershell)

查看:298
本文介绍了在剪贴板文本发送到应用程序如记事本(C#或PowerShell的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够发送到剪贴板上的文字,在Windows中,一个应用程序。例如,我的工作在记事本中的文本文件,我想一个部分出复制到一个新file..I希望将它复制到剪贴板,然后使用热键启动发送应用程序或PowerShell脚本该文本复制到记事本的新实例。

我如何在C#或PowerShell的实现这一目标?

解决方案:利用​​AutoHotkey的

  ^ + C ::
发送^ C
运行记事本
WinWait无标题 - 记事本
WinActivate
发送^ V
返回


解决方案

我有两个解决方案,一个使用PowerShell中,对方使用的 AutoHotkey的

AutoHotkey的版本

我会用这一个;)可以定义绑定到按键自定义按键和行动。我的文件中包含该code:

  ^#N ::
  运行记事本
  WinWaitActive无标题 - 的Notepad2
  发送E!
  提交p
  返回

它运行的Notepad2然后模拟pressing Alt + E键和P.那膏字符串以同样的方式,你会preSS它自己。从某种原因,我遇到了一些问题,pressingCtrl + V键(我不记得任何更多)。欲了解更多信息看看AutoHotkey的网站。

的PowerShell版本

您需要使用像的Notepad2 的编辑器。随着开关 / C 它启动的Notepad2并粘贴剪贴板中的文本。

为了让我使用它的功能更加有用 TNP 这样定义:
(注意,您需要使用-Sta参数运行PowerShell中,否则他们将不会对propely工作)

 功能TNP {
    参数(
        [参数(强制性= $真实,ValueFromPipeline = $真实)
        [目的]
        $ InputObject
    )
  开始{$ OBJ文件= @()}
  流程{$ OBJ文件+ = $ InputObject}
  结束 {
        $旧=获取剪贴板#店内现值
        $ OBJ文件|出弦-width 1000 |设置剪贴板
        记事本/ C
        睡500 -MIL
        $旧|设置剪贴板#恢复原来的值
  }
}功能设置剪贴板{
  参数(
    [参数(强制性= $真实,ValueFromPipeline = $真实,位置= 0)] [对象] $ S
  )
  开始{$ SB =新的对象Text.StringBuilder}
  过程{
    $ S | %{
      如果($ sb.Length -gt 0){$空= $ sb.AppendLine(); }
      $空= $ sb.Append($ _)
    }
  }
  最终{添加型-a System.Windows.Forms的; [windows.forms.clipboard] ::的setText($ sb.Tostring())}
}函数获取剪贴板{
  添加型-a System.Windows.Forms的
  [windows.forms.clipboard] ::的getText()
}

使用这些功能,您可以运行这样的:

 #获取成员列表,打开的Notepad2和粘贴内容(成员列表)
(获取更新)|克| TNP

在换句话说 - 如果有些信息将返回并格式化屏幕,你可以得到它,并粘贴到记事本

I want to be able to send the text on the clipboard, in Windows, to an application. For example, I'm working on a text file in notepad, and I want to copy a portion out into a new file..I want to copy it to the clipboard and then use a hotkey to launch an application or powershell script that sends that 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

解决方案

I have 2 solutions, one that uses PowerShell, the other that uses Autohotkey.

Autohotkey version

I would use this one ;) You define custom key and actions bound to the keys. My file contains this code:

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

It runs notepad2 and then simulates pressing Alt+E and P. That pastes the string the same way as you would press it by yourself. From some reason I had some problems with 'pressing' Ctrl+V (I don't remember that any more). For more info have a look at Autohotkey's website.

PowerShell version

You need to use an editor like Notepad2. With switch /c it launches the Notepad2 and pastes the text from clipboard.

To make it more useful I use function tnp defined like this: (note that you need to run PowerShell with -sta parameter, otherwise they won't to work propely)

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() 
}

With these function you can run something like this:

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

In other words -- if some info would be returned and formatted to screen, you can get it and paste to notepad.

这篇关于在剪贴板文本发送到应用程序如记事本(C#或PowerShell的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆