Macro: Remove iBooks citation - question regarding regex and general approach

Hi, my macro replaces the mandatory citation from iBooks excerpts.  While this is a handy feature in some situations, it isn't helpful for e.g. looking copied text up on the web.

They look like this (German system language):

„Behold my recurring nightmare: Are we, too, missing some basic pieces of the universe that once were? What part of the cosmic history book has been marked “access denied”? What remains absent from our theories and equations that ought to be there, leaving us groping for answers we may never find?“

Auszug aus: Neil DeGrasse Tyson. „Astrophysics for People in a Hurry.“ iBooks.

The result of my macro should be:

Behold my recurring nightmare: Are we, too, missing some basic pieces of the universe that once were? What part of the cosmic history book has been marked “access denied”? What remains absent from our theories and equations that ought to be there, leaving us groping for answers we may never find?

The macro works for me.

Questions:

  • Should I improve the regular expressions, as I might miss some side effect?
  • Is there a better approach to my problem?

Thanks,

Chris

PS: Just saw that the disabled actions at the end contains the former name of the variable.

Yes to both. I'm sure there's more than one way to tackle the problem, but here's one that will allow you to replace four of your Search and Replace actions with a single Search Using Regular Expression action:

Quite usefully, as well, you'll now have the different components of the citation stored in their own variable: Excerpt being the one to which your question directly pertains, containing only the quote with the speech marks and iBooks reference stripped away; Author, which will store the name of the author as given by the iBooks reference; and Title, which will store the title of the book from the iBooks reference.

Here's the regular expression I used:

^„(.*)“[\r\n]*Auszug aus: (.*)\. „(.*)\.“ iBooks.

(You'll have to make sure the characters used to match quote marks are the correct ones. I did a direct copy-n-paste from your post into KM, and those were the symbols that were used.)

2 Likes

Thank you for your recommendations. Storing text in different variables opens more possibilities (although I don't need it right now) and the regular expression is much more elegant than my solution.

The regex works at regex101.com (https://regex101.com/r/roRz7M/1/) with $ at the end, but in the KM macro only without. (Like in your screenshot). This shouldn't be an issue however.

Thanks again!

  • Chris

Thanks for highlighting this. I've amended the answer appropriately.

I'm a bit confused by this statement. It always gets my attention when Regex101.com does NOT work the same as KM Search Action.

I am finding that the RegEx pattern and source as shown in regex101.com (regex101: build, test, and debug regex) does work FINE with $ at the end in all of these tools:

  1. KM Search
  2. RegEx101.com
  3. RegExRX.app

Clearly the "$" is NOT needed, but it does not hurt in this case.

I won't bother posting my test macro, but here is the image of it showing that it does work in KM:

image

@qrt, perhaps something else is causing the difference for you.

I'm running Keyboard Maestro 8.2.4 on macOS 10.12.6.

Thanks for double-checking this. I, personally, do prefer the ending $ here.