Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[Class] WinClip - direct clipboard manipulations


  • Please log in to reply
94 replies to this topic
tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008
trying to use winclip to replace hotstrings with rtf content (another thread started in ask help forum -> link). Here is the code I have and its not working .. how do I load rtf file and then paste that rtf content from clipboard to my active winidow? Simple WinClip.paste also did not work for me (commented out line)

::utsig::
bytes := Winclip.Load("c:\bin\autohotkey\rtf\utsignature.rtf")
Winclip.SetRTF
;WinClip.Paste("Test")
WinClip.Paste
Return

OK, this code worked for simple paste, Now I will try reading a simple text file .. wc is defined earlier as wc := new WinClip

::utsig::
;bytes := WinClip.Load("c:\bin\autohotkey\rtf\utsignature.rtf")
;WinClip.SetRTF()
wc.SetText("Test")
wc.Paste()
;WinClip.Paste()
Return

Reading a text file didn't work. hmm what am I not doing right? and I get a beep after this one.
::utsig::
bytes := wc.Load("c:\bin\autohotkey\rtf\test.txt")
wc.Paste()
Return


Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
hello
bytes := Winclip.Load("c:\bin\autohotkey\rtf\utsignature.rtf")
methods Load() and Save() both work with binary content of clipboard, therefore Load() intended to load full clipboard from file saved eariler with Save()

to load RTF file onto clipboard, use following example:
rtf := FileOpen( "c:\bin\autohotkey\rtf\utsignature.rtf", "r" ).read()
WinClip.SetRTF( rtf )

wc.SetText("Test")
wc.Paste()
by doing this, you set the "Test" string onto clipboard in Unicode format. By pasting to control which accept text clipboard format (like notepad ) you will see 'Test'. This action do not create file with this name

wc.Paste()
WinClip.Paste()
there is no difference between using this methods
The difference between instantiated 'wc' clipboard object and WinClip it self is that you may use 'i' prefixed methods with "wc" to keep several copies of clipboard, like this:
wc := new WinClip
wc.SetText("SomeText") ;sets text directly to the current clipboard
wc.iSetText("SomeText2") ;sets text to the memory buffer of 'wc' object
wc.iRestore() ;puts all this memory buffer has onto current clipboard


tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008
Thanks Deo, That worked beautifully. Now I don't have to depend on outlook signatures and outlook quick parts

Working Code
::utsig:: ; usual signature
WinClip.Clear()
rtf := FileOpen("c:\bin\autohotkey\rtf\utsignature.rtf", "r").read()
WinClip.SetRTF(rtf)
WinClip.Paste()
Return

Couple more questions .. if someone can help out. They are not probably winclip issues anymore ..

1. After the paste, the cursor goes to the next line. I am guessing that since we are reading a file, end of file is treated as end of line? What can I do to keep the cursor at the end character of the paste?

2. I would like to paste gvim editor syntax highlighted code as rtf. may be I should start a separate thread for this one .. I have searched around and found couple of possible ways .. Converting to html .. then saving to a file, opening in webpage and pasting .. (too much hassle) or finding a html2rtf utility for windows and modify an existing mac plugin for windows .. But may be ahk has some better way using winclip?

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010

1. After the paste, the cursor goes to the next line. I am guessing that since we are reading a file, end of file is treated as end of line? What can I do to keep the cursor at the end character of the paste?

first of all make sure you don't have new line character in your signature text. Then you may send backspace just after pasting, like this
SendInput {BS}


tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008
Thanks Deo.

This works on outlook, word and wordpad. However, it did not work on OneNote and Microsoft Office Communicator or pidgin :( Copying and pasting the same rtf from word/outlook to onenote works. (It does not work on communicator or pidgin, so I think communicator/pidgin does not support rtf ..) Any ideas on how to make it work in OneNote?

  • Guests
  • Last active:
  • Joined: --
Why are you so hung up on RTF? Look a few posts above and see the base64 solution or revert to fileappend clipboardall, fileread *c (and there are dozens of free cmdline utils for windows, dos & perl that do html to rtf conversion so why you would need to lookinto a Mac app is beyond me)

tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008
If this becomes completely unrelated to WinClip .. I will move this discussion to a new thread ..

Why are you so hung up on RTF?

For my original problem .. I want to replace a string with syntax color coded code or formatted paragraph or hyperlinked text. I know I can do it with RTF, so I started looking for that. Can I do the same with html?

Look a few posts above and see the base64 solution or revert to fileappend clipboardall, fileread *c (and there are dozens of free cmdline utils for windows, dos & perl that do html to rtf conversion so why you would need to lookinto a Mac app is beyond me)


Are we on to my second question here? What I want is to paste syntax colored code snippet from my most used editor (gvim) into outlook or onenote with the coloring intact. gvim does not provide this capability by itself. (notepad ++ has this capability, but I really like gvim). gvim does provide a way to save that snippet as html file. I found a mac plugin that uses this functionality and converts it to rtf and then pastes it to clipboard. (copy as rtf). I could do the same by replacing pbcopy with clip (w7 functionality) .. and html2rtf instead of textutil .. Anyway, I will look at base64 solution .. I am not sure what that is at the moment, but after some reading I should be able to find out. Now that you know what I am trying to do .. is there a different/better way?

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
why don't you use html itself? i'm sure outlook should understand it right
i just tried gvim and was able to copy snippets in following steps:
1. Copy html text from gvim editor (it copied as plain text, not html format, therefore we need to convert it to html )
2. Do the following:
htmlTxt := WinClip.GetText()
WinClip.SetHTML( htmlTxt )
3. Paste it somewhere pressing ctrl+v
4. It was successfully pasted into Thunderbird/msword.. :)

  • Guests
  • Last active:
  • Joined: --
@tnkrer: I see now what you mean. You want to copy plain text from your editor which has syntax highlighting into another program while keeping the colours (the syntax highlighting) intact.

tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008

why don't you use html itself? i'm sure outlook should understand it right
i just tried gvim and was able to copy snippets in following steps:
1. Copy html text from gvim editor (it copied as plain text, not html format, therefore we need to convert it to html )
2. Do the following:

htmlTxt := WinClip.GetText()
WinClip.SetHTML( htmlTxt )
3. Paste it somewhere pressing ctrl+v
4. It was successfully pasted into Thunderbird/msword.. :)


Deo, That worked great. Now I will try to code that as vim plugin. I will invoke autohotkey script inside vim plugin. The script will essentially have the same code that you have provided. (Never really executed autohotkey scripts like that .. so I hope it will work)
So final goal is .. in gvim .. I give a command :CopyHtml and my selected text is now available in html format on the clipboard.

Here is what the plugin code will look like. (changing from CopyRTF code by Nathan Witmer)

function! s:CopyHtml(line1,line2)

  if !exists(':TOhtml')
    echoerr 'TOhtml command not found'
    return
  endif

  call tohtml#Convert2HTML(a:line1, a:line2)
" Somehow copy this output to clipboard
" Invoke AutoHotkey with appropriate script or make an exe with that script and invoke that
  silent bd!
  echomsg "html copied to clipboard"
endfunction

@Guest: Thanks for pointing me to WinClip ..

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
tnkrer
cool, glad you found a solution :)

tnkrer
  • Members
  • 12 posts
  • Last active: Mar 06 2012 10:58 PM
  • Joined: 17 Dec 2008
my exe is not working inside gvim :(

When I run AutoHotKey.exe copyHtml.ahk .. it works (I am in the same directory where the WinClip files are) Here is copyHtml.ahk code

#Include WinClipAPI.ahk
#Include WinClip.ahk

htmlTxt := WinClip.GetText()
WinClip.SetHTML( htmlTxt )
Then I use ahk2exe and convert this script into an executable copyHtmlToClipboard.exe
I invoke that from my powershell window .. and it works fine ..

But from inside gvim .. no luck. Here is my code .. must be something with how I am calling the command. (exe is in $Path variable)
" convert the selected range into html and put it on current buffer
  call tohtml#Convert2HTML(a:line1, a:line2)
" copy current buffer to clipboard
  %y+
" use WinClip to convert the clipboard into html formatted text
  silent exe "!copyHtmlToClipboard.exe"
" delete the buffer
  silent bd!
  echomsg "html copied to clipboard"
endfunction

hmm looks like vim calls cmd.exe /c copyHtmlToClipboard.exe and that does not work :( However on cmd prompt copyHtmlToClipboard.exe runs just fine

edit: reboot of the computer fixed the problem. It seems that even though I modified env variable $Path (and was restarting gvim countless times) vim did not pick it up when it was starting a shell.
I will leave all my ramblings/codes here since I resolved both my questions using WinClip .. so they are related to WinClip and someone else may find it useful. Thanks Deo and Guest for your help

oldbrother
  • Members
  • 149 posts
  • Last active: Nov 09 2014 07:36 PM
  • Joined: 06 Jul 2005
Hi Deo,

Thank you so much for your great job!

Euro symbol does not working with your library. Can you fix it?

cost=€10000.00

if html
{
  WinClip.Clear()
  WinClip.SetHTML( cost )
  msgbox html data should be on clipboard
}
The content in clipboard after running this code is "10000.00", without "€"

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
hi, oldbrother
i've fixed this problem, this function will use UTF-8 encoding now
redownload please
and thank you for finding this!

oldbrother
  • Members
  • 149 posts
  • Last active: Nov 09 2014 07:36 PM
  • Joined: 06 Jul 2005
Well done!

Thank you very much!