How to shorten the words number in clipboard

Almost everyday I need to look for the specific titles of varoius scientific papers in databases (like Scopus, RG, Mendeley, etc.). Most of them work great and I use KM macro to look for the titles in 4 databases (I copy just the title to the clipboard, run the macro… and see the Safari window with the results). However, one database cannot be searched with the whole title (e.g. 20 words is far to many). How can I limit the number of words pasted from the clipboard to the search field of the database? I know the “substring of clipboard”, but this function works on characters. When it ends e.g. in the middle of a word, the database shows the error, so I need to have whole words. Help!

If you are running Yosemite or above, then you should be able to do something like this, which types out only the first N words in the clipboard, leaving the clipboard itself unchanged.

Paste only first N words from clipboard.kmmacros (19.4 KB)

1 Like

or if you simply want to abbreviate the clipboards contents, you could use something like:

(function (strN) {
	var a = Application.currentApplication(),
		sa = (a.includeStandardAdditions = true, a),
		strShorter = sa.theClipboard({
			as: 'string'
		}).split(
			/\s+/
		).slice(
			0, parseInt(strN, 10)
		).join(' ');
		
	sa.setTheClipboardTo(strShorter);
	
	return strShorter;
	
})("10");

or

// Discards any punctuation
(function (strN) {
	var a = Application.currentApplication(),
		sa = (a.includeStandardAdditions = true, a),
		strShorter = sa.theClipboard({
			as: 'string'
		}).split(
			/\s*\b\s*/
		).slice(
			0, parseInt(strN, 10)
		).filter(
			function (x) {
				return /[\w]+/.test(x);
			}
		).join(' ');
		
	return (
		sa.setTheClipboardTo(strShorter),
		strShorter
	);
	
})("10");

In place of the code above.

Thanks. It works great (I used the first solution).

1 Like

Rob always has a great JavaScript solution. But if you, like me, aren't running Yosemite yet, then this solution using only KM Actions will work.

This uses white space as word boundaries.
It will fail if there is any punctuation.

####I would like to modify to include all punctuation in the word boundaries.
So a word boundary could be any/all of the following in addition to the standard RegEx "White Space" of \s

.,;:!?-*_

Anyone know how?

###Download Here:
Get N Words Using RegEx.kmmacros (4.1 KB)

##screenshot

OK, I think I may have found the solution, but I don't know enough about RegEx to implement it:

Regex Boundaries and Delimiters—Standard and Advanced

Your next step could be to combine the two to form a boundary that can be popped on either side:

(?i)(?<=^|[^a-z])(?=[a-z])|(?<=[a-z])(?=$|[^a-z])

On the left side, of the alternation, we have our earlier left boundary, and we add a lookahead to check that what follows is a letter. On the right side of the alternation, we have our earlier right boundary, and we add a lookbehind to check that what precedes us is a letter.

What I don't understand is how to use this word boundary with a complete RegEx expression that includes the capture group.

When I enter just the above expression into a RegEx tester, it says:

No match groups were extracted.

This means that your pattern matches but there were no (capturing (groups)) in it that matched anything in the subject string.

Can anyone help?

Hey JM,

Stuff like this gets complex, as I mentioned in a recent thread about finding words. The punctuation possibilities are myriad.

^[[:punct:]]?((?:\b\w+\b)('[ds])?\W*){5}

Test text:

One two three's four. Five six seven.

This regex will leave whitespace at the end, so you have to clean that up if desired.

Note that this pattern isn't bombproof. I just threw it together as an example.

-Chris

1 Like

Thanks, Chris.

Fortunately I work on Yosemite :slight_smile: Thanks for the answers.

I have just posted the below macro that achieves this, and more.
I provides for user-defined delimiters for word boundaries.

###Get a Max of N Words in String Using RegEx