/* RhymeMenu.ahk by Jack Dunning January 15, 20174 Downloads rhymes and displays menu options for selected word. Part of the "Why AutoHotkey for Poets?" blog. Similar script discussed in Beginning AutoHotkey Hotstring book found at http://www.computoredgebooks.com and blogs at https://jacksautohotkeyblog.wordpress.com */ #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Enable warnings to assist with detecting common errors. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. /* The script uses the standard Windows Clipboard routine for capturing highlight text describe in the following blog: https://jacksautohotkeyblog.wordpress.com/2016/03/23/autohotkey-windows-clipboard-techniques-for-swapping-letters-beginning-hotkeys-part-9/ */ ^!r:: ActWin := WinActive("A") OldClipboard:= ClipboardAll Clipboard:= "" Send, ^c ;copies selected text ClipWait 0 If ErrorLevel { MsgBox, No Text Selected! Return } ; Download the target Web page source code UrlDownloadToFile, http://www.rhymer.com/RhymingDictionary/%Clipboard%.html, rhymes ; Read downloaded file into variable rhymes FileRead, rhymes, rhymes ; If no rhymes are found If (InStr(rhymes,"is not found in")) { MsgBox, "%Clipboard%" not found! Return } /* This next section parses the same text three times. Once for one-syllable rhymes Once for two-syllable rhymes Once for three-syllable rhymes While these RegExs chop out a significant piece of the text, they are not perfect. Good enough? They are next formatted for parsing with the StringSplit command using the RegExReplace command. Three list to make three menus */ RhymePos := RegExMatch(rhymes,"One-syllable rhymes.+?",rhymelistone) RhymePos := RegExMatch(rhymes,"Two-syllable rhymes.+?",rhymelisttwo) RhymePos := RegExMatch(rhymes,"Three-syllable rhymes.+?",rhymelistthree) Rhymelistone := RegExReplace(rhymelistone,".+?(.+?)","$1,") Rhymelisttwo := RegExReplace(rhymelisttwo,".+?(.+?)","$1,") Rhymelistthree := RegExReplace(rhymelistthree,".+?(.+?)","$1,") StringSplit, RhymeArrayone, RhymeListone, `, StringSplit, RhymeArraytwo, RhymeListtwo, `, StringSplit, RhymeArraythree, RhymeListthree, `, ; Each loop creates one menu. ItemCount used to detect empty arrays. ItemCount := 0 Loop %RhymeArrayone0% { Item := RhymeArrayone%A_Index% if (RhymeArrayone%A_Index% != " ") and (InStr(item,"") = 0) ; { Menu, MyMenuone, add, %Item%, WordMenuAction ItemCount++ } } If ItemCount = 0 Menu, MyMenuone, add, None, NoAction ItemCount := 0 Loop %RhymeArraytwo0% { Item := RhymeArraytwo%A_Index% if (RhymeArraytwo%A_Index% != " ") and (InStr(item,"") = 0) ; { Menu, MyMenutwo, add, %Item%, WordMenuAction ItemCount++ } } If ItemCount = 0 Menu, MyMenutwo, add, None, NoAction ItemCount := 0 Loop %RhymeArraythree0% { Item := RhymeArraythree%A_Index% if (RhymeArraythree%A_Index% != " ") and (InStr(item,"") = 0) ; { Menu, MyMenuthree, add, %Item%, WordMenuAction ItemCount++ } } If ItemCount = 0 Menu, MyMenuthree, add, None, NoAction ; Submenus added to the main menu Menu, MyMenuMain, add, One-syllable, :MyMenuone Menu, MyMenuMain, add, Two-syllable, :MyMenutwo Menu, MyMenuMain, add, Three-syllable, :MyMenuthree Menu, MyMenuMain, add, Visit Site, VisitSite Menu, MyMenuMain, Show ; Clean up used menu Menu, MyMenuMain, DeleteAll Menu, MyMenuone, DeleteAll Menu, MyMenutwo, DeleteAll Menu, MyMenuthree, DeleteAll Clipboard:= OldClipboard Return WordMenuAction: WinActivate Send, %A_ThisMenuItem% Return VisitSite: Run http://www.rhymer.com/RhymingDictionary/%Clipboard%.html Return NoAction: Return