Split clipboard in 2 parts and keep only 1 Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Split clipboard in 2 parts and keep only 1

Post by tekla » 16 Jan 2022, 18:54

Hello!
I'm a very novice user.
I have some little past experience with scripting but haven't done much in many years (Mortscript... if you know, you know it was ages ago)
AHK is fairly more complex than Mortscript so I know it will take me a lot of time to get the job done, even if in an ugly unpolished way.

I need to automate a tedious repeating task at my practice.
I have a working script (probably fairly rough) that does the job perfectly but I still need to perform some manual work ater it runs.

I'm copying text from webpages into Excel (about 120 lines of text at a time)
the first ~20 lines are always rubbish that I end up having to manually remove afterwards....

If I could split the clipboard in 2 parts, at a specific word (I want only the 2nd part after that "separator" word), everything would be automatic.
(So long as it keeps the text structured in a way that would result in me pasting the desired ~100 lines in Excel)

I've been messing with the StrSplit function without luck... Ending with an empty variable / clipboard

AHK is fairly more complex than Mortscript so I know it will take me a lot of time to get the job done, even if in an ugly unpolished way.

Can anyone help?

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 16 Jan 2022, 20:04

Code: Select all

Clipboard =
(
and so
not this or
that is this very
question
)
Sleep, 100
; ----------------------------------------
separator = this
RegExMatch(Clipboard, separator "\s*\K.*", m), Clipboard := "", Clipboard := m
ClipWait, 0
If ErrorLevel
     MsgBox, 48, Error , An error occurred while waiting for the clipboard.
Else MsgBox, 64, Result, %Clipboard%

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 04:08

Thanks for the reply!
This looks like it would do the trick (it does perfectly with the string in the script)

But for the specific case results in a blank clipboard...
I attached an Excel with the structure it ends up when I currently past the data I copied (the website has a table like structure, but the script copis a bit of stuff outside / above the table too...)
The rubbish is always pasted only in column 1 in Excel.
What I want to keep is pasted in several columns starting in column 2 (13 columns to for this website)

does regex dismiss the other lines because the cell in the first column is empty?
can this be avoided
Attachments

[The extension xlsx has been deactivated and can no longer be displayed.]


User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 17 Jan 2022, 06:29

Are you copying from Excel? If so, what are you selecting? If not, then what is on the clipboard? Are you simply trying to copy all of the used cells except what is already in column A?

It sounds like you want to adjust the clipboard before you paste into Excel, right?

Code: Select all

Clipboard =
(
This		
is		
rubbish		
	this	a
	i	b
	want	c
	to	d
	keep	
)
Sleep, 100
; ---------------------------------
separator = rubbish

; F3::
RegExMatch(StrReplace(Clipboard, "`n`t", "`n"), separator "\s*\K.*", m)
Clipboard := "", Clipboard := m
ClipWait, 0
If ErrorLevel
     MsgBox, 48, Error , An error occurred while waiting for the clipboard. Aborting
Else MsgBox, 64, Result, %Clipboard%
Return

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 07:21

Hi, and thanks again!
I'm copying from a webpage (browsing with Chrome)
What I copy (and is parsed) is copied from Chrome
And pasting into Excel.

Website is password protected.
Here I attach a real example from what I copy (I removed private data).
I can't use the perfect separator because it has chars like "ç" so I used the line above ( "registos").
As a result all the text I get is from line 45 (the important part are the ones bellow)
Attachments

[The extension xlsx has been deactivated and can no longer be displayed.]


User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 17 Jan 2022, 07:38

It sounds like you want to adjust the clipboard before you paste into Excel, right?

Code: Select all

Gosub, Clip
; ---------------------------------
separator = registos

; F3::
RegExMatch(RegExReplace(Clipboard, "\n(\xa0|\s)+", "`n"), separator "\s*\K.*", m)
Clipboard := "", Clipboard := m
ClipWait, 0
If ErrorLevel
     MsgBox, 48, Error , An error occurred while waiting for the clipboard. Aborting
Else MsgBox, 64, Result, %Clipboard%
Return

Clip:
Clipboard =
(
													
Trace Covid-19													
 Início													
													
Vigilância COVID-19													
													
Pessoas													
													
Lista de Pessoas													
Template Adicionar													
Instituição 													
1													
Unidade Funcional 													
Todos													
Sub Unidade Funcional 													
Todos													
 Médico de Família (OM) 													
Todos													
 Sintomas na Última Vigilância 													
                             													
Todos													
Nº Utente													
Estado Vigilância 													
×													
×VP													
×VA													
×VS													
Estado Pessoa 													
Estado Exame 													
Todos													
 Última Vigilância Realizada 													
                             													
Todos													
 Última Alt. Est. Vigilância 													
                             													
Todos													
 Última Interação 													
                             													
Todos													
Universo de Pesquisa 													
Na minha instituiçãoTodas as instituições													
    Apenas os meus casos      Apenas as minhas interações     Pesquisa Avançada     Pesquisar   Limpar													
Mostrar 													
 registos													
	Estado	Estado Exame	Localização	Estado da Pessoa	Nome	Nº Utente	Data Nasc.	Telefone	Início Estado Vigilância	Fim Estado Vigilância	Última Alt. Estado Vig.	Última Vig. Realizada	Última Interação
													
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Passiva	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Passiva	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
         	Vigilância Sobreativa (MGF)	1	1	1	1	1	01/01/1900	1	01/01/1900 00:00	1	1	1	1
)
Sleep, 100
Return

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 07:41

Yes. So that I don't need to manually remove those lines in Excel (which is not that much time, using filters)

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 17 Jan 2022, 07:54

OK. In this case, posting the Excel file is informative, but the key information, not available here, is what is on the clipboard. You could try my last script, where I have made a guess about it.

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 08:16

I tried.
It gives me only the text in line 45 from the Excel (the lines bellow are not included, even if I clear line 46 which is empty or delec col A which cells are empty in the lines I want)
Either with clipboard copying from the webpage or from the Excel (after pasting from the webpage without removing formatting)

"
Estado Estado Exame Localização Estado da Pessoa Nome Nº Utente Data Nasc. Telefone Início Estado Vigilância Fim Estado Vigilância Última Alt. Estado Vig. Última Vig. Realizada Última Interação
"
I use a
clipboard := clipboard
in my script (the one wich doesn't try to remove the rubbish) which I assume removes formating and other stuff.

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 17 Jan 2022, 08:27

Without having a clipboard example, debugging is a challenge.

Test the posted script first, rather than your script. Does it work?

If additional adjustments are needed, you can post your clipboard here.

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 12:18

Thank you for all the help.

If I paste the clipboard in your script it works.
But it doesn't if it uses the clipboard itself (either the Excel I psoted or from the clipboard from the webpage)
I would need no more than your script, were it not that my script appends the clipboard about 120 times (each with rubbish)

How can I post my clipboard here?
In an Excel?

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 17 Jan 2022, 12:44

You can paste your clipboard into a post here. We are working with the text. Alternatively, paste it into a text file, and attach it to your post.

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 13:42

is txt good?
it pastes into Excel with correct structure and reproduces the issue (it misses the alst line which it shoud keep)
I would have to edit the a Word file just to fit the text and erase private data (to keep no text info)
Attachments
paste.txt
(944 Bytes) Downloaded 17 times

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1  Topic is solved

Post by mikeyww » 17 Jan 2022, 14:08

Code: Select all

separator = registos
; ---------------------------------
Gosub, F4
F4::Gosub, Clip
; ---------------------------------

F3::
RegExMatch(RegExReplace(Clipboard, "\n(\xa0|\s)+", "`n"), separator "\s*\K.*", m)
Clipboard := "", Clipboard := m
ClipWait, 0
If ErrorLevel
     MsgBox, 48, Error , An error occurred while waiting for the clipboard. Aborting
Else Send ^v
Return

Clip:
Clipboard =
(
Lista de Pessoas
Template Adicionar
Instituição 
Unidade Funcional 
Sub Unidade Funcional 
 Médico de Família (OM) 
 Sintomas na Última Vigilância 
Nº Utente
Estado Vigilância Vigilância PassivaVigilância AtivaVigilância Sobreativa (MGF)Sem Vigilância
Estado Pessoa SusceptívelExpostoSuspeitoInfectado (achado laboratorial)Infectado AssintomáticoInfectado SintomáticoCuradoÓbitoEstado Por Atribuir
Estado Exame 
 Última Vigilância Realizada 
 Última Alt. Est. Vigilância 
 Última Interação 
Universo de Pesquisa     Apenas os meus casos      Apenas as minhas interações     Pesquisa Avançada    
Mostrar 
10
 registos
Estado	Estado Exame	Localização	Estado da Pessoa	Nome	Nº Utente	Data Nasc.	Telefone	Início Estado Vigilância	Fim Estado Vigilância	Última Alt. Estado Vig.	Última Vig. Realizada	Última Interação
         	Vigilância Sobreativa (MGF)	Positivo	Domicílio	Infectado Sintomático	AA	31	21	98	20		>= 72h	< 24h	< 24h
)
Sleep, 100
SoundBeep, 1500
Return
image220117-1408-001_cr.png
After pasting into Microsoft Excel
image220117-1408-001_cr.png (19.89 KiB) Viewed 639 times

Another one is below.

Code: Select all

FileRead, Clipboard, %A_ScriptDir%\paste.txt
Sleep, 100
; ----------------------------

separator = registos

; F3::
RegExMatch(RegExReplace(Clipboard, "m`a)^\s+", ""), "s)" separator "\K.*", m)
Clipboard := "", Clipboard := Trim(m, "`r`n")
ClipWait, 0
If ErrorLevel
 MsgBox, 48, Error, An error occurred while waiting for the clipboard.
Return

tekla
Posts: 8
Joined: 16 Jan 2022, 17:45

Re: Split clipboard in 2 parts and keep only 1

Post by tekla » 17 Jan 2022, 15:20

That's it!
The second one works perfect!
Thank you very much!

I would have never got there on my own (not without learning a lot more about AHK... which I intend to do even more now)

User avatar
hoodlvoe
Posts: 17
Joined: 09 Jul 2019, 00:02

Re: Split clipboard in 2 parts and keep only 1

Post by hoodlvoe » 06 Nov 2023, 17:37

Is there a version 2.0 of this small script?

Is there a way to split two words of text and copy each one to a variable or such and paste either one as needed?

User avatar
mikeyww
Posts: 26859
Joined: 09 Sep 2014, 18:38

Re: Split clipboard in 2 parts and keep only 1

Post by mikeyww » 06 Nov 2023, 17:55

Perhaps:

Code: Select all

If RegExMatch(RegExReplace(A_Clipboard, 'm`a)^\s+', ''), 's)' separator '\K.*', &m)
 A_Clipboard := Trim(m[], '`r`n')

Code: Select all

#Requires AutoHotkey v2.0
word := (str, n) => StrSplit(RegExReplace(str, 's)\s+', ' '), ' ')[n]
str  := 'ab cd ef'
F2::Send word(str, 2)
F3::Send word(str, 3)

Code: Select all

#Requires AutoHotkey v2.0
str := 'ab cd ef'
F2::Send StrSplit(str, ' ')[2]
F3::Send StrSplit(str, ' ')[3]

Post Reply

Return to “Ask for Help (v1)”