0
votes

You have many code sample boxes in the Questions and Answers in stackoverflow

I want this: if i go to that box, that i can do in ONE CLICK f2 save the text from that box direct to a file.

The autohotkey (AHK) script must do this:

  • Select all text in box
  • Copy the text to clipboard
  • Save the text to a file (example.ahk)

This script does not work.

; ^ = Ctrl 
; ! = Alt
; # = Win (Windows logo key)
; + = Shift
f2::
; this does not work it will select all text from the website 
; but i want do that it select all the text in that box 
; and then save it direct to a file
send ^a 
send ^c ;copy selected text to clipboard
FileAppend, %Clipboard%, %A_MyDocuments%\example.ahk ;save clipboard to file
return
1
Do you expect your script to work? Surely you must be aware that ^a selects all the text in a browser document, not just the currently selected "code box". Anyway, AHK can't get you very far since modern browsers don't expose any content of their documents. Injecting native JavaScript into the browser document (e.g. with a User script) is the most powerful option.MCL

1 Answers

1
votes

Maybe something like this:

F2::
FileDelete, %A_Temp%\stackoverflow website.txt
FileDelete, %A_MyDocuments%\example.ahk
ClipSaved := ClipboardAll       ; save clipboard
clipboard = ""                  ; empty  clipboard
Send, ^l                        ; mark the adress in your browser 
Sleep, 100
Send, ^c                        ; copy the adress 
ClipWait 1                      ; wait for the clipboard to contain data
If not ErrorLevel               ; If NOT ErrorLevel clipwait found data on the clipboard
{
    If !InStr(clipboard, "http://stackoverflow.com")
    {
        MsgBox, You're NOT in stackoverflow
        clipboard := ClipSaved
                return
    }
    ; otherwise:
    UrlDownloadToFile, %clipboard%, %A_Temp%\stackoverflow website.txt
        Sleep, 200
    FileRead, Contents, %A_Temp%\stackoverflow website.txt
    {
        Array := StrSplit(Contents, "code>")
        for key, val in Array
        {
            If  (SubStr(val, -1) = "</")
            {
                If (StrLen(val) < 50)
                    continue
                If InStr(val, "WScript")
                    continue
                ; ... add more restrictions

                val := SubStr(val, 1, -2) 
                MsgBox, 4,, Do you want to save this code? `n`n%val%                
                IfMsgBox Yes
                {
                    FileAppend, %val%, %A_MyDocuments%\example.ahk ; save val to file
                         break
                }
            }
        }
    }
}
Sleep, 300
clipboard := ClipSaved               ; restore original clipboard
FileDelete, %A_Temp%\stackoverflow website.txt
return