Skip to content

Instantly share code, notes, and snippets.

@acsr
Created November 21, 2024 11:27
Show Gist options
  • Save acsr/d557cfa2955750fa72aea499574b393f to your computer and use it in GitHub Desktop.
Save acsr/d557cfa2955750fa72aea499574b393f to your computer and use it in GitHub Desktop.
Screenshot Selektion als Text in Zwischenablage.sh Automator Script (wip)
# Automator Action (Schnellaktion) using a shell script:
# Name "Screenshot Selektion als Text in Zwischenablage"
# V1.0.3 2012 - 20240825_144319-acsr needs tesseract installed using Homebrew
# https://support.apple.com/de-de/guide/automator/welcome/mac
# To install homebrew visit https://brew.sh/ this may differ by processor platform Intel or Apple Silicon / M, you may need to install the free XCode from Apple as well.
# Beware: If you do not have access to an Administrator account on you mac, ask your staff.
# Using the Shortcut does not need admin privileges but the install!
# Terminal install command for tesseract and languages: `brew install tesseract tesseract-lang`
# current versions: tesseract 5.4.1, tesseract-lang 4.1.0
# in Automator add a new Workflow Type Action
# Settings:
# Workflow receives: `no input` in `every Application`
# In Library search for step `shell script` and add it
# Setting
# Shell: `bin/bash`
# Leave `Input` preset as is: `to stdin`
# Paste this whole script text to the script area.
# Save the workflow as: "Screenshot Selektion als Text in Zwischenablage"
# You may need to give Automator and / or Terminal Permissions to record the Screen!
# The macOS will Display Erropr Dialogs regarding this!
# Add a Keyboard Shortcut to this Automator Action command using "System Preferences -> Keyboard -> section: General" We use `⌃⎇⌘↑` (Control-Alt-Command-CursorUp)
# How does it work?
# - Start the screencapture command to grab a screen section marked with the mouse as rectangle
# - Save it as temporary image file
# - Run `tesseract` OCR on the image using the Latin Script system.
# - This performs better than using just german and covers diacritic letters of a lot of latin languages
# - Use the `tr`command to remove obsolete output
# - Use `iconv` command to convert the output encoding from UTF-8 to MacRoman for the Finder
# - Tweak the `iconv` pipe command parameters if needed
# - Use the `pbcopy` command to copy the result into the clipboard.
#say Bitte Textbereich mit Mausrahmen markieren
screencapture -i /tmp/tesseract.png && /opt/homebrew/bin/tesseract -l script/Latin /private/tmp/tesseract.png stdout|tr -d //f | iconv -f UTF-8 -t MACROMAN | pbcopy
#say fein gemacht! Du hast nun folgenden Text in der Zwischenablage: `pbpaste`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment