Page 1 of 2

How to trim extra space when selecting text?

Posted: Jul 17th, ’19, 15:35
by revavi
Hello,

I'm using Win 10. I have learnt that windows has this build-in feature that adds a space at the end of the selected text regardless of the program involved.
For example, if I double click on a word, there is always this extra space at the end the selection. For some reason, I want it removed. Please is there a way I could do this using this software?

Re: How to trim extra space when selecting text?

Posted: Jul 17th, ’19, 22:08
by Tom
What do you want to do with the selected text?

Re: How to trim extra space when selecting text?

Posted: Jul 18th, ’19, 14:48
by revavi
I want to paste it in Excel. I do this repeatedly.

Here is why:

I'm a foreign languages learner. I maintain an Excel spreadsheet where I collect new words I come across, which I then paste into a vocabulary learner. When I copy the word, it gets copied with that space. I paste it in the spreadsheet together with that space, but I didn't maintain consistency since the beginning. This spreadsheet has thousands of entries. To avoid wasting time on duplicates, I use the build-in search feature to check if the word is already there. However, if the text pasted in the search field has a space, but the the same word in the spreadsheet does not, the search function will give me a zero result although it is a duplicate. But if the the text being searched lacks a space but the spreadsheet has one, the search function retrieves it. There are no settings in Excel to change this. Since the spreadsheet is old and has no consistency, the only solution is to automatically select a text without that extra space.

I can't do all this manually because it is a recurrent activity. It costs so much time.

Thank you. I will be glad if you could help me.

Re: How to trim extra space when selecting text?

Posted: Jul 18th, ’19, 19:59
by Tom
You could make a shortcut (Type: Command).

Code: Select all

Send, ^c
Sleep, 200
Clipboard=%Clipboard%
Use this shortcut instead of Ctrl+C. Double-click the word to select it and then press a shortcut - the selection is copied to clipboard without unwanted spaces and formatting.

I would further improve the script to automatically collect all words into the txt file and then occasionally copy them to Excel.

Re: How to trim extra space when selecting text?

Posted: Jul 18th, ’19, 21:12
by revavi
Tom wrote: Jul 18th, ’19, 19:59 You could make a shortcut (Type: Command).

Code: Select all

Send, ^c
Sleep, 200
Clipboard=%Clipboard%
Use this shortcut instead of Ctrl+C. Double-click the word to select it and then press a shortcut - the selection is copied to clipboard without unwanted spaces and formatting.
It works. Thank you very much!
Tom wrote: Jul 18th, ’19, 19:59I would further improve the script to automatically collect all words into the txt file and then occasionally copy them to Excel.
I don't know if you are talking in general or about my situation.
By the way, I don't use any intermediary "txt file." When I come across an unfamiliar word, I check if it is already covered in the Excel file, if not, then check it in an electronic dictionary, copy the entry, and paste the word in one column and the entry in the column next to it. I finally gradually take portions of the Excel content and bulk-add it to the vocabulary learner.
My understanding is that these steps cannot be automated. If you think otherwise, I would be glad to know how.

Re: How to trim extra space when selecting text?

Posted: Jul 19th, ’19, 15:39
by Tom
It depends on your needs. Here is a quick example how you could do it using simple delimited text file. The following script checks if the selected word already exists in the list - if yes, it shows the translation, if not, it adds it to the end of the list and opens notepad where you can continue editing.

Code: Select all

filename:="words.txt"
delimiter:=";"

send, ^c
sleep, 200
word=%clipboard%

fileread, OUT, %filename%

found:=0
loop, parse, OUT, `n
{
	if A_LoopField contains %word%%delimiter%
	{
		stringreplace, str, A_LoopField, %delimiter%, `n`n
		msgbox, 64,, % str
		found:=1
		break
	}
}
if not found
{
	fileappend, `n%word%%delimiter%, %filename%, UTF-8
	run notepad %filename%
	sleep, 500
	send, ^{End}
}
return
There are many other possibilities...

Re: How to trim extra space when selecting text?

Posted: Jul 21st, ’19, 09:37
by revavi
I'm trying to make use of the code you suggested.

I copied a small portion of the excel file into a txt file with the filename you suggested to test the code. I gave the command a shortcut.

I put the txt file in desktop.

Then, I selected a word that is not in the file and typed the shortcut. Although the words.txt file is in desktop, a new notepad file opens telling me "Cannot find the words.txt file. Do you want to create a new file?"

Did I do something wrong?

I look forward to hearing from you. Thank you very much.

Re: How to trim extra space when selecting text?

Posted: Jul 22nd, ’19, 06:24
by Tom
The script assumes the words.txt file is in the FastKeys folder. To change this, change the first line of the code to the full path to your file.

Re: How to trim extra space when selecting text?

Posted: Jul 22nd, ’19, 09:09
by revavi
This is magical. From now on, I will use this and save much time and energy. Thank you very much!

Re: How to trim extra space when selecting text?

Posted: Jul 23rd, ’19, 11:11
by revavi
Dear Tom,

Can the command be taught to do the following as well:
When the selected word, which has a delimiter next to it (all of them do), already exists in the file, and the word has no text next to the delimiter (the words and their entries are separated by a line break), open the txt file and place the cursor next to the delimiter.
Example:
Book;[bʊk] noun [books] TEXT 1. <E > countable a set of pages that have been fastened together inside a cover to be read or written in • I took a book with me to read on the train. • She wrote a book on car maintenance.
Privilege;[no text]

After selecting the word 'Privilege' and typing the shortcut, I want the command to open the file and place the cursor next to the delimiter, but not if I do the same with the word 'Book'; here it should behave as it does now.

So, I want to keep the command as is now and only add the above instruction.

I want to add this because the txt file has thousands of similar cases that need to have their entries added to them immediately following the delimiter. When I was compiling this list two years ago, I wasn't in the habit of adding the word's entry immediately as I come across it. I learnt it is better to add the entry as I come across the word, not later, to save time.

With this the command will be perfect.

It would be greatly appreciated to hear from you, dear Tom. Thanks!