Skip to content

Instantly share code, notes, and snippets.

@iandundas
Created July 20, 2014 12:15
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save iandundas/7863c6c248f76e14a3a8 to your computer and use it in GitHub Desktop.
Save iandundas/7863c6c248f76e14a3a8 to your computer and use it in GitHub Desktop.
# 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'
@moritzWa
Copy link

moritzWa commented Jan 12, 2020

Something is wrong with the script. It pasts two additional paragraph spaces.

Edit:
attaching this code did it for me:
| tr -d '\n'

@0xouzm
Copy link

0xouzm commented Feb 7, 2020

I got this problem:
There was a problem with the input to the Service.
and I found I should choose no input in Automator
Thanks! It works!
Now I'm struggling to set my shortcut...

@moritzWa
Copy link

moritzWa commented Mar 1, 2020

The correct code should be this, as noted above:
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' | tr -d '\n'

@sixtyonePRIME
Copy link

Hi Guys,
is there also a solution for the German language? What I have to change in the script? If I copy & paste something, I get an output like this (The text I copied is bold, anything else is added automatically by books):

func greet(person: String) -> String {
let greeting = "Hello, " + person + "!"
return greeting
}

Auszug aus: Apple Inc. „The Swift Programming Language (Swift 5.0).“ Apple Books. https://books.apple.com/de/book/the-swift-programming-language-swift-5-2/id881256329

@sixtyonePRIME
Copy link

When using the proposal form @iandundas, I run into the issue, that the clipboard is stacking up with all the copied content. Therefore you have to delete the "Get Contents of Clipboard" step in Automator.

Here is a step by step guide (the screenshots are in German, but you get the point):

  1. Open Automator >> New Document >> Quick Action Workflow >> Choose

create new Automator Quick Action Workflow

(You can see Step 2-4 in the Screenshot below Step 4)
2. Change following in the headers first row (leave the remaining rows as they are)
2.1. Workflow receives current: chosse "text"
2.2. in: choose Application "Books"

  1. In the body add the "Run Shell Script" step from the library
    3.1. Choose "/bin/zsh" in Shell
    3.2. Add this following script:
    # 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'

Attention: If you work in German language, then change "Excerpt From" with "Auszug von" in the script. This is valid also for other languages, just change the needed translation for "Excerpt From"

  1. In the body add the "Copy to Clipboard" step from the library

create Automator Quick Action Workflow

  1. Save the script with your desired name.

  2. Now go to the system settings to the keyboard and select tap "Shortcuts"
    6.1. On the left side select "Services" and make sure that on the ride side your scriptname is selected
    6.2. Add a new shortcut for this script.

Keyboard and Shortcut settings

  1. Your done! Now when copying Text from Books with your selected shortcut, it will remove the citation. Alternatively you can copy the text in Books also over the menu >> Books >> services >> your script name

@aoenth
Copy link

aoenth commented Oct 8, 2020

The correct code should be this, as noted above:
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' | tr -d '\n'

It doesn't work with multi-line code as all new line characters are removed by tr. I found a great solution from this link:

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' | awk '/^$/ {nlstack=nlstack "\n";next;} {printf "%s",nlstack; nlstack=""; print;}'

@jjjchens235
Copy link

sixtyonePRIME's instructions worked for me in Apr 2021. A very big help.
Don't forget to restart iBooks after setting up the shortcut in System Preferences.

@SiarheiBobryk
Copy link

SiarheiBobryk commented Nov 4, 2021

Hey folks!

This script has stopped working fine after updating to macOS Monterey.

I believe it's related to the new original output format.

“Currying is similar to partial application”

Excerpt From
Functional-Light JavaScript: Balanced, Pragmatic FP in JavaScript
Kyle Simpson
This material may be protected by copyright.

Any ideas on how to get it working with the new output format?

@majormomogram
Copy link

same here, need help!

Hey folks!

This script has stopped working fine after updating to macOS Monterey.

I believe it's related to the new original output format.

“Currying is similar to partial application”

Excerpt From
Functional-Light JavaScript: Balanced, Pragmatic FP in JavaScript
Kyle Simpson
This material may be protected by copyright.

Any ideas on how to get it working with the new output format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment