Stripping quotes from iBooks copy

When you copy something from an iBooks book, the text copied and quoted, i.e., the result is"text copied" and an annotation about the source. A fix with AppleScript and Automator is posted here..

How can I do this in KM? I’d like to say have Cmd-option-C copy the text and put just the selected text on the clipboard.

This is the AppleScript:

# strip out iBooks citation
sed -E -e 's/^[ ]?[0-9]* //g' | sed -E -e 's/“[ ]?[0-9]?[ ]?//g' | sed -E -e 's/”$//g'  | sed -E -e 's/^(Excerpt From).*//g'

I tried KM Copy, Execute AS; but then couldn’t return output to clipboard (or maybe the script isn’t really running.

Thanks for any help. The link also has a Ruby script.

Hey @KBM,

No, it's not – it's a straight-up shell script.

Try running it from an Execute a Shell Script action and outputting to the Clipboard.

-Chris

I was but found another error. I missed the option to copy to “save results to clipboard.” Duh! But now the script is giving an error.

script error: Expected expression but found “/”. (-2741)

which means little to me. Particularly since many slashes appear.

I tried the Ruby script, but got this error

/var/folders/y1/2kz16x1n4zd46lwwyss_g_4m0000gn/T/Keyboard-Maestro-Script-D26E2094-6428-4661-A10D-4704D670360C:6:in `match': invalid byte sequence in US-ASCII (ArgumentError)
	from /var/folders/y1/2kz16x1n4zd46lwwyss_g_4m0000gn/T/Keyboard-Maestro-Script-D26E2094-6428-4661-A10D-4704D670360C:6:in `match'
	from /var/folders/y1/2kz16x1n4zd46lwwyss_g_4m0000gn/T/Keyboard-Maestro-Script-D26E2094-6428-4661-A10D-4704D670360C:6:in `block in <main>'
	from /var/folders/y1/2kz16x1n4zd46lwwyss_g_4m0000gn/T/Keyboard-Maestro-Script-D26E2094-6428-4661-A10D-4704D670360C:3:in `loop'
	from /var/folders/y1/2kz16x1n4zd46lwwyss_g_4m0000gn/T/Keyboard-Maestro-Script-D26E2094-6428-4661-A10D-4704D670360C:3:in `<main>'

Thank you for responding. I’m good at getting errors

Post a sample citation – so I have something to test – and I'll try to fix it.

-Chris

If you mean, what does iBooks copy look like:

“class AddDescriptionToLocations < ActiveRecord::Migration
  def change
    add_column :locations, :name, :string
    add_column :locations, :description, :string
  end
end”

Excerpt From: W. Jason Gilmore. “Easy Active Record for Rails Developers.” iBooks. 

What is wanted is what’s between the first set of double quotes.

Okay, try this.

Execute Shell Script → Strip Junk from iBook Copy.kmmacros (2.2 KB)

Works like a charm. Thank you.

This solution doesn't work for me. Neither does this one (though it worked for a while).

When I copy in Apple Books, I get this:

“In fact, the performance of British riders had been so underwhelming that one of the top bike manufacturers in Europe refused to sell bikes to the team because they were afraid that it would hurt sales if other professionals saw the Brits using their gear.”

Excerpt From
Atomic Habits: Tiny Changes, Remarkable Results
James Clear
This material may be protected by copyright.

I'd like for the macro to be executed seamlessly—when I press Cmd+C in Books.

Any help? :pray:

Doesn't work in what way?

This is working for me, leaving the "cleaned" text on the System Clipboard ready to be pasted:

Trim Books Copy.kmmacros (2.3 KB)

1 Like

Your version works, thank you for sharing. Your regex differs from the one shared by @ccstone.

Chris's regex should work -- at least for the text you provided (it has problems if there are quotes within the copied text). The Paste action will fail if you run it with Books frontmost and the text you want selected, but you should still be able to switch to another app and Paste the cleaned text manually. Perhaps that was the problem?