StrokesPlus.net
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
markd  
#1 Posted : Thursday, October 3, 2019 2:45:08 PM(UTC)
markd

Rank: Member

Reputation:

Groups: Approved
Joined: 9/25/2019(UTC)
Posts: 10
United States

Thanks: 1 times
A quick little script I used to get the word at the cursor position while I'm learning S+. This works, but is there an easier way to do it?

Code:

//gets the full word under the cursor
var head = "";
sp.SendKeys("+{HOME}");
sp.SendKeys("^c");
sp.Sleep(100);
head = clip.GetText();
var tail = "";
sp.SendKeys("+{END}");
sp.SendKeys("^c");
sp.Sleep(100);
var tail = clip.GetText();
var all = head + tail;
var curPos = head.length;
var spaceBefore = head.lastIndexOf(" ")
if(spaceBefore==-1) {spaceBefore=0;}
var spaceAfter = tail.indexOf(" ");
if(spaceAfter==-1) {spaceAfter = tail.length;}
var word = all.substr(spaceBefore,curPos-spaceBefore) + all.substr(curPos, spaceAfter);
word = word.trim();
sp.MessageBox(word, '');

Edited by user Thursday, October 3, 2019 4:40:55 PM(UTC)  | Reason: Not specified

Rob  
#2 Posted : Thursday, October 3, 2019 4:29:38 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
This seems to work for me. Really just leveraging existing Windows key shortcut behavior for selecting the word.

Code:
sp.SendKeys("^{RIGHT}"); //Ctrl+Right
sp.SendKeys("^+{LEFT}"); //Ctrl+Shift+Left
sp.SendKeys("^c"); //Ctrl+C
sp.Sleep(25); //Slight delay to ensure clipboard is ready
sp.MessageBox(clip.GetText().trim(), "Word");
markd  
#3 Posted : Thursday, October 3, 2019 4:36:09 PM(UTC)
markd

Rank: Member

Reputation:

Groups: Approved
Joined: 9/25/2019(UTC)
Posts: 10
United States

Thanks: 1 times
Yeah, I should have clarified -- I should have said that this is specifically for finding the word surrounded by space. Sometimes I need a little more than what the normal Ctrl-Shift-Arrow will select.

For example in tsql I may need a table name:
select * from dbo.employee

The Ctrl-Shift-Arrow won't go past the "." so I needed to get any text between the space.
Rob  
#4 Posted : Thursday, October 3, 2019 5:01:29 PM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 1,349
United States
Location: Tampa, FL

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)
Ah, yeah that's a little more complicated. Any variation of your code will probably end up being complicated in some way due to the fact that we can't obtain the cursor position without doing some stuff like what you're doing.

However, it can be simplified some by using split() to create arrays on space characters and grabbing the element based on the array length of just the head.
I've only done some quick testing, this may have edge case issues.

Code:
//gets the full word under the cursor
var head = "";
sp.SendKeys("+{HOME}");
sp.SendKeys("^c");
sp.Sleep(100);
head = clip.GetText();

sp.SendKeys("+{END}");
sp.SendKeys("^c");
sp.Sleep(100);

var all = head + clip.GetText();
//below is basically "allSplitOnSpaces[n]"
//where n is the index based on space count in "head" up to the cursor
var word = all.split(" ")[head.split(" ").length-1];

sp.MessageBox(word, '');

Edited by user Thursday, October 3, 2019 5:05:55 PM(UTC)  | Reason: Removed .trim() from all as it caused issues when the line started with a space

markd  
#5 Posted : Thursday, October 3, 2019 5:47:26 PM(UTC)
markd

Rank: Member

Reputation:

Groups: Approved
Joined: 9/25/2019(UTC)
Posts: 10
United States

Thanks: 1 times
That is cleaner, so I will take it.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.