9600bauds Posted March 16, 2013 Share Posted March 16, 2013 (edited) Hello everyone, I've been making a very basic AutoIt script to read a .txt file and paste it, page by page, into a certain Java application. The files contain simple pages of text, several lines long. I made the script scan every single text line: If it contains only the characters "@np", which I use as a 'line break', it will turn the page, and if not, it will copy and then paste the line. The code below has worked for me so far, but since the script pastes the text line by line, it's somewhat slow for long texts... I'm sure there must be a way to paste per whole page, but I haven't been able to figure it out. Any help? #include <File.au3> $file = FileOpen("C:\Myfile.txt", 0) Sleep (3000) ; Gives me three seconds to switch to the app before starting. Should probably use a hotkey, but it works well enough for me! While 1 $line = FileReadLine($file) If @error Then ExitLoop If $line=='@np' Then ; "@np" means a pagebreak, the script will click the "next page" button and continue. MouseClick('left') ; Since "next page" button can vary in placement, I simply place my cursor on it when running the script. Else ClipPut($line) Send("^v{ENTER}") ; If the line is not "@np", it will be pasted, line by line. EndIf Sleep (50) ; Although the script itself works fine without this sleep time, the app starts acting up if I don't wait 50 miliseconds between paste... WEnd FileClose($file) Edit: Here is an example of a file that I use with the program, to clear up any confusion: I do not like green eggs and ham. @np I do not like them, Sam-I-am. Edited March 16, 2013 by 9600bauds Link to comment Share on other sites More sharing options...
kaotkbliss Posted March 16, 2013 Share Posted March 16, 2013 Just a quick idea You could possibly do a FileRead() to get all the text into a single variable, then StringSplit() the variable using @np to give you an array of all pages. Then all you need to do is loop through the array, pasting the pages. 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
9600bauds Posted March 16, 2013 Author Share Posted March 16, 2013 Thanks! I managed to make some progress using StringSplit(), but sadly it doesn't work entirely right with longer files... Splitting seems to really not like new lines, so at the start of nearly every page there is a completely blank line, and sometimes a page will be completely empty while the next one will contain two pages. Here's what I've got so far. #include <File.au3> $file = FileOpen("C:\Test.txt", 0) Sleep (3000) $txt = FileRead($file) $pageArray = StringSplit($txt, '@np', 1) $totalPages = UBound($pageArray) For $i = 1 to $totalPages-1 ClipPut($pageArray[$i]) Send("^v{ENTER}") MouseClick('left') Sleep(50) Next FileClose($file) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 16, 2013 Moderators Share Posted March 16, 2013 9600bauds,To eliminate the blank line at the start of each page, you need to include the line break in the StringSplit parameter - I am assuming you use @CRLF here:$pageArray = StringSplit($txt, '@np' & @CRLF, 1)You might also note that StringSplit returns the number of split elements in the [0] element so there is no need to use UBound to size the loop. M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
9600bauds Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) Thanks a lot, everyone! I finally managed to get the script fully working!It was being a little unconsistent, skipping some pages sometimes, but after a while of tinkering I realized I also had to also include the line break before my StringSplit parameter, otherwise all pages would have an extra linebreak at the end which didn't work well with the app's maximum character limit.Again, thanks to you all! Here's my finished code.#include <File.au3> $file = FileOpen("C:\Myfile.txt", 0) Sleep (3000) $txt = FileRead($file) $pageArray = StringSplit($txt, @CRLF & '@np' & @CRLF, 1) For $i = 1 to $pageArray[0] ClipPut($pageArray[$i]) Send("^v") Sleep(50) MouseClick('left') Next FileClose($file) Edited March 16, 2013 by 9600bauds Link to comment Share on other sites More sharing options...
kaotkbliss Posted March 16, 2013 Share Posted March 16, 2013 interesting that you used ClipPut($pageArray[$i]) then Send("^v") as you might be able to just simply Send($pageArray[$i]) 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
9600bauds Posted March 17, 2013 Author Share Posted March 17, 2013 Ah! I used ClipPut because Send() tends to be a much less instantaneous solution. By using faux copy-and-paste, each page is filled in as fast as possible, rather than "typed in" Again, thanks for helping! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now