Jump to content

Workflow: Strip citation from iBooks selection and append to text file?


Recommended Posts

When copying text from iBooks, pasting produces text + citation:

 

Quote

 

“In preparing yourself for war, you must rid yourself of myths and misconceptions.”

Excerpt From: Robert Greene & Joost Elffers. “The 33 Strategies of War.” iBooks. https://itun.es/us/jxwvv.l

 

 

I'd like to be able to copy text only from iBooks (sans the citation and quotes ideally) and append to a separate plain text file. Really, it'd be wonderful to utilize Alfred's hotkey and selected text function as an input. So, highlight text in iBooks, hit a hotkey, have the citation stripped, and text appended to a separate file for review.

 

Thoughts on how to accomplish?

Link to comment

This will strip the quotes and citation from text copied from iBooks. I tested it with a Hotkey + Selection in macOS. Connect it to whatever output you like.

 

Run Script Action, Language = /usr/bin/python with input as argv

# encoding: utf-8
from __future__ import print_function
import sys

# Fancy quotes
quotes = u'\u201c\u201d'

if len(sys.argv) < 2:
    print('No input', file=sys.stderr)
    sys.exit(1)

text = sys.argv[1].decode('utf-8')

if '\n' in text:
    # Remove last line (citation)
    text = u'\n'.join(text.splitlines()[:-1]).strip()

# Remove surrounding quotes
text = text.rstrip(quotes).lstrip(quotes).strip()

# Output cleaned text
print(text.encode('utf-8'), end='')

Thanks for the beer, btw!

Edited by deanishe
Link to comment

Strong work, @deanishe. I wish I had your python skills, Sir.

 

I can only get Alfred to recognize input with 'hotkey + clipboard content' by first copying the text in iBooks. Were you able to pass input with 'hotkey + selection' alone? Also, Alfred's built-in 'append to file' output is great, but I can't figure out how to separate text with a blank line. Is there a python method I can tack onto the existing code?

 

Side-note: I understand the appeal of argv. I like the simplicity, but what is the {query} equivalent for the argv input? Is it {argv}?

 

Sorry for all the questions. I'm loving code way too much.

Link to comment

@deanishe

 

Tested the workflow in the Kindle app; it works perfectly with 'hotkey + selection'. Maybe just an iBooks thing. Any workarounds for iBooks specifically?

 

Also, because it was bugging me so much, I eventually figured out the bit about Alfred's 'append to file' output. Turns out I just had to add a physical return after the {query}. I feel so dumb for asking you about it.

Link to comment
3 hours ago, jonteamere said:

I can't figure out how to separate text with a blank line. Is there a python method I can tack onto the existing code?

 

Change the last line of the code from print(text.encode('utf-8'), end='') to print(text.encode('utf-8'))

 

That will add a trailing newline to the output.

 

3 hours ago, jonteamere said:

I understand the appeal of argv. I like the simplicity, but what is the {query} equivalent for the argv input? Is it {argv}?

 

There is no equivalence between argv and {query}. "input as argv" means pass the script the input as command-line options. Using {query} means Alfred edits your code to replace {query} with the input before running the code. argv is almost always the better solution (which is why Andrew has made it the default).

 

It's the standard way to pass arguments around, and you don't have to mess about with the Escaping options needed to make {query} work without breaking your code. Almost everybody got Escaping options wrong at some point, and there are some situations where it really doesn't work.

 

1 hour ago, jonteamere said:

Any workarounds for iBooks specifically?

 

Not a clue. It works fine with the couple of books that I have.

Link to comment
  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...