Jump to content

Mail2File GUI to save clipboard data as file


Recommended Posts

a program to send a copy of the clipboard to a folder inbox in your @ScriptDir

Please run the script  in test mode (with out to compile) less one time to make the necessary folders in your @ScriptDir (line 37 to 40)

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Comment=Mail to file GUI to save clipboard data as file
#AutoIt3Wrapper_Res_Description=Mail2File
#AutoIt3Wrapper_Res_Fileversion=1.2.1.0
#AutoIt3Wrapper_Res_ProductName=Mail2File
#AutoIt3Wrapper_Res_ProductVersion=1
#AutoIt3Wrapper_Res_CompanyName=ioa747
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 6 -w- 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Mail2File.au3
;Mail to file GUI to save clipboard data as file
#include <ButtonConstants.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Local $iParams = $CmdLine[0]
Local $dP  ; === debug print OFF

If Not $iParams Then
    $iParams = "Empty"

    MsgBox($MB_SYSTEMMODAL, "Test mode", "$iParams = " & $iParams & @CRLF _
             & "Mail2File startet with out parameters" & @CRLF & "... and for this reason" & @CRLF & "begin in test mode")

    $dP = 1 ; === debug print ON
    $iParams = "mailto:?subject=Forums%20-%20AutoIt%20Forums&body=https://www.autoitscript.com/forum/"

    If FileExists(@ScriptDir & "\_Test") Then
        ;ok
        If $dP = 1 Then ConsoleWrite(@ScriptDir & "\_Test = ok" & @CRLF)
    Else
        If $dP = 1 Then ConsoleWrite(@ScriptDir & "\_Test = not found" & @CRLF )
        ; Create the inbox directory.
        DirCreate(@ScriptDir & "\_Test")
        DirCreate(@ScriptDir & "\_Test\000_inbox")
        DirCreate(@ScriptDir & "\_Test\001_info")
        DirCreate(@ScriptDir & "\_Test\002_Autoit")
        If $dP = 1 Then ConsoleWrite("... and make it" & @CRLF)
    EndIf

Else
    $iParams = $CmdLine[1]
    Send("{ESC}")

EndIf

If $dP = 1 Then ConsoleWrite("$iParams = " & $iParams & @CRLF)
PutInLog($iParams)

Local $sFilePath, $tmp, $fTopic, $fComment, $fLink
Local $mLink, $mMailto, $mSubject, $mBody
Local $sClipData = ClipGet()

$mLink = StringSplit($iParams, "?subject=", 1)
If $dP = 1 Then ConsoleWrite("$mLink[0] = " & $mLink[0] & @CRLF)

; $mMailto ------------------------------------------------------------------------------
If $dP = 1 Then ConsoleWrite("$mLink[1] = " & $mLink[1] & @CRLF)
$mMailto = StringLeft($mLink[1], 7)
If $mMailto = "mailto:" Then
    $mMailto = StringTrimLeft($mLink[1], 7)
    If StringLen($mMailto) > 7 Then ; mailto:....
        $mMailto = CharReplace($mMailto)
    Else
        $mMailto = "<Empty>"
    EndIf
EndIf
If $dP = 1 Then ConsoleWrite("- $mMailto --> " & $mMailto & @CRLF)

; $mSubject -----------------------------------------------------------------------------
If $dP = 1 Then ConsoleWrite("$mLink[2] = " & $mLink[2] & @CRLF)
$mSubject = StringReplace($mLink[2], "?", "")
$tmp = StringSplit($mSubject, "&body=", 1)
$mSubject = $tmp[1]
If $dP = 1 Then ConsoleWrite("StringLen($mSubject) = " & StringLen($mSubject) & @CRLF)
If StringLen($mSubject) > 1 Then     ; subject=....
    $mSubject = CharReplace($mSubject)
Else
    $mSubject = "<Empty>"
EndIf
If $dP = 1 Then ConsoleWrite("- $mSubject --> " & $mSubject & @CRLF)

; $mBody --------------------------------------------------------------------------------
If $dP = 1 Then ConsoleWrite("$tmp[2] = " & $tmp[2] & @CRLF)
$mBody = $tmp[2]
If $dP = 1 Then ConsoleWrite("StringLen($mBody) = " & StringLen($mBody) & @CRLF)

If StringLen($mBody) > 1 Then     ; Body=....
    $mBody = CharReplace($mBody)
Else
    $mBody = "<Empty>"
EndIf
If $dP = 1 Then ConsoleWrite("- $mBody --> " & $mBody & @CRLF)

MailDaemonGUI($mSubject, $mBody) ;, $mMailto)

ConsoleWrite(">>>>>>>>>>>>> Exit" & @CRLF)

Exit

;----------------------------------------------------------------------------------------
Func CharReplace($String) ; Replace %xx escapes
    Local $iPosition, $iChar, $NewString

    $NewString = $String
    While 1
        $iPosition = StringInStr($NewString, "%")
        If $iPosition > 0 Then
            $iChar = StringMid($NewString, $iPosition + 1, 2)
            If $dP = 1 Then ConsoleWrite(" -- %" & $iChar & "=" & Chr(Dec($iChar)))
            $NewString = StringReplace($NewString, "%" & $iChar, Chr(Dec($iChar)))
        Else
            ExitLoop
        EndIf
    WEnd

    If $dP = 1 Then ConsoleWrite("" & @CRLF)

    $NewString = StringReplace($NewString, "�", "-")
    Return $NewString


EndFunc   ;==>CharReplace
;-------------------------------------------------------------------------------
Func OpenScript() ; ShellExecute
    ShellExecute($sFilePath)
EndFunc   ;==>OpenScript
;-------------------------------------------------------------------------------
Func PutInLog($TxtData) ; Put trak In Log
    ; Create a constant variable in Local scope of the filepath that will be read/written
    Local Const $sFilePath = @ScriptDir & "\Mail-LogBook.log"

    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the Mail-LogBook.log file.")
        Return False
    EndIf

    ; Write data to the file using the handle returned by FileOpen.
    FileWrite($hFileOpen, @MDAY & "." & @MON & "." & @YEAR & "-" & @HOUR & ":" & @MIN & ":" & @SEC & "-" & @MSEC & "  ")
    FileWrite($hFileOpen, $TxtData & @CRLF)


    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

    Return True

EndFunc   ;==>PutInLog
;----------------------------------------------------------------------------------------
Func MailDaemonGUI($Subject, $Body, $MailTo = "") ; run the MailDaemonGUI

    Local $ClipData = ClipGet() ;store clipboard
    ClipPut("")

    ; remove illegal character
    $Subject = StringRegExpReplace($Subject, '[/\\:*?"<>|]', '')
    $Subject = StringRegExpReplace($Subject, '(--*)', '-')
    $Subject = StringRegExpReplace($Subject, '\s', '_')
    $Subject = StringRegExpReplace($Subject, '(__*)', '_')

    Local $MailDaemonGUI = GUICreate("Mail Daemon", 510, 400, -1, -1, -1, $WS_EX_TOPMOST)
    GUISetIcon("C:\Windows\system32\ieframe.dll", -73)

    GUICtrlCreateLabel("MailTo:", 10, 10, 100, 15)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    Local $cMailTo = GUICtrlCreateCombo($MailTo, 10, 30, 490, 20) ;BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    GUICtrlCreateLabel("Subject:", 10, 60, 101, 15)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    Local $tSubject = GUICtrlCreateInput($Subject, 10, 80, 490, 40)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    GUICtrlCreateLabel("Body:", 10, 130, 40, 15)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    Local $lBody = GUICtrlCreateLabel($Body, 45, 130, 455, 15)
    GUICtrlSetFont(-1, 8, 550, 0, "MS Sans Serif")
    Local $tBody = GUICtrlCreateInput($ClipData, 10, 150, 490, 140, BitOR($WS_VSCROLL, $ES_MULTILINE))
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    GUICtrlCreateLabel("File name:", 10, 300, 100, 15)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    Local $tFileName = GUICtrlCreateInput("", 10, 320, 280, 24)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    GUICtrlCreateLabel("Save as...", 310, 300, 100, 15)
    GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    Local $cFileType = GUICtrlCreateCombo("", 310, 320, 190, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    Local $Button_Cancel = GUICtrlCreateButton("Cancel", 310, 360, 90, 31, $BS_CENTER)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    Local $Button_OK = GUICtrlCreateButton("OK", 410, 360, 90, 31, $BS_CENTER)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")

    ; Includes directories to the $cMailTo combobox.
    _GUICtrlComboBox_BeginUpdate($cMailTo)
    _GUICtrlComboBox_AddDir($cMailTo, @ScriptDir & "\_Test\*", $DDL_DIRECTORY)
    _GUICtrlComboBox_EndUpdate($cMailTo)

    ; Select Item
    _GUICtrlComboBox_SetCurSel($cMailTo, 1)

    ; Add additional items to the $cFileType combobox.
    GUICtrlSetData($cFileType, ".txt|.au3|.html", ".txt") ; * <--- Default FileType

    If $dP = 0 Then Send("^{F4}") ; Close curent firefox Tab Ctrl + F4 * <-- uncoment this
    Sleep(200)

    Send("^c") ; Copy selectet txt for $tFileName
    Local $Selection = ClipGet()

    If StringLen($Selection) < 2 Then $Selection = "file"

    ; remove illegal character
    $Selection = StringRegExpReplace($Selection, '[/\\:*?"<>|]', '')
    $Selection = StringRegExpReplace($Selection, '(--*)', '-')
    $Selection = StringRegExpReplace($Selection, '\s', '_')
    $Selection = StringRegExpReplace($Selection, '(__*)', '_')

    GUICtrlSetData($tFileName, $Selection)
    Local $FileType

    GUISetState(@SW_SHOWNORMAL)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $Button_Cancel
                ExitLoop

            Case $Button_OK
                ; retrive data in case they changed
                $Subject = GUICtrlRead($tSubject)
                $Body = GUICtrlRead($lBody)
                $MailTo = GUICtrlRead($cMailTo)
                $Selection = GUICtrlRead($tFileName)
                $ClipData = GUICtrlRead($tBody)
                $FileType = GUICtrlRead($cFileType)

                Send2($Subject, $Body, $MailTo, $Selection, $ClipData, $FileType)

                GUISetState(@SW_HIDE)

                ;----------------------------------------------------------------------
                ; This will create info tooltip & Sets a hotkey that calls OpenScript()
                HotKeySet("{SPACE}", "OpenScript")
                ToolTip($sFilePath & @CRLF & "press  SpaceBar to open it.", @DesktopWidth / 4, @DesktopHeight / 5, "saved", 1)
                Sleep(4000) ; * <---- tooltip wait time
                ToolTip("")
                HotKeySet("{SPACE}")
                ;----------------------------------------------------------------------

                ExitLoop

            Case $GUI_EVENT_DROPPED    ; ChekDroped()
                ;ChekDroped()

            Case Else
                ;
        EndSwitch
    WEnd
    GUIDelete($MailDaemonGUI)
EndFunc   ;==>MailDaemonGUI
;----------------------------------------------------------------------------------------
Func Send2($Subject, $Body, $MailTo, $fileName, $ClipData = "<Empty>", $FileType = ".txt") ; paste to test folder
    Local $dirName, $iFileExists

    If StringLeft($MailTo, 1) = "[" And StringRight($MailTo, 1) = "]" Then
        $dirName = @ScriptDir & "\_Test\" & StringTrimRight(StringTrimLeft($MailTo, 1), 1)
        If FileExists($dirName) Then
            ;OK File Exists
        Else
            ;set default folder
            $dirName = @ScriptDir & "\_Test\000_inbox"
        EndIf
    Else
        ;set default folder
        $dirName = @ScriptDir & "\_Test\000_inbox"
    EndIf

    ; remove illegal character
    $fileName = StringRegExpReplace($fileName, '[/\\:*?"<>|]', '')
    $fileName = StringRegExpReplace($fileName, '\s', '_')

    ; remove illegal character
    $Subject = StringRegExpReplace($Subject, '[/\\:*?"<>|]', '')
    $Subject = StringRegExpReplace($Subject, '(--*)', '-')
    $Subject = StringRegExpReplace($Subject, '\s', '_')
    $Subject = StringRegExpReplace($Subject, '(__*)', '_')


    $dirName &= "\" & $Subject

    Local $i = 0
    Do
        If $i = 0 Then
            $sFilePath = $dirName & "\" & $fileName & $FileType
        Else
            $sFilePath = $dirName & "\" & $i & "-" & $fileName & $FileType
        EndIf

        $iFileExists = FileExists($sFilePath)

        $i = $i + 1

    Until $iFileExists = 0 ; Increase the value of $i until File not Exists

    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE + $FO_CREATEPATH + $FO_UTF8)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf

    Switch $FileType
        Case $FileType = ".au3"
            $Body =  "; " & $Body
        Case $FileType = ".html"
            $Body =  "<!-- " & $Body & " -->"
        Case Else ; If nothing matches then execute the following.
            $Body =  "this is from " & $Body
    EndSwitch

    ; Write data to the file using the handle returned by FileOpen.
    FileWrite($hFileOpen, $Body & @CRLF & @CRLF)
    FileWrite($hFileOpen, $ClipData)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

EndFunc   ;==>Send2
;----------------------------------------------------------------------------------------

 

  • first you have to compile it into exe
  • then set it as a mail program in firefox

in firefox go to the settings in the Applications section,

point to mailto action, and select Use other…, hit Browse… and select your program

image.png.420f8916ca59e1bf5e8b31bdc255b4f4.png

 

  • then to make it available on every page we add the flagfox plugin

https://addons.mozilla.org/en-US/firefox/addon/flagfox/?utm_source=addons.mozilla.org&utm_medium=referral&utm_content=search

after putting it, we right-click on the flag located in the address bar, point in more.. and then point options

hit down below + Add new action

image.png.92ffac7331dcbf432ce9c0f712f21258.png

 

in Template paste mailto:?subject={title}&body={fullURL}  and in Icon Click: select Click  and hit save

 

After that we are ready! Go to a page copy some text to clipboard then select pair word (act as file name )

and hit the flag located in the address bar

when click OK clip data saved to Subject folder inside 000_inbox folder

image.png.244475ad868b828f750c55e178383537.png

 

:) Please, leave your comments and experiences here.

 

Link to post
Share on other sites

Hi @ioa747,

I will have a look at your topic here later on. First view: interesting, but I am not sure about the use cases yet. Let's see, after reading this again and a tryout 😀 .

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔍 Forum search

🔮 Me on GitHub

💬 Opinion about new forum sub category

 📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to post
Share on other sites

Hi @ioa747,

I still didn't try your program yet, but thank you for the video which is pretty easy to understand 😀 👍 .
Feedback after a tryout follows.

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔍 Forum search

🔮 Me on GitHub

💬 Opinion about new forum sub category

 📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...