Jump to content
Sign in to follow this  
9600bauds

Copying multiple lines until pagebreak?

Recommended Posts

9600bauds

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 by 9600bauds

Share this post


Link to post
Share on other sites
kaotkbliss

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 game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
9600bauds

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)

Share this post


Link to post
Share on other sites
Melba23

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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
9600bauds

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 by 9600bauds

Share this post


Link to post
Share on other sites
kaotkbliss

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 game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Share this post


Link to post
Share on other sites
9600bauds

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!

Share this post


Link to post
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
Sign in to follow this  

×