AutoHotkey Community

It is currently May 26th, 2012, 12:24 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: September 19th, 2008, 12:41 pm 
Offline

Joined: April 17th, 2007, 7:36 pm
Posts: 22
I have a small AHK script now that removes selected text from one input field, replaces it with ___________ and pastes it in a second input field. Here it is...
Code:
F1:: ; Trigger button.
Send ^x
Send ______
Send {Tab}
Send ^v
Send {Enter}
return ; Exits trigger condition actions


As it's written, all text that is cut gets replaced with a single _________ no matter how many words are cut. I'd like to determine how many words are in the text that is cut (by counting spaces with regex I suppose) and insert the same number of blanks.

In other words...
Quote:
The quick brown fox jumped over the lazy dog.


would be...
Quote:
The ________ ________ _________ jumped over the lazy dog.


instead of...
Quote:
The ________ jumped over the lazy dog.


when I cut out...
Quote:
quick brown fox


I don't know how to write regular expressions and don't have time to learn (I'm a medical student who is using AHK along with Ankito make flashcards for studying during lecture. Adding this feature to my existing script would make things even more efficient.

Thanks in advance,
Ryan


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 1:16 pm 
Offline

Joined: November 8th, 2004, 12:46 am
Posts: 1271
You could do it with StringReplace. ErrorLevel stores the number of replaced items:

Code:
F1:: ; Trigger button.
Send ^x
ClipWait
StringReplace, clipboard, clipboard, %A_Space%,, All
words := Errorlevel, str := "______ "
Loop % words
   Send % str
Send {Tab}
Send ^v
Send {Enter}
return ; Exits trigger condition actions

_________________
"Anything worth doing is worth doing slowly." - Mae West
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 2:05 pm 
Offline

Joined: April 17th, 2007, 7:36 pm
Posts: 22
Serenity,
Thanks for the help, but it doesn't quite work.

It produces the following in inputOne
Quote:
The jumped over the lazy dog.


and this in inputTwo
Quote:
Thequickbrownfoxjumpedoverthelazydog.


It *should* produce this in inputOne
Quote:
The _______ ______ ______ jumped over the lazy dog.


and this in inputTwo
Quote:
quick brown fox

_________________
---
I used AutoHotKey to write this signature crazy fast.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 19th, 2008, 6:58 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Tested and it works
Code:
F1:: ; Trigger button.
Clipboard=
Send ^x
ClipWait, 1
StringReplace, clipboard, clipboard, %A_Space%,%A_Space%, UseErrorLevel ; don't remove the space put it back
Words:=ErrorLevel + 1 ; make sure to add one. Two words selected -> one space, e.g. loops needs to be done twice
Loop %Words%
   Send ______{space}
Send {tab}
Send ^v
return ; Exits trigger condition actions

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Thanks!
PostPosted: September 19th, 2008, 7:13 pm 
Offline

Joined: April 17th, 2007, 7:36 pm
Posts: 22
Awesome HugoV. It works like a charm. Thanks so much.

_________________
---
I used AutoHotKey to write this signature crazy fast.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Morpheus, RUBn, SKAN, sks, Yahoo [Bot] and 20 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group