Macro to get rid of the Kindle Book title text when copying from the Kindle App

I do a lot of programming and use Kindle Books to learn new languages. One of the problems is that when I copy a section of code to run it in my REPL, it always has additional test like this...
"

Wampler, Dean. Programming Scala (p. 35). O'Reilly Media. Kindle Edition. "
That's a CR + text. I would like to replace that with "".

At least one other person has come up against the problem, and has written a script in AutoHotKeys for Windows. I wonder if some bright spark here could come up with a KM equivalent, ideally one which uses the exact same steps, ie: Select text, ⌘C, switch app, ⌘V. I had a go myself, but I couldn't come up with anything functional... but then I am only on page 35 :wink:

The AutoHotKeys script is...

#SingleInstance force
^k:: ; Ctrl + k triggers the scripts
Send, ^c ; Copies text
ClipWait ; Waits for clipboard to finish populating
AuthorName := "G.ron, Aur.lien.+" ; Change this variable to the name of the author of the book you are reading
ReplaceStr := RegExReplace(Clipboard, AuthorName, "") ; Replace citation with empty string
Clipboard := ReplaceStr ; Assign ReplaceStr to the Clipboard
return

That's the tricky bit -- how does KM know what text to select for you?

Otherwise, and assuming you select the text yourself, you want to hit ⌃K to:

  1. Copy
  2. Search and replace in the text on the Clipboard
  3. Activate the target app
  4. Paste

For the search and replace, you want to find "a newline followed by some defined text followed by the rest of that line and a newline" and replace with nothing. That's a regular expression and, while it's difficult to be sure with the limited information given, the pattern will be something like

\RWampler, Dean.+\R

...using \R for any newline (in case the text copied somehow has Windows newlines instead of Unix/Mac), the author's name, one or more characters that aren't a newline, and the newline at the end.

To make it easier when you change books you can use a variable for the author's name -- then you only have to set it at the top of the macro, not hunt through the actions. And for safety we'll encode the variable, in case their name contains characters that would confuse the regex.

You'll have to change the target of the "Activate" action to the app you want to paste into but, putting all the above together:

Strip Author from Kindle Snippet.kmmacros (3.5 KB)

Image

2 Likes

Thanks Nige! Sorry I should have mentioned that I would be selecting the text myself. And I was also expecting to have to right click and select copy from Kindle's own context menu. However, the macro didn't - it looks as if somehow, Kindle is blocking Keyboard Macro from working at all.
This didn't even work!

Try KM's Interactive Help (in the Editor's Help menu). That'll help you with the not-so-obvious stuff like whether the Macro Group is enabled and so on.

Meanwhile you might be able to disable the "Copy" action then do a manual select and copy in the Kindle app before running the macro to do the transform-and-paste.

No matter! Out of every problem comes an opportunity, they say. This prompted me to look at O'Reilly Online Learning which I used to use years ago in its former guise of Safari Online. It has that book and 40,000 other interesting ones. And you can copy and paste freely!