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 8:45:08 AM(UTC)
markd

Rank: Member

Reputation:

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

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 10:40:55 AM(UTC)  | Reason: Not specified

Rob  
#2 Posted : Thursday, October 3, 2019 10:29:38 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 401
United States

Thanks: 5 times
Was thanked: 78 time(s) in 68 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 10:36:09 AM(UTC)
markd

Rank: Member

Reputation:

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

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 11:01:29 AM(UTC)
Rob

Rank: Administration

Reputation:

Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 401
United States

Thanks: 5 times
Was thanked: 78 time(s) in 68 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 11:05:55 AM(UTC)  | Reason: Removed .trim() from all as it caused issues when the line started with a space

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

Rank: Member

Reputation:

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

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.