OCR Code Snippets on Mac: Copy Code from Tutorials and Screenshots
By Eduard Bruch, developer of Optic · Updated
To copy code that is only shown as an image, let OCR read it from the screen and then review it before you run it. Live Text works for images in Apple's apps; a screen OCR app with a code mode works anywhere and keeps the indentation.
Short answer: for code in a screenshot you can open in Preview, try Live Text. For code in videos, IDE screenshots in chat or image-only docs, my pick is Optic, a menu bar app that copies the text in any area you drag across the screen ($4.99, a one-time purchase on the Mac App Store), with a Code mode for code.
Disclosure: I build Optic.
Why code is the hardest text to retype
Prose forgives small errors; code does not. A hyphen instead of an underscore breaks an import, a missing bracket breaks a function, and a lowercase letter in the wrong place changes a variable. Long shell commands, regex patterns and config blocks are exactly what people share as screenshots, and exactly what is easiest to mistype.
Option 1: Live Text
Live Text works where Apple supports it: images in Preview, Photos, Quick Look and Safari, and paused video frames in some players. It does not work on arbitrary app windows, video calls or UI text. For a single code screenshot in Preview, selecting and copying the text is worth a try. Check the indentation after pasting, since that is where general text recognition often has trouble.
Option 2: Optic with Code mode
Optic reads code from anywhere on screen: a tutorial page, a paused video, a screenshot in Slack or a pull request comment.
- In Optic's Settings, switch on Code mode (once).
- Show the code on screen.
- Click the Optic icon in the menu bar and choose Capture Text.
- Drag a box over the snippet.
- Paste with Command-V into your editor or terminal.
What Code mode changes:
- Indentation is kept. Optic works out each line's indentation from where the text sits in the image, so nested blocks come out nested. That matters for Python and YAML.
- Language correction is off. Normal text recognition may "correct" unusual words toward real ones. For code, that would change identifiers, so Code mode turns it off.
Recognition uses Apple's Vision framework on the Mac with no internet needed, and Optic has no network access. That is useful when the screenshot shows internal code.
Typical cases
- Terminal commands in docs. Copy an install or build command from a terminal screenshot.
- IDE screenshots from colleagues. Get the code from a screenshot in chat into your editor, where you can actually read and test it. See copy text from a Slack image on Mac.
- Coding videos. Pause the video and capture the code on screen.
- Several snippets from one tutorial. Capture each block in turn; your last 20 captures stay in the menu bar history, so you can copy each one again when you reach it in your project.
Review before you run
OCR is good on clear, high-resolution code, but it can still confuse similar characters: 0 and O, 1, l and I, or straight and curly quotes, especially in small or blurry text. Read through what you pasted before running it, and let your editor's syntax highlighting point out anything odd. Do not paste OCR output straight into a shell with root rights without reading it.
Where Optic does not fit
- No file import. It does not open images or PDFs; show them on screen first.
- No batch. One capture per drag, so a 30-page PDF of code listings is a job for a document OCR tool.
- No syntax awareness. Optic recognizes characters; it does not check or format the code.
- Handwriting. Code on a whiteboard photo is not something Optic promises to read.
For more on screenshots of code, see copy code from a screenshot on Mac.
FAQ
How do I copy code from a YouTube tutorial on Mac?
Pause the video so the code is sharp, then drag over it with a screen OCR app such as Optic with Code mode on, and paste into your editor.
Does OCR keep the indentation of code?
With Optic's Code mode, yes: indentation is rebuilt from the position of each line. Without it, many OCR tools return the lines without leading spaces.
Is OCR'd code reliable enough to run?
Usually close, but check it. Similar-looking characters are the usual errors, so read the code before running it.
Does Optic send my code anywhere?
No. Recognition runs on the Mac and the app has no network access.
Get Optic on the Mac App Store · All Optic facts. Optic needs macOS 14 Sonoma or later. Comparing tools? See Optic vs TextSniper.