Jump to content

FileReadLine alternative for open/"live" documents?


Recommended Posts

Hi,

I am trying to do some line-by-line functions on documents, but the catch is that I need to use the current state of the active document. In other words; I'm hoping to find a way to go through each line of an open Dreamweaver or Notepad document without having to save it and use FileOpen. I'm thinking that some sort of temp file might do the trick but I'm not quite sure how to go about it. Can anybody point me in the right direction?

Thanks.

Link to comment
Share on other sites

Hi,

I am trying to do some line-by-line functions on documents, but the catch is that I need to use the current state of the active document. In other words; I'm hoping to find a way to go through each line of an open Dreamweaver or Notepad document without having to save it and use FileOpen. I'm thinking that some sort of temp file might do the trick but I'm not quite sure how to go about it. Can anybody point me in the right direction?

Thanks.

FileCopy?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I'm thinking that some sort of temp file might do the trick but I'm not quite sure how to go about it. Can anybody point me in the right direction?

Look at using _Tempfile (Standard UDF) to return a temporary filename to use for writing each line using FileOpen and FileReadLine or _FileReadToArray (Standard UDF). Once done FileWriting the temporary file, then you can FileMove the temporary file to overwrite the original.

:whistle:

Link to comment
Share on other sites

Wow, you guys are fast!

Thanks a ton for the help. Sorry, but I'm still a little confused.

OK, so when you say to write to a temp; where does the data actually come from to write that file? If I use FileOpen or FileCopy then it might not be accurate because the file may not have been saved recently. What I'm hoping to find is a way to copy the current text directly from the active file into some sort of an array that would allow me to step through it line by line just as I would reading from a file.

Here is a real-world example of what I'm trying to do (if it helps):

Let's say I'm writing a php file in Dreamweaver and would like to stop and display a list of the doc's current variables. I don't want to lose my place on the page, saving is not a good option at the moment, and if the bus drops below 70mph we're all dead.

**Keanu Reeves voice: "Whadaya dooo? Whadaya doooo?" :whistle:

I could probably get away with ^a ^c on the active file and then use the clipboard to write a temp file, but wouldn't that require me to select all/copy/create temp/paste/save as temp/and then re-open in order to use fileReadLine? I may be way off on that one, but I don't think it would work anyway because sometimes the current caret position is a factor and the ^a would move it. Maybe I could use ControlGetText?

Thanks again, and sorry if I'm not making much sense.

Edited by Dolemite50
Link to comment
Share on other sites

...I could probably get away with ^a ^c on the active file...

Try this sample code:
;setup example app
Run("notepad")

WinWait("Untitled - Notepad")
WinActivate("Untitled - Notepad")
WinWaitActive("Untitled - Notepad")

; put some sample text in
ControlSetText("Untitled - Notepad", "", "Edit1", _
        "This is some info on line1." & @CRLF & _
        "This is some info on line2." & @CRLF & _
        "This is some info on line3." & @CRLF & _
        "This is some info on line4.")

;capture sample text like ^a ^c
$All_Text = ControlGetText("Untitled - Notepad", "", "Edit1")

;split text into an array
$All_Text_Array = StringSplit($All_Text, @CRLF, 1)

;cycle thru the array
For $i = 1 To $All_Text_Array[0]
    MsgBox(0, "", $All_Text_Array[$i])
    ;do StringInStr to look for info
Next

WinClose("Untitled - Notepad")

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

Link to comment
Share on other sites

Hey plato,

Thanks alot! I had no idea that there were CRLF's hiding out in notepad like that, those sneaky mo-fos. The example you posted is exactly what I'd hoped to find and seems to be working perfect so far.

Just wondering, would:

$All_Text = ControlGetText()

produce the exact same result as saving the file and using:

_FileReadToArray(myFile, $All_Text)?

Thanks again for the help, I really appreciate it.

Edited by Dolemite50
Link to comment
Share on other sites

...Just wondering, would:

$All_Text = ControlGetText()

produce the exact same result as saving the file and using:

_FileReadToArray(myFile, $All_Text)? ...

You are welcome.

The ControlGetText line is similar to saving the info to a file.

The StringSplit line is what puts the info into an array...

...so, the StringSplit is the closest parallel to the _FileReadToArray UDF.

If you look inside of the _FileReadToArray UDF you will find that the main thing it does is a StringSplit.

Most Windows app use Carriage Returns and Line Feeds as a pair on the end of each line. The app that you use may not. You can look at how the _FileReadToArray UDF uses StringStripCR to handle either case. If a CR is present, then StringStripCR takes it away. If no CRs are present, then StringStripCR does nothing. Then the StringSplit can the use the remaining LFs for the split.

...enjoy AutoIt...

Edited by herewasplato

[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...