Jump to content

Quickly copying the page text and save it as a TXT file to specified folder


gear
 Share

Recommended Posts

This program is for copying the text and save it as a TXT file.

You also can delete the last created TXT file. The instruction

state the operation clearly.

Always welcome for comment, suggestion and correction.

Thanks.

Gear

;; This program is for copying the text and save it as a TXT file.
;;there are some webpages that can be copied by Ctrl+LeftMouse clikc to get the
;;selected window text, so just use Ctrl+left mouse click this program copy the text to the specified folder
;;in order to copy the file text, you should first press ctrl and click left mouse button, then release the Ctrl key,
;; the program will quickly copy the whole page file txt to the specified folder as TXT file.
;; if you just want to copy the selected window txt, please delete Send("^a"), the nthe program
;; will just copy the txxt inside the window you select with the Ctrl and left mouse button.
;; sometimes you may copy the wrong text, or due to other reason, you donot want to copy this page text to your folder, you
;;want to delete the last created TXT file, you can press left mouse button and sgift to do so.
;; but you only can delete the last one, you cannot comtinuely delete more files. 
;;you can know if the file is copied to your folder or fail to copy the text or if the last is deleted successfully by listening to the Beep sound
#include <Misc.au3>
#Include <File.au3>
#Include <Array.au3>
$MsgID=7;;control if you can creat a folder to save your files
While $MsgID==7
$ChildDir=InputBox("New Folder","Input The Name of New Folder that you want the new TXT files will be located in:")
$FileLocation=$ChildDir&"\";;the folder you try to put the TXT files in
If FileExists($FileLocation) Then
    $MsgBoxTextContent1="The folder you try to creat has been existing,do you still want to copy the text files to this folder?"
    $MsgBoxTextContent2="If you Press OK button,Some files with the same name will be overwritten."
    $MsgID=MsgBox(3,"Folder is existing!",$MsgBoxTextContent1&@CR&$MsgBoxTextContent2)
    If $MsgID==6 Then MsgBox(1,"Continue","Attention, some files in the folder may be deleted."&@CR&"This message box will be closed after 5 seconds.",5)
    If $MsgID==7 Then MsgBox(1,"Creat Another Folder","I will try to creat another folder to save my files")
    If $MsgID==2 Then Exit
Else
        $MsgID=0
EndIf
WEnd
$i=0;how many files you save to folder the default first TXT file created is 0, you change it following
;;Try to avoid rewrite the old files within the folder, you can reenter a new file nmuber as the beginnig TXT file name
$FileList=_FileListToArray($FileLocation,"*.txt",1);find all the TXT files within this folder, if the folder does not exist, it will return 0 but not error message
If UBound($FileList)>0 Then;it means the folder exists and there is(are) some TXT files already within this folder
$LastCreatedFile=$FileList[UBound($FileList)-1];;all files names are put into an array
$UseTheNewBeginingNumberAsFileName="1. If you would like to use a new number as the file name, please press OK Button."
$IWantToViewTheExistingFileList="2. If you would like to view the existing files list, please press NO Button."
$IDonotCare="3. Go on, I donot care! Please press Cancel button."
$ReEnterFileNumber=6; control if you would like to enter a new number as the file name
While $ReEnterFileNumber==6 Or $ReEnterFileNumber==7
$ReEnterFileNumber=MsgBox(3,"Avoid Rewriting the old files",$UseTheNewBeginingNumberAsFileName&@CR&$IWantToViewTheExistingFileList&@CR&$IDonotCare)
If $ReEnterFileNumber==6 Then
    $NewFileNameNum=InputBox("Please enter a new file number as the first-created TXT file name","The number can be like:0,1,2,3,.....500,if better, donot be bigger than 10000")
    $i=Number($NewFileNameNum)
    $ReEnterFileNumber=0
EndIf
If $ReEnterFileNumber==7 Then _ArrayDisplay($FileList,"$FileList"); list all the existing files so the user can know the detailed info about the existing TXT files
If $ReEnterFileNumber==2 Then ExitLoop; exit this program
WEnd
EndIf
;; At present all the preparation work for avoiding rewriting old files has been finished
$TryToCopy=0;detect if you want to copy text
While 1
    If 1000<$i Then $FileName=$FileLocation&String($i)&".txt"
    If 100<$i<=1000 Then $FileName=$FileLocation&"0"&String($i)&".txt";; it can list all files accoeding to their name order
    If 10<$i<=100 Then $FileName=$FileLocation&"00"&String($i)&".txt"
    If 0<=$i<10 Then $FileName=$FileLocation&"000"&String($i)&".txt"
;; if you want to delete the last created TXT file, you can Press shift key and left mouse button
;;Sometimes the program may fail to react to this operation, so only when the messagebox popup means the file is delted successfully.
;;in order to increase the possibility, you can first press left mouse button then press the shift key.
if (_IsPressed(10) And _IsPressed(01)) Then
    $IfSuccess=FileDelete($TheLastCreatedTXTFile);;only the last file can be deleted
    If $IfSuccess==1 Then 
        MsgBox(1,"File Deleted","OK! The last created TXT file has been deleted successfully!")
        For $FileDelete=1 To 5
        Beep(50, 100)
        Sleep(50)
        Next
        $i=$i-1
    EndIf
EndIf
While (_IsPressed(10) And _IsPressed(01)); wait the user to release the shift key and left mouse button, otherwise program stop here to wait
Sleep(50);; without this section, the program will try to delete the last created TXT file again and again
WEnd;; we donot hope such occasion happens. but this section is not always neccessary, since the last created file has been deleted no useful operation will happen 
if _IsPressed(11) And _IsPressed(01) Then
    $TryToCopy=1; it means you try to copy some text
    $begin = TimerInit();control the time interval you press the key
    $TooLong=0
    While _IsPressed(11);onlt when you release the Ctrl key the program begin to try to copy the text
        Sleep(50)
        $dif = TimerDiff($begin)
        If $dif>=1500 Then 
            Beep(500, 250);; it means you press the Ctrl key too long. when you try to abandon this time's text copy, you can use this manner
            $TooLong=1; it means you press the Ctrl key too long, the copy text operarion fails
            ExitLoop; it means you press the Ctrl key too long and program begin another time's text-copy
        EndIf
    WEnd
While _IsPressed(11);;it waits you release the Ctrl key so that another time text-copy can begin
    Sleep(50);; without this section, your long-time pressing Ctrl key will fail to abandon this-time's text-copy operation
WEnd
EndIf
If $TryToCopy==1 And $TooLong==0 Then;your try to copy text and your press the key with the right time-interval
    Send("^a");select all the text
;;Sleep(500)
    Send("^c");copy the selected text to clipboard
    Sleep(200)
    $TextFromExplorer=ClipGet()
    $file=FileOpen($FileName,10);write the text to file, if it doesnot exist, creats it.
    FileWrite($file, $TextFromExplorer)
    $IfTheFileGood=FileClose($file)
    If $IfTheFileGood==1 Then 
        Beep(100, 150);;it means the success of copying the text to TXT file
        $TheLastCreatedTXTFile=$FileName;record the last created TXT file so that you can press Shift key and left mouse button to delete it
        $i=$i+1
    Else
    Beep(2000, 2000);;it means fail to copy the text to file    
    EndIf
EndIf
$TryToCopy=0;begins another around's text-copy preparation
WEnd
Link to comment
Share on other sites

Today I improve the program yesterday I posted, now it is much better.

you append the text to the previous file if neccessary.

Always welcome for comment and suggestion, correction.

Gear

;; This program is for copying the text and save it as a TXT file.
;;1. Ctr+Alt+Left Mouse button creats new file;
;;2. Ctrl+left mouse button append the selected text to the last created file
;;3. Shift+left mouse buuton delete the last created file or delete the section appended to the last file
;;4. at first both 1 and 2 can creat new file, once a new file has been created, only 1 can created new file
;;5. only the last created file or last section appended can be deleted
;;there are some webpages that can be copied by Ctrl+LeftMouse clikc to get the
;;selected window text, so just use Ctrl+left mouse click this program copy the text to the specified folder
;;in order to copy the file text, you should first press ctrl and click left mouse button, then release the Ctrl key,
;; the program will quickly copy the whole page file txt to the specified folder as TXT file.
;; if you just want to copy the selected window txt, please delete Send("^a"), the nthe program
;; will just copy the text inside the window you select with the Ctrl and left mouse button.
;; sometimes you may copy the wrong text, or due to other reason, you donot want to copy this page text to your folder, you
;;want to delete the last created TXT file, you can press left mouse button and sgift to do so.
;; but you only can delete the last one, you cannot comtinuely delete more files.
;;you can know if the file is copied to your folder or fail to copy the text or if the last is deleted successfully by listening to the Beep sound
Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage")  ;Shift-Alt-d
#include <Misc.au3>
#Include <File.au3>
#Include <Array.au3>
;select or creat a new folder to save the copied files in.
$TheFolderToSaveFiles = ""
While $TheFolderToSaveFiles == ""
    $TheFolderToSaveFiles = FileSelectFolder("Please Choose or creat a folder to save your copy files:", "", 1)
    If @error == 1 Then Exit
WEnd
$FileLocation = $TheFolderToSaveFiles & "\"
$i = 0;how many files you save to folder the default first TXT file created is 0, you change it following
;;Try to avoid rewrite the old files within the folder, you can reenter a new file nmuber as the beginnig TXT file name
$FileList = _FileListToArray($FileLocation, "*.txt", 1);find all the TXT files within this folder, if the folder does not exist, it will return 0 but not error message
If UBound($FileList) > 0 Then;it means the folder exists and there is(are) some TXT files already within this folder
    $LastCreatedFile = $FileList[UBound($FileList) - 1];;all files names are put into an array
    $UseTheNewBeginingNumberAsFileName = "1. If you would like to use a new number as the file name, please press OK Button."
    $IWantToViewTheExistingFileList = "2. If you would like to view the existing files list, please press NO Button."
    $IDonotCare = "3. Go on, I donot care! Please press Cancel button."
    $ReEnterFileNumber = 6; control if you would like to enter a new number as the file name
    While $ReEnterFileNumber == 6 Or $ReEnterFileNumber == 7
        $ReEnterFileNumber = MsgBox(3, "Avoid Rewriting the old files", $UseTheNewBeginingNumberAsFileName & @CR & $IWantToViewTheExistingFileList & @CR & $IDonotCare)
        If $ReEnterFileNumber == 6 Then
            $NewFileNameNum = InputBox("Please enter a new file number as the first-created TXT file name", "The number can be like:0,1,2,3,.....500,if better, donot be bigger than 10000")
            $i = Number($NewFileNameNum)
            $ReEnterFileNumber = 0
        EndIf
        If $ReEnterFileNumber == 7 Then _ArrayDisplay($FileList, "$FileList"); list all the existing files so the user can know the detailed info about the existing TXT files
        If $ReEnterFileNumber == 2 Then ExitLoop; exit this program
    WEnd
EndIf
;; At present all the preparation work for avoiding rewriting old files has been finished
$TheFirstCreatedFileNameNumber = $i
Global $FileName = TheFullPathFileName()
$TheLastCreatedTXTFile = $FileName;; The user may use try to delete the file before he creats any new file here avoids the error of delete the unexisting filename variable
$CreatOrAppend = "No";; before creat any new file, no permit to try to delete and file or section
$BeforeAppendFileSize = -100; avoid try to delete the appending section
While 1
    ;; if you want to delete the last created TXT file, you can Press shift key and left mouse button
    ;;Sometimes the program may fail to react to this operation, so only when the messagebox popup means the file is delted successfully.
    ;;in order to increase the possibility, you can first press left mouse button then press the shift key.
    If (_IsPressed(10) And _IsPressed(01) And $CreatOrAppend == "Creat") Then
        DeleteLastCreatedFile();delete the last created file
    EndIf
    If (_IsPressed(10) And _IsPressed(01) And $CreatOrAppend == "Append" And $BeforeAppendFileSize > 0) Then
        DeleteLastAppendedSectionFromTheFile();delete the last created file
    EndIf
    If (_IsPressed(11) And _IsPressed(01) And _IsPressed(12)) Then
        CopyAndCreatNewFile();the last element is the file name of the just created file name
    EndIf
    ;; Try to use Ctrl+Left mouse key to copy some text and append to its previous file
    If (_IsPressed(11) And _IsPressed(01) And (Not _IsPressed(12))) Then
        CopyAndAppendToFile();;the last element is bytes foe the file before appending
    EndIf
WEnd
;;copy the text and creat a new file to save the text,  the return array's first element tells if the operation is OK.
Func CopyAndCreatNewFile()
    ;;$TryToCopy = 1; it means you try to copy some text and creat a new file to save it
    $begin = TimerInit();control the time interval you press the key
    $TooLong = 0
    While (_IsPressed(11) Or _IsPressed(12));only when you release the Ctrl and Alt key the program begins to try to copy the text
        Sleep(50)
        $dif = TimerDiff($begin)
        If $dif >= 1500 Then
            Beep(500, 250);; it means you press the Ctrl key too long. when you try to abandon this time's text copy, you can use this manner
            $TooLong = 1; it means you press the Ctrl+Alt key too long, the copy text operarion fails
            ExitLoop; it means you press the Ctrl key too long and program begin another time's text-copy
        EndIf
    WEnd
    While (_IsPressed(11) Or _IsPressed(12));;it waits you release both the Ctrl and Alt keyes so that another time text-copy can begin Sleep(50);; without this section, your long-time pressing Ctrl+Alt key will fail to abandon this-time's text-copy operation
    WEnd
    If $TooLong == 0 Then
        Send("^a");select all the text
        Sleep(500)
        Send("^c");copy the selected text to clipboard
        Sleep(200)
        $TextFromExplorer = ClipGet()
        $i = $i + 1;; only here the program can increase $i
        $FileName = TheFullPathFileName()
        $file = FileOpen($FileName, 10);write the text to file, if it doesnot exist, creats it.
        FileWrite($file, $TextFromExplorer)
        $IfTheFileGood = FileClose($file)
        If $IfTheFileGood == 1 Then
            Beep(100, 150);;it means the success of copying the text to TXT file
            $TheLastCreatedTXTFile = $FileName;record the last created TXT file so that you can press Shift key and left mouse button to delete it
            $BeforeAppendFileSize = -100;; once a new file is created, try to delete the appending section to the last file will be not available
            $CreatOrAppend = "Creat";; it is useful when you try to delete the last created file later
        Else
            $i = $i - 1;;the current last created name is still the one before $i = $i + 1 at the previous step
            Beep(2000, 2000);;it means fail to copy the text to file
        EndIf
    EndIf
EndFunc   ;==>CopyAndCreatNewFile
;;copy the text and append the text to the previous file,  the return array's first element tells if the operation is OK.
Func CopyAndAppendToFile()
    ;;$TryToCopy = 2; it means you try to copy some text and cappend to its previous file
    $begin = TimerInit();control the time interval you press the key
    $TooLong = 0
    While _IsPressed(11);only when you release the Ctrl key the program begins to try to copy the text
        Sleep(50)
        $dif = TimerDiff($begin)
        If $dif >= 1500 Then
            Beep(500, 250);; it means you press the Ctrl key too long. when you try to abandon this time's text copy, you can use this manner
            $TooLong = 1; it means you press the Ctrl+Alt key too long, the copy text operarion fails
            ExitLoop; it means you press the Ctrl key too long and program begin another time's text-copy
        EndIf
    WEnd
    While _IsPressed(11);;it waits you release the Ctrl key so that another time text-copy can begin Sleep(50);; without this section, your long-time pressing Ctrl+Alt key will fail to abandon this-time's text-copy operation
    WEnd
    If $TooLong == 0 Then
        Send("^a");select all the text
        Sleep(500)
        Send("^c");copy the selected text to clipboard
        Sleep(200)
        $TextFromExplorer = ClipGet()
        $file = FileOpen($FileName, 9);write the text to file, if it doesnot exist, creats it.
        Global $BeforeAppendFileSize = FileGetSize($FileName)
        ConsoleWrite($BeforeAppendFileSize)
        ConsoleWrite(@CR)
        FileWrite($file, $TextFromExplorer)
        $IfTheFileGood = FileClose($file)
        If $IfTheFileGood == 1 Then
            Beep(100, 150);;it means the success of copying the text to TXT file
            ;; since there is no new file is created, so it is no need to refresh the $TheLastCreatedTXTFile
            $CreatOrAppend = "Append";;it is useful when youtry to delete the last section you append to the last file
        Else
            Beep(2000, 2000);;it means fail to copy the text to file
        EndIf
    EndIf
EndFunc   ;==>CopyAndAppendToFile
;thansfer the filename that will be created to the full path one
Func TheFullPathFileName()
    If 1000 < $i Then $FileName = $FileLocation & String($i) & ".txt"
    If 100 < $i <= 1000 Then $FileName = $FileLocation & "0" & String($i) & ".txt";; it can list all files accoeding to their name order
    If 10 < $i <= 100 Then $FileName = $FileLocation & "00" & String($i) & ".txt"
    If 0 <= $i < 10 Then $FileName = $FileLocation & "000" & String($i) & ".txt"
    Return ($FileName)
EndFunc   ;==>TheFullPathFileName
Func DeleteLastCreatedFile()
    $IfSuccess = FileDelete($TheLastCreatedTXTFile);;only the last file can be deleted
    If $IfSuccess == 1 Then
        MsgBox(1, "File Deleted", "OK! The last created TXT file has been deleted successfully!")
        For $FileDelete = 1 To 5
            Beep(50, 100)
            Sleep(50)
        Next
        $i = $i - 1
    EndIf
    While (_IsPressed(10) Or _IsPressed(01)); wait the user to release the shift key and left mouse button, otherwise program stop here to wait
        Sleep(50);; without this section, the program will try to delete the last created TXT file again and again WEnd;; we donot hope such occasion happens. but this section is not always neccessary, since the last created file has been deleted no useful operation will happen
    ;;Try to use Ctrl+Left mouse key to copy some text and creat a new file to save it
EndFunc   ;==>DeleteLastCreatedFile
Func DeleteLastAppendedSectionFromTheFile()
    $file = FileOpen($TheLastCreatedTXTFile, 0)
    $WholeFile = FileRead($TheLastCreatedTXTFile);read the whole file
    FileClose($file)
    $BeforeAppendFile = StringMid($WholeFile, 1, $BeforeAppendFileSize)
    $file = FileOpen($TheLastCreatedTXTFile, 2)
    $ifWrite = FileWrite($file, $BeforeAppendFile)
    $Ifclose = FileClose($file)
    If $ifWrite And $Ifclose Then
        For $FileDelete = 1 To 5
            Beep(50, 100)
            Sleep(50)
        Next
    EndIf
    $BeforeAppendFileSize = -100;; avoid the next time to try to delete the appending section
EndFunc   ;==>DeleteLastAppendedSectionFromTheFile
Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate
Func ShowMessage()
    $Howmanyfilecreated = $i - $TheFirstCreatedFileNameNumber
    $totalFiles = "So far you creat  " & String($Howmanyfilecreated) & "  files."
    MsgBox(1, "Files Created", $totalFiles)
EndFunc   ;==>ShowMessage

Link to comment
Share on other sites

  • 1 year later...

Wow, that is impressive... digging up a year old thread to tell him about a spelling mistake :)

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

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