Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • September 26, 2016, 02:03:43 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Writing custom script...?  (Read 1629 times)

PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Writing custom script...?
« on: May 31, 2015, 08:53:05 PM »
Hi:

I've been attempting to write a custom script to complement Clipboard Help+Spell, but I've been having so much difficulty I thought it might be time to ask for some help...

I use Clipboard Help+Spell on my computers at home and also at work.  It's a nice program,... I appreciate the effort that's gone into creating it.  I do have some experience with scripting and even some experience with complied coding, but largely that's when I was getting my computer science degree, which was in 2002.  So, I haven't done much complied coding in a long time.

Mostly I'm trying to find a way to parse the current clipboard entry into words.  This would mostly be helpful at work.  However, installing Clipboard Help+Spell is probably about they'll let me install on my computer at work.  Consequently, I'm limited in the tools I can use to solve this problem.

At first I thought maybe I could just do it in Batch, but I don't think there's any native way to get the clipboard's contents from Batch.  Clip will copy to the clipboard, but it gives you a newline character, which I don't want.  Further, once I decided that I really needed a regex to do any decent job of parsing the words out of the current clipboard entry and all that's naively available in Batch for regexes is Findstr... although perhaps I could do the job with Findstr, it's not a good regex processor... and I still can't get the clipboard's contents.  There are Batch add-ons which perhaps I could get away with putting on the computer at work, but the computer at work does have Powershell 3.0 and there are ways to get around having to change the execution policy... and, doing all these things in Powershell should be easier.

Perhaps someone else has an idea I haven't thought of, but I haven't thought of any way of doing what I want just within Clipboard Help+Spell.  The major obstacle here from what I know is taking one clipboard entry and making multiple entries from that.  So, one way or another, unless I'm missing something, I need some sort of custom script, although perhaps it could be a Quick Action?  What's more, I don't believe Clipboard Help+Spell's default mechanism of exchanging information with a custom script by passing the custom script an input and output file is going to work here... unless perhaps the script is called multiple times for each word parsed from the current clipboard entry.  Because... I don't believe there's any way to tell Clipboard Help+Spell that the output file contents aren't supposed to all be all one clipboard entry?  So, I decided it'd probably be best to have the script itself read the current clipboard entry and, of course, paste back separate clipboard entries for each word parsed from the current clipboard entry.

So, I devised a script that works with Powershell 5.0,... well, at least on one of my computer's at home which has Powershell 5.0.  Powershell 5.0 does now have native ability to copy and paste to the clipboard with a couple new utility cmdlets.  However, the scripts I'm using to copy and paste to the clipboard were targeted at earlier versions of Powershell... I'm not really certain why I can't find a Powershell script that appears to work as I wish in an earlier version of Powershell, particularly Powershell 3.0.  What's more, when I try to run it as a custom script from Clipboard Help+Spell, all that happens is an Windows Explorer window opens to the Clipboard Help+Spell directory.  However, even if I could get one of these Powershell scripts to run correctly from Powershell, none of them I've tested give all the words in the current entry... at least not in Powershell 3.0.  It doesn't seem to matter if I do it through the pipeline or iteratively, it won't do it correctly in Powershell 3.0.

So, I decided to try another scripting language and see if I can make it an exe... then, perhaps I can use that on the computer at work.  Perl was my first choice because there are so many libraries for Perl, so just everything you might want to do has already been done,... better than I could ever do anyway.  However, the problem with this approach is trying to find a way to make a Perl file into an exe...  It appears that's probably not viable, at least on 64-bit machine unless you have hundreds of dollars, at least at this time.  My impression is that the situation isn't much better with Ruby or Python.  Yes, I did think of a standalone VBScript, but the limitation here appears to be that Wscript and Cscript can only access the clipboard through Internet Explorer, and that always pops up a security warning...

So, then I said let's download Visual Studio 2015 and try a VB.net script...  The problem here is that these IDE's are really complicated nowadays and I'm stumped on numerous fronts here...  The most immediate problem is that I want a VB.net console application that accesses the clipboard, preferably through System.Windows.Forms, but no matter how many times I find another message board topic that says this should be no problem, I try to do as they instruct and I still always get a compile error.  The regex I have for Powershell works fine as it's a .NET regex, as is the regex for VB.net.  I'm hoping that someone here might be able to show me some code that allows a VB.net program to access the System.Windows.Forms clipboard functions without generating a compile error...?

Thanks,

Paul

PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Writing custom script...?
« Reply #1 on: May 31, 2015, 08:59:32 PM »
Last line should read:

I'm hoping that someone here might be able to show me some code that allows a VB.net CONSOLE program to access the System.Windows.Forms clipboard functions without generating a compile error...?

PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Writing custom script...?
« Reply #2 on: June 01, 2015, 02:21:20 AM »
I don't know why this didn't occur to me before... AutoHotkey!  And there is a .ahk to .exe converter!  That's got to be easier than trying to do it with VB...

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,252
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Writing custom script...?
« Reply #3 on: June 01, 2015, 09:37:12 AM »
ahk is a good general purpose windows solution.. though for heavy text manipulation, perl wouldn't be a bad idea either (as much as i hate perl).

now this jumps out at me:

Quote
What's more, I don't believe Clipboard Help+Spell's default mechanism of exchanging information with a custom script by passing the custom script an input and output file is going to work here... unless perhaps the script is called multiple times for each word parsed from the current clipboard entry.  Because... I don't believe there's any way to tell Clipboard Help+Spell that the output file contents aren't supposed to all be all one clipboard entry?  So, I decided it'd probably be best to have the script itself read the current clipboard entry and, of course, paste back separate clipboard entries for each word parsed from the current clipboard entry.

that's an interesting dilemma.

you are right that CHS is going to take whatever you output and make it as one clip.

your proposed workaround, of having your tool send multiple clips to the clipboard directly may not work.. that's because CHS probably (i dont exactly remember) disables clipboard monitoring while it runs a clip processing script.

now what i CAN do is put in place a way for the output format to specify multiple clips -- like by using an agreed upon separator like "@@@@CHS:NEWCLIP@@@@" and have CHS auto separate clips into multiple clips when processing.


PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Writing custom script...?
« Reply #4 on: June 01, 2015, 12:14:39 PM »
That'd be great!  I'll post my solution if I actually get something to work halfway reliably...!

PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Writing custom script...?
« Reply #5 on: June 01, 2015, 12:25:22 PM »
For what it's worth, this is the basic Powershell script I was messing with... this works on on one computer with Powershell 5.0.  I have another computer with Powershell 5.0 and it doesn't work... so, I've given up figuring out what's going on!  I did try the iterative Foreach, as well... seems to have the same behavior, so... I don't know.  But, anyway, when I try to run it from Clipboard Help+Spell, it just brings up a Windows Explorer window of the Clipboard Help+Spell directory...?!?  You don't what to see the copy with all the comments indicating all the changes... it's a total mess!

# powershell -ExecutionPolicy Bypass -File Drive:\Path\to\file\ClipboardEntryParse.ps1

function Get-Clipboard([switch] $Lines) {
   if($Lines) {
      $cmd = {
         Add-Type -Assembly PresentationCore
         [Windows.Clipboard]::GetText() -replace "`r", '' -split "`n"
      }
   } else {
      $cmd = {
         Add-Type -Assembly PresentationCore
         [Windows.Clipboard]::GetText()
      }
   }
   if([threading.thread]::CurrentThread.GetApartmentState() -eq 'MTA') {
      & powershell -Sta -Command $cmd
   } else {
      & $cmd
   }
}

function Out-Clipboard {
    [cmdletbinding()]
    param (
        [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]$InputObject,
        [switch] $File
    )
    begin {
        $ps = [PowerShell]::Create()
        $rs = [RunSpaceFactory]::CreateRunspace()
        $rs.ApartmentState = "STA"
        $rs.ThreadOptions = "ReuseThread"
        $rs.Open()
        $data = @()
    }
    process {$data += $InputObject}
    end {
        $rs.SessionStateProxy.SetVariable("do_file_copy", $File)
        $rs.SessionStateProxy.SetVariable("data", $data)
        $ps.Runspace = $rs
        $ps.AddScript({
            Add-Type -AssemblyName 'System.Windows.Forms'
            if ($do_file_copy) {
                $file_list = New-Object -TypeName System.Collections.Specialized.StringCollection
                $data | % {
                    if ($_ -is [System.IO.FileInfo]) {[void]$file_list.Add($_.FullName)}
                    elseif ([IO.File]::Exists($_))    {[void]$file_list.Add($_)}
                }
                [System.Windows.Forms.Clipboard]::SetFileDropList($file_list)
            } else {
                $host_out = (($data | Out-String -Width 1000) -split "`n" | % {$_.TrimEnd()}) -join "`n"
                [System.Windows.Forms.Clipboard]::SetText($host_out)
            }
        }).Invoke()
    }
}



$text = [String]::Join(' ', (Get-Clipboard))
  [regex]::Matches($text, '(\w+(?:[-'']\w+)*)') |
  ForEach-Object { Out-Clipboard $_.Value  }

PaulM

  • Participant
  • Joined in 2014
  • *
  • default avatar
  • Posts: 6
    • View Profile
    • Donate to Member
Re: Writing custom script...?
« Reply #6 on: June 09, 2015, 01:02:20 AM »
Hi:

Perhaps there's one thing that someone here can help me understand better...  When something gets copied to the clipboard what are the conditions that determine whether or not Clipboard Help+Spell will save the current entry,... that is, push the current clipboard entry onto it's stack or queue?  As opposed, perhaps, to the current entry just being overwritten, and not being pushed onto Clipboard Help+Spell stack or queue?

Is this determined entirely by Clipboard Help+Spell, or does it depend entirely on the particular method used to place contents on the clipboard, or is it some combination of the two?

Thanks