Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Libreoffice Key Hash Macro
Sub Main
Dim oDocument As Object
Dim oSearch As Object, oFound As Object
Dim md5cmd As String
oDocument = ThisComponent
oSearch = oDocument.createSearchDescriptor
oSearch.SearchRegularExpression = TRUE
' REGEX TO IDENTIFY KEY LINES
oSearch.SearchString = "\<.{64}\>"
oFound = oDocument.findFirst(oSearch)
' LOOPING THROUGH RESULT LINES
While NOT IsNull(oFound)
oFoundCursor = _
oFound.Text.createTextCursorByRange(oFound)
' EXECUTE SHELL COMMAND AND PIPE OUTPUT TO CLIPBOARD
md5cmd = "echo -n $(md5 -qs " + oFound.String+")"
Shell ("bash", 1, "-c "" " & md5cmd & " | xclip -selection clipboard"" ", true)
' REPLACE THE LINE WITH ADDED MD5 SUM
oFound.String = oFound.String + " md5: " + getClipboardText()
oFound = oDocument.findNext(oFound, oSearch)
Wend
End Sub
' FUNCTION TO READ THE CLIPBOARD CONTENT (USED ABOVE)
Function getClipboardText () AS String
dim oClip as object ,oConverter as object
dim oClipContents as object ,oTypes as object
dim i%
oClip = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
oConverter = createUnoService("com.sun.star.script.Converter")
On Error Resume Next
oClipContents = oClip.getContents
oTypes = oClipContents.getTransferDataFlavors
For i=LBound(oTypes) To UBound(oTypes)
If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
Exit For
End If
Next
If (i >= 0) Then
On Error Resume Next
getClipboardText = oConverter.convertToSimpleType _
(oClipContents.getTransferData(oTypes(i)), com.sun.star.uno.TypeClass.STRING)
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment