Jump to content

Letter counter (# of characters in a string w/o spaces)?


Recommended Posts

I was wondering if it was possible to write up something to do this?

I'm getting into puzzle development and it would be so much easier when trying to figure out if a phrase is right length (i.e., not too long or too short) with something that I could paste to the clipboard and then have clipboard return # of characters in the string. What I've been doing so far is counting manually and it's starting to make my eyes hurt <g>.

Not to be confused with a word counter, there are a lot of those, the letter counter would count # characters while ignoring spaces in between words.

Lots of word counting utilities out there but so far nothing has come up, besides dead links, for a letter counter.

Thanks! :whistle:

Link to comment
Share on other sites

Hi!

I've taken a look at both of these and have found that the biggest drawback is that you have to edit the script each time (open, add string, etc.). I found out that MS Word's word count feature (under TOOLS > WORD COUNT) shows a letter count, too, with and without spaces, so this script is actually harder to use. But I still feel that it would be good to have something not dependent on something like the Office suite as I find myself on computers that don't have these apps installed such as at the library or internet cafés where I can use my thumb drive but don't have access to any applications other than what I carry, sometimes.

When I posted my question, I was hoping that there would be something with a user prompt box, which would make the most sense. I have one AI script myself that has the type of functionality that I need. But it has too many things that I can't seem to figure out how to adapt. I've been working on it this evening and I haven't gotten it to work.

Here is the type of thing I'd like to incorporate into the above letter count macros:

;
; AutoIt v3.0
;

; Turn CapsLock off if it is on.
Opt("SendCapslockMode", 0)
Send("{CapsLock off}")

;The number prompted for will appear wherever $CDnumber is found
$CDnumber = InputBox("Next number ...","What is the next number to use?", "", "", -1, -1, 600, 400)

Sleep(2000)

#include <date.au3>
$thisday = StringMid("Sn,Mn,Tu,Wd,Th,Fr,Sa", StringInStr("SunMonTueWedThuFriSat", _DateDayOfWeek(@WDAY,1) ), 2)

Sleep(2000)

Send("D-")
Send($CDnumber)
Send("_")
Send(@YEAR & @MON & @MDAY & $thisday)

Exit
; finished
Obviously, I've left the script intact, but I imagine a lot of it can go. None of the date/time items need to be included. I just didn't want to delete something, however, that I didn't understand and that would confuse experienced AI developers.

If this could be made to work, I'd like to compile it then share it. If it can be done, the ideal would be to display LETTER COUNT (without spaces), LETTER COUNT (including spaces), WORD COUNT.

What does anyone think? Can this be done? :whistle:

Edited by Diana (Cda)
Link to comment
Share on other sites

...........If this could be made to work, I'd like to compile it then share it. If it can be done, the ideal would be to display LETTER COUNT (without spaces), LETTER COUNT (including spaces), WORD COUNT.

What does anyone think? Can this be done? :whistle:

You can use the Windows Clipboard or an InputBox with the code below:

;$Test = "this is a counter"

;$Test = ClipGet()

$Test = InputBox("AutoIt", "Type prompt here")

$wo_space = StringSplit(StringStripWS($Test, 8), "")
$w_space = StringSplit($Test, "")
$word = StringSplit($Test, " ")

MsgBox(0, "answer", _
        "Letter Count without spaces: " & $wo_space[0] & @CR & _
        "Letter Count with spaces: " & $w_space[0] & @CR & _
        "Word Count: " & $word[0])

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Wow, this is incredible. Such an unbelievably small amount of code does exactly what's needed. AutoIt is super amazing!

I've tweaked the descriptive text parts to make them more specific to the job so that the script now looks like this:

$Test = "this is a counter"

;$Test = ClipGet()

$Test = InputBox("WORD AND LETTER COUNTER", "Paste the text selection into the rectangular box below ...")

$wo_space = StringSplit(StringStripWS($Test, 8), "")
$w_space = StringSplit($Test, "")
$word = StringSplit($Test, " ")

MsgBox(0, "Text stats ...", _
       "Number of characters (with spaces) = " & $w_space[0] & @CR & _
       "  Number of characters (no spaces) = " & $wo_space[0] & @CR & _
       "                        Word Count = " & $word[0])
I feel that this is nearly perfect. There are some minor things missing to make it complete. I've not mastered boxes very well yet. The input and msgboxes I've made often have an extra button step in them no matter what I do, or they don't work properly, so I've not been able to modify the behaviour of buttons very well though I tried again here with these. I'm afraid to say I didn't manage it here. Can we have the buttons do this:

1) OK button - loops back to the beginning instead of quitting, so that we can enter text over and over, as needed, until we press CANCEL?

2) CANCEL button - that the cancel bypasses the text stat box with "0" values and that it instead quits the process completely?

Also, in AI, is there a way to always have the 2 buttons present? i.e., whenever there's an OK, there's a CANCEL ... (?)

I know all this can be done, but I don't have enough example working code for different types of boxes so I know I'm still missing some aspects of understanding.

***********************************

btw, just out of curiousity, though this isn't so critical, is there a way to get the text in the display window to display like above, with the results all lined up under the equal sign "="? They line up in the script but they don't in the display window. It would be easier on the eyes if the equal signs lined up <g>.

You guys are the best, thank you. :whistle:

Link to comment
Share on other sites

If it's just a simple letter count then you could also use

$Txt = "This is a sample line"  ;; Or $Txt = ClipGet()

$A = StringLen(StringStripWS($Txt, 8))

Remember this is only going to give a character count but if you are stripping spaces then all you have left is characters anyway.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

...They line up in the script but they don't in the display window. It would be easier on the eyes if the equal signs lined up <g>.

Different fonts maybe? Each treating a space as a different width - I really don't know why.

Anyway, here is how I would build the tool that you are working on. It seemed like you were asking the user of your script to paste in the info to be "counted". If that is the case, then the script below might better serve your needs. It will continuously "count" whatever is in the Windows clipboard and continuously display the results. Whenever you change the contents of the Windows clipboard, the script changes the display of the results.

HotKeySet("{ESC}", "Terminate")

While 1
    $Test = ClipGet()
    $wo_space = StringSplit(StringStripWS($Test, 8), "")
    $w_space = StringSplit($Test, "")
    $word = StringSplit($Test, " ")

    TrayTip("Text stats for: ", _
            '"' & $Test & '"' & @CR & @CR & _
            "Number of characters (with spaces) = " & $w_space[0] & @CR & _
            "  Number of characters (no spaces) = " & $wo_space[0] & @CR & _
            "                                     Word Count = " & $word[0] & @CR & @CR & _
            "Press ESC to exit the script.", 10)
    Sleep(200)
WEnd

Func Terminate()
    Exit
EndFunc   ;==>Terminate
If you need the ESC key for something else, you can change the HotKey that exits the script to any other key of combination of keys that are allowed - see the helpfile under HotKeySet.

The drawback to using TrayTip is that it is for Windows 2000/XP (and probably Vista) only. Let us know if that does not work for your needs.

-MSP-

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Glad I came back and read the rest of your message under the code box. Yes, on our recent WinXP upgrade, this doesn't work. But I'm not one to be idle. I asked a couple of questions elsewhere and look at what we got:

********************************************************************************

$Test = "this is a counter"

;$Test = ClipGet()

$Test = InputBox("WORD AND LETTER COUNTER", "Paste the text selection

into the rectangular box below ...")

$wo_space = StringSplit(StringStripWS($Test, 8), "")

$w_space = StringSplit($Test, "")

$word = StringSplit($Test, " ")

MsgBox(0, "Text stats ...", _

"Number of characters (with spaces) = " & $w_space[0] & @CR & _

" Number of characters (no spaces) = " & $wo_space[0] & @CR &

_

" Word Count = " & $word[0])

********************************************************************************

This works and is exactly what I was needing. I just don't always know the best way to describe things from a software point of view, I think.

About the only thing I'd add here is to perhaps have an extra button to send the results to the clipboard in case that's ever needed (that would send the stats themselves from the 3 "number of characters ..." lines). But the above stands perfectly as is. Good for writers or for those, like me, who need to target specific sizes of phrases for word puzzles. This is very good.

I compiled it with a nice custom icon and it comes to a very small 180kb which is perfect for my small USB flash drive (I only have 250megs so it's tough to keep contents down <g>).

Thanks to the group. Where would I be without AI, I don't know. Wish I was better at coding in it, though. :whistle:

Link to comment
Share on other sites

...Yes, on our recent WinXP upgrade, this doesn't work....

It would be helpful to us to know what you mean by "this doesn't work". Are you saying that the TrayTip does not appear above the AutoIt icon in the system tray? If so, then this makes the 3rd XP system in recent days to have this issue.

Please let us know...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

It would be helpful to us to know what you mean by "this doesn't work". Are you saying that the TrayTip does not appear above the AutoIt icon in the system tray? If so, then this makes the 3rd XP system in recent days to have this issue.

Please let us know...

With XP the tray tip will often be hidden behind the task bar. It's an XP bug that MS has no intention of ever fixing.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

It would be helpful to us to know what you mean by "this doesn't work". Are you saying that the TrayTip does not appear above the AutoIt icon in the system tray? If so, then this makes the 3rd XP system in recent days to have this issue.

Please let us know...

Sure, sorry. I should have been more specific, shouldn't I? I didn't see any change in the screen, no icons or anything, anywhere. But upon seeing that cool message when I first logged back in just now, realized that I'd probably hidden the AI icon. The icon does show indeed come up in the systray, but apparently nothing further is set in motion that I can see. I don't know if a popup box is supposed to appear or what exactly is supposed to happen, but when I save to the clipboard, nada.

I'm not surprised re XP. Through working over last couple of years on others' XP boxes and my own new computer w/XP on it since late November, the difficulties with it are many. I never had half the troubles with my Win98SE box.

However, in my case, the other script does the letter count perfectly. I'll have to give it a try at home to see if it still works there <g>. I know, I know, it _should_ but I'm very wary these days. <g>

Thank you! :oD

Link to comment
Share on other sites

It should look like:

..and all you had to do was highlight new text and press control-c or right-click and copy --- however you liked to get the text into the Windows clipboard.

Anyone in the forum know why TrayTips are not always showing in XP?

See this thread also: http://www.autoitscript.com/forum/index.ph...st&p=321020

Is there a [XP] setting that I'm missing?

Edit: added [XP]

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

I am such a dork, I'm sorry <sigh>. I'd make a piece-meal developer because things don't occur to me until I'm actually trying out code, sometimes. The script is just beautiful. Unbelievable what AI can do with just a few words. But I was wondering if there's a way to add one last thing, a "copy to clipboard" type of button (?). Honestly, I spent over an hour between yesterday and today going through the archives again for script to do this and found lots of great information, but I don't know how to adapt the bits of clipboard-related code I found to this particular script.

What would be really perfect is to have the same functionality with this script here below ...

while 1

$Test = InputBox("WORD AND LETTER COUNTER",  _
    "Paste the text selection into the rectangular box below ...")

if @error > 0 or $Test == "" Then
        ExitLoop
EndIf

$wo_space = StringSplit(StringStripWS($Test, 8), "")
$w_space = StringSplit($Test, "")
$word = StringSplit($Test, " ")

$ret = MsgBox(4161, "Text stats ...",  _
"Number of characters (with spaces) = " & $w_space[0] & @CR _
& "  Number of characters (no spaces) = " & $wo_space[0] & @CR _
& "                        Word Count = " & $word[0])

if $ret <> 1 Then
        ExitLoop
EndIf
WEndoÝ÷ Ù»­i×bªºB¢­ )b¥º­Ú®¢Öî¶Ú'²ajÚ.¶­q©Ûzǧ¶Ú-ç%èj·_¢YhÂ)ඬ¶»¶êÞméhÁ8^¥ËhëZ­ê^jÚ¶­{l¶¸§¬µªíyÚ.·­©ÝÛ,µ«ljwlzwl¶­wè¶ayÉb¥º­Òyéb·
.ÖÞ{kzg¥Êë~é_¢»azf§ÉƬzÌ!z·°{kyÊ&¦)b¢jZ®+(í{l¶¸§Ì!zyh¢H§ú+²¶§ØZµø­²«ç°Z*Úµè­Â¥u·½©ni¹^i×hÚ-ë®*mjëh×6<repeat of text string here>

<text stats>
What does anyone think on best way to add this ... ?

Thanks much. :whistle:

Link to comment
Share on other sites

I cannot make a button for that unless I make a GUI and I don't do GUIs yet. Perhaps someone else will make it for you. The code below always puts info into the clipboard - see if it will do what you wanted.

While 1

    $Test = InputBox("WORD AND LETTER COUNTER", _
            "Paste the text selection into the rectangular box below ...")

    If @error > 0 Or $Test == "" Then
        ExitLoop
    EndIf

    $wo_space = StringSplit(StringStripWS($Test, 8), "")
    $w_space = StringSplit($Test, "")
    $word = StringSplit($Test, " ")
    
    ClipPut('"' & $Test & '"' & @CRLF & @CRLF & _
            "Number of characters (with spaces) = " & $w_space[0] & @CRLF & _
            "  Number of characters (no spaces) = " & $wo_space[0] & @CRLF & _
            "                        Word Count = " & $word[0])

    $ret = MsgBox(4161, "Text stats for: ", ClipGet())

    If $ret <> 1 Then
        ExitLoop
    EndIf
WEnd

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Just responding to an earlier request in this thread for a way to line up the =

Using @TAB can help with positioning

local $w_space[2], $wo_space[2], $word[2]
MsgBox(0,"","Number of characters (with spaces)" & @TAB & "= " & $w_space[0] & @CRLF & _
            "Number of characters (no spaces)" & @TAB & "= " & $wo_space[0] & @CRLF & _
            "Word Count" & @TAB & @TAB & @TAB & "= " & $word[0])
Link to comment
Share on other sites

I cannot make a button for that unless I make a GUI and I don't do GUIs yet. Perhaps someone else will make it for you. The code below always puts info into the clipboard - see if it will do what you wanted.

Pretty kewl. This just repeats the text string above the text stats in the display area itself; still isn't text that can be retrieved and saved somewhere, but it's pretty kewl! <g> Thanks. Much appreciated.
Link to comment
Share on other sites

Just responding to an earlier request in this thread for a way to line up the =

Using @TAB can help with positioning

local $w_space[2], $wo_space[2], $word[2]
MsgBox(0,"","Number of characters (with spaces)" & @TAB & "= " & $w_space[0] & @CRLF & _
            "Number of characters (no spaces)" & @TAB & "= " & $wo_space[0] & @CRLF & _
            "Word Count" & @TAB & @TAB & @TAB & "= " & $word[0])
Silly. I just hadn't put enough spaces in the text and I don't know why. My eyes must have been tired because I couldn't see any change in the position of "Word Count" before. But I kept adding and trying and adding and trying today and eventually everything showed up nicely right-justified. That makes seeing the stats much easier on the eyes.

That's what happens around fiscal year end. This year it's a little tougher as I'm trying to keep up with my own needs along with work. But this little gem will help me put out my free word puzzles because I'm currently working on a particular puzzle type where I need to target even-numbered strings divisible by 4 and this little utility will most definitely help me to focus on the puzzles themselves rather than in the work on making them. On a side note, I had wanted to get my webpage up and running by today, my birthday, but it hasn't happened yet as I'm having trouble re some issues but at least this little utility has eliminated one big challenge!

Thanks! :whistle:

Edited by Diana (Cda)
Link to comment
Share on other sites

Had to try it with the display text string part. This seems to work, too.

While 1

    $Test = InputBox("WORD AND LETTER COUNTER", _
            "Paste the text selection into the rectangular box below ...")

    If @error > 0 Or $Test == "" Then
        ExitLoop
    EndIf

    $wo_space = StringSplit(StringStripWS($Test, 8), "")
    $w_space = StringSplit($Test, "")
    $word = StringSplit($Test, " ")
    
    ClipPut('"' & $Test & '"' & @CRLF & @CRLF & _
            "   Number of characters (no spaces) = " & $wo_space[0] & @CRLF & _
            "Number of characters (with spaces) = " & $w_space[0] & @CRLF & _
            "                                      Word Count = " & $word[0])

    $ret = MsgBox(4161, "Text stats for: ", ClipGet())

    If $ret <> 1 Then
        ExitLoop
    EndIf
WEnd
I'll compile this and take to work and actually get down to starting process of trying to create a puzzle during my lunch hour.

Thanks for everyone's valuable help! :whistle:

Link to comment
Share on other sites

...still isn't text that can be retrieved and saved somewhere...

It can be retrieved - it is written directly to the Windows clipboard. You do not have to copy and paste the info from any display or msgbox - it is already in the clipboard - just paste it where you want it.

Sorry that I did not make that clear...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...