jonteamere Posted November 6, 2016 Share Posted November 6, 2016 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
deanishe Posted November 6, 2016 Share Posted November 6, 2016 (edited) 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 November 6, 2016 by deanishe Link to comment
jonteamere Posted November 6, 2016 Author Share Posted November 6, 2016 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
jonteamere Posted November 6, 2016 Author Share Posted November 6, 2016 @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
deanishe Posted November 6, 2016 Share Posted November 6, 2016 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
jonteamere Posted November 6, 2016 Author Share Posted November 6, 2016 Thank you for the help, @deanishe Link to comment
Oneechan69 Posted September 28, 2021 Share Posted September 28, 2021 I know this is necroposting but can someone walk me through step by step to make the script work in Alfre Link to comment
vitor Posted September 29, 2021 Share Posted September 29, 2021 (edited) Here’s a variation of what I use. Edited July 14, 2022 by vitor Updated link Link to comment
hi1234 Posted March 9, 2023 Share Posted March 9, 2023 @vitor what is the shortcut to use? Link to comment
vitor Posted March 9, 2023 Share Posted March 9, 2023 @hi1234 Open Alfred and type ibpaste. Link to comment
hi1234 Posted March 9, 2023 Share Posted March 9, 2023 @vitor and then? thanks for your reply I open alfred and type ibpaste, apply enter, then back to ibooks to copy, then paste to my notes, still not working Link to comment
vitor Posted March 9, 2023 Share Posted March 9, 2023 Copy the text in Apple Books,Go to the place where you want to paste it.Launch Alfred.Type ibpaste.Press ↩. The text is will be pasted. If you’ve copied to much at once, you’ll get a notification. Link to comment
hi1234 Posted March 9, 2023 Share Posted March 9, 2023 that works, thanks!!! but every time I need to repeat this? can i replace keyword ibpaste with a hotkey for example double command, so the process would be: Copy the text in Apple Books, Go to the place where you want to paste it. press double command(the alfred has already been launched) The text will be pasted. If you’ve copied to much at once, you’ll get a notification. Link to comment
vitor Posted March 9, 2023 Share Posted March 9, 2023 Yes, you can add the Hotkey yourself and connect it to the second object (Run Script). Link to comment
hi1234 Posted March 9, 2023 Share Posted March 9, 2023 or can i override mac's clipboard? directly use Alfred's clipboard so I won't need to repeat the whole process everytime Link to comment
hi1234 Posted March 9, 2023 Share Posted March 9, 2023 (edited) override ibooks' clipboard I have reached the maximum number of posts I can make per day. you are right, I will try to find if the automaker way works Edited March 9, 2023 by hi1234 Link to comment
vitor Posted March 9, 2023 Share Posted March 9, 2023 That would require getting funky with the copy procedure, which is something to be avoided at all costs as it can break behaviour in most apps in subtle ways. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now