AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

clipboard word count, possible?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
edu



Joined: 12 Oct 2006
Posts: 98
Location: Canada

PostPosted: Thu May 24, 2007 5:48 pm    Post subject: clipboard word count, possible? Reply with quote

Hello,

I wonder if it's possible to make a simple clipboard word count with AutoHotKey.
This is the farthest I got, just for testing purposes:

Code:
Loop, parse, clipboard, %A_Space%,
{
   MsgBox, %A_Index%
}
return


I guess I would get it if I could retrieve what is the number of the last loop, that is, the last %A_Index%.
Any suggestion?

Eduardo
Back to top
View user's profile Send private message
ggirf14



Joined: 01 Nov 2005
Posts: 96
Location: Ottawa

PostPosted: Thu May 24, 2007 6:24 pm    Post subject: Reply with quote

Try this:
Code:
Loop, parse, clipboard, %A_Space%,
{
   ;MsgBox, %A_Index%
Totalwords = %A_Index%
}

MsgBox, %Totalwords%
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6921
Location: Pacific Northwest, US

PostPosted: Thu May 24, 2007 6:24 pm    Post subject: Reply with quote

Code:

StringSplit, count, Clipboard, %A_space%`n, `r
msgbox, %count0%


This also takes into account carriage returns
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Titan



Joined: 11 Aug 2004
Posts: 5438
Location: /b/

PostPosted: Thu May 24, 2007 7:05 pm    Post subject: Reply with quote

A parsing loop outperforms StringSplit, so I'd use ggirf14's method. It can also be done with RegEx, so if you're interested:

Code:
RegExReplace(clipboard, "\b.+?\b", "", count)
count := Ceil(count / 2)
MsgBox, There are %count% words in the clipboard.

_________________

Back to top
View user's profile Send private message Send e-mail Visit poster's website
edu



Joined: 12 Oct 2006
Posts: 98
Location: Canada

PostPosted: Fri May 25, 2007 12:37 am    Post subject: Reply with quote

Wow! Thank you fellow users!
I found the script from ggirf14 more accurate.
Titan, I didn't know that a regexmatch could be converted into a number, or am I missing something?

Edu
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6921
Location: Pacific Northwest, US

PostPosted: Fri May 25, 2007 1:13 am    Post subject: Reply with quote

Manual - RegExReplace wrote:

OutputVarCount
The unquoted name of a variable in which to store the number of replacements that occurred (0 if none).

_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
YMP



Joined: 23 Dec 2006
Posts: 299
Location: Russia

PostPosted: Fri May 25, 2007 6:21 am    Post subject: Reply with quote

edu wrote:
I found the script from ggirf14 more accurate.

However I doubt it's accurate enough because it depends on the number of spaces between words. It also reports words to be on the clipboard when there are only spaces there. The same applies to engunneer's example.

Titan's code is the best, though it must depend on what is considered to be word and non-word characters, in which, as far as I know, AutoHotkey is currently fully reliable only for English texts. So a word having a national character in the middle will probably be taken for two words.

Perhaps this variation may be more 'international':
Code:

RegExReplace(Clipboard, "\S(?:\s|$)", "", Count)
Msgbox, % Count
Back to top
View user's profile Send private message
BoBoĻ
Guest





PostPosted: Fri May 25, 2007 8:29 am    Post subject: Reply with quote

StringReplace?
Quote:
When the last parameter is UseErrorLevel, ErrorLevel is given the number occurrences replaced (0 if none). Otherwise, ErrorLevel is set to 1 if SearchText is not found within InputVar, or 0 if it is found.


Code:
StringReplace, OutputVar, ClipBoard, Autohotkey, , UseErrorLevel
MsgBox, The word 'Autohotkey' occurs %ErrorLevel% times within the text.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group