- AuthorPosts
- November 21, 2020 at 5:43 pm #27187QiaoJiaoParticipant
Is it possible to select random line and set cursor here in the selection?
I can do it for the whole file:
var lines_total = document.GetLines(); var line_random = Math.floor((Math.random()*lines_total)+1); document.selection.SetActivePoint(eePosLogical, 1, line_random);
But I can’t wrap my mind how to do this for selection, if it possible at all.
November 24, 2020 at 7:38 am #27190Yutaka EmuraKeymasterDo you want to select only one random line from the whole document? Then you can append this line:
document.selection.EndOfLine(true,eeLineLogical);
November 24, 2020 at 6:50 pm #27193QiaoJiaoParticipantI want to set cursor to a random line from selected lines. So if I select 3 lines, run script, it would set cursor to one of them randomly.
My code above works for whole file, I wonder is it possible to do this for selected lines.
Main problem is getting line number for selected lines, after that it is the same as in the previouse code.
November 25, 2020 at 3:15 am #27195LTTParticipantSomething like this?
yTop = document.selection.GetTopPointY(eePosLogical);
yBottom = document.selection.GetBottomPointY(eePosLogical);
var line_random = Math.floor((Math.random()*(yBottom – yTop + 1)) + yTop);
document.selection.SetActivePoint(eePosLogical, 1, line_random);November 26, 2020 at 5:05 am #27200QiaoJiaoParticipantYes, execty this. GetTopPointY is the magic I was looking for. Thanks a lot.
“–” in code is messed up with formating, should be “-“.
November 26, 2020 at 5:29 am #27201QiaoJiaoParticipantMy final super script: jump to random line in the file, if more than 1 line selected, jump to a random line from them.
The quirky thing here is to remove last “\n”, otherwise next line will also be included which we don’t want.var text = document.selection.Text; var last_char = text.slice(-1); if ( last_char === "\n" ) text = text.slice(0, -1); if ( text.indexOf("\n") === -1 ) { var lines_total = document.GetLines(); var line_random = Math.floor((Math.random()*lines_total)+1); } else { var yTop = document.selection.GetTopPointY(eePosLogical); var yBottom = document.selection.GetBottomPointY(eePosLogical); if ( last_char === "\n" ) yBottom = yBottom - 1; var line_random = Math.floor((Math.random()*(yBottom - yTop + 1)) + yTop); } document.selection.SetActivePoint(eePosLogical, 1, line_random);
- AuthorPosts
- You must be logged in to reply to this topic.