Advertisement
n8abx9

Copy highlighted words to file for Anki (Readlang style)

Oct 6th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; TODO: ADAPT FILEPATH BELOW
  2. ; Copies a highlighted words and its context plus browser source URL (Firefox) to a file
  3. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  4. ; #Warn  ; Enable warnings to assist with detecting common errors.
  5. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  6. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  7.  
  8.  
  9. ^3::
  10. tmp := clipboard
  11. clipboard := ""
  12. Send, ^c
  13. ClipWait, 2
  14. word := RegExReplace( RegExReplace(RegExReplace(clipboard, "\r?\n"," "), "(^\s+|\s+$)"), ";", "," )
  15. clipboard := ""
  16. bLength := StrLen(clipboard)
  17. rRegex := "[\.!?]"
  18. rReplaceRegex := "(.+[\.!?]+)[^\.!?]*"
  19. lRegex := "[:\.!?]"
  20. lReplaceRegex := ".*[:\.!?]\)*\s*(.+)"
  21. Send, +{Down}
  22. IfWinActive ahk_exe firefox.exe
  23. Loop
  24. {
  25.     Send, ^c
  26.     ClipWait, 2
  27.     aLength := StrLen(clipboard)
  28.     if ( RegExMatch( clipboard, rRegex ) or bLength == aLength )
  29.         break  ; Terminate the loop
  30.     else {
  31.         Send, +{Right}
  32.         clipboard := ""
  33.         bLength := aLength
  34.         continue ; Skip the below and start a new iteration
  35.     }
  36. }
  37. rContext := RegExReplace(clipboard, rReplaceRegex, "$1" )
  38. clipboard := ""
  39. Sleep, 200
  40. bLength := StrLen(clipboard)
  41. Send, +{Up}
  42. IfWinActive ahk_exe firefox.exe
  43. Loop
  44. {
  45.     Send, ^c
  46.     ClipWait, 2
  47.     aLength := StrLen(clipboard)
  48.     if ( RegExMatch( clipboard, lRegex ) or bLength == aLength )
  49.         break  ; Terminate the loop
  50.     else {
  51.         Send, +{Up}
  52.         clipboard := ""
  53.         bLength := aLength
  54.         continue ; Skip the below and start a new iteration
  55.     }
  56. }
  57. context := RegExReplace(clipboard, lReplaceRegex, "$1" ) rContext
  58. context := RegExReplace( RegExReplace(RegExReplace( context, "\r?\n"," "), "(^\s+|\s+$)"), ";", "," )
  59. clipboard := ""
  60. Sleep, 200
  61. IfWinActive ahk_exe firefox.exe
  62. Send, ^l
  63. Sleep, 100
  64. Send, ^c
  65. ClipWait, 2
  66. link := RegExReplace(RegExReplace(clipboard, "\r?\n"," "), "(^\s+|\s+$)")
  67. clipboard := tmp ; restores original clipboard
  68. Sleep, 100
  69. ClipWait
  70. Sleep, 100
  71. ClipWait
  72. FileAppend, %word%;%context%;%link%;`n, C:\Users\TODO\Documents\Anki.txt
  73. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement