Rank: Member
Groups: Approved
Joined: 9/25/2019(UTC) Posts: 10 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
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,349 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");
|
|
|
|
Rank: Member
Groups: Approved
Joined: 9/25/2019(UTC) Posts: 10 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.
|
|
|
|
Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,349 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
|
|
|
|
Rank: Member
Groups: Approved
Joined: 9/25/2019(UTC) Posts: 10 Thanks: 1 times
|
That is cleaner, so I will take it.
|
|
|
|
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.
Important Information:
The StrokesPlus.net Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close