We help IT Professionals succeed at work.
Get Started

Curious how Expert Awards are earned? Find out on this week's podcast, featuring Experts Exchange CEO Randy Redberg.Listen Now

x
Private

AutoHotkey: GUI for retention/removal operation applied on clipboard

319 Views
1 Endorsement
Last Modified: 2020-09-17
Hello experts,

I set up the following AutoHotkey script in order to retain or remove lines from a clipboard based on string reported in the GUI.

+^r::
AddTextEditWidth:=400
Gui,Add,Text
Gui,Add,Text,xm,Which operation to you want to performed?
Gui,Add,Radio,Checked vWhichOp xm,Retain
Gui,Add,Radio,x+20,Remove
Gui,Add,Text
Gui,Add,Text,xm,Enter the string related to retain or remove operation (or click Cancel not to add text):
Gui,Add,Edit,vAddTextRetainRemove xm w%AddTextEditWidth%
Gui,Add,Text
Gui,Add,Button,xm gButtonAddTextRetainRemoveOK Default,&OK
Gui,Add,Button,x+10 gButtonAddTextRetainRemoveCancel,&Cancel
Gui,Show,,Add text
Return

ButtonAddTextRetainRemoveOK:
Gui,Submit
Gui,Destroy
Send, ^c
ClipWait,1
If (ErrorLevel=1)
{
  MsgBox,4144,Error,No text appeared on clipboard after initial Ctrl+c and waiting for one second
  Return
}
Else
ReferenceString:=AddTextRetainRemove
NewClipboardVar:=""
Loop,Parse,Clipboard,`n,`r
If (WhichOp=1) 
{
  If InStr(A_LoopField, ReferenceString)
    NewClipboardVar:=NewClipboardVar . A_LoopField . "`n"
}
Else
{
  If InStr(A_LoopField, ReferenceString)
    Continue
    NewClipboardVar:=NewClipboardVar . A_LoopField . "`n"
}
LastChar:=SubStr(NewClipboard,0,1) ; see if last char is CR or LF
If ((LastChar="`r") or (LastChar="`n"))
{
  StringTrimRight,NewClipboard,NewClipboard,1 ; last char is CR or LF - remove it
  LastChar:=SubStr(NewClipboard,0,1) ;  see if new last char is CR or LF
  If ((LastChar="`r") or (LastChar="`n")) ;
    StringTrimRight,NewClipboard,NewClipboard,1 ; new last char is CR or LF - remove it
}
Clipboard:=NewClipboardVar
ClipWait,2
If (ErrorLevel=1)
  MsgBox,4144,Error,No text appeared on clipboard with blank lines removed after waiting for two seconds`nThis should never happen
Else
  Send ^v
ButtonAddTextRetainRemoveCancel:
Gui,Destroy
Return

Open in new window


I tested on Notepad++/Windows 10 home and it works.
Certainly some aspects should be reviewed, if so please let me know.

I would also take the opportunity to ask for some help in order to add an additional GUI radio with 3 options:
-Anyway option (checked) option: retention or removal will be performed as is, if one of the lines contains anyway the string reported
-Beginning second option: retention or removal will be performed if one of the line begins with string reported
-End third option: retention or removal will be performed if one of the line ends with string reported

Thank you in advance for your help.
Comment
Watch Question
Developer
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 17 Answers and 42 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE