I have a simple one-line shell script that I use to OCR a PDF file (or re-do the OCR if the existing layer is bad).
Right now this means navigating to the file in Terminal, remembering (or looking up) the command, pasting or typing in the target file name and then typing in the destination file path. It isn’t hard, but I’d love to automate it outside the command line, with a default destination path (e.g., in the same folder with the existing filename but appending ocrYYYYMMDD or similar).
Any suggestions for the best approach? A service? Is there a way to drag-and-drop a file onto something? I could easily use Keyboard Maestro, Hammerspoon, AppleScript python or any number of other tools. Learning Swift to create an actual app seems like overkill.
Depending on the complexity of the terminal code and/or input choices you desire, put the shell script inside an AppleScript. Use Automator with the tab “Run Shell Script”. Put a pointer to the AppleScript inside. Save in the ~/Library/Services folder. You can then context click on the file to find the service and run it. And, since the shell call is encased in an AppleScript, you can select files and run the AppleScript on them.
Here is my example to crop margins on documents to 1 inch all around. I have a separate AppleScript that prompts with a dialog to take a setting value to crop the margins.
(*
crop PDF document margins
author: jjw
version 1.1
-- sets margins to 1 in all around
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property pdfcropi : "/usr/local/bin/pdf-crop-margins -gsp '/usr/local/bin/gs' "
property pdfcropf : "-p 0 -a -72 -mo -o "
on run {}
tell application "Finder" to set theFileList to selection
if theFileList is {} then return
if (count of items in theFileList) > 1 then
set pf to "-pf "
else
set pf to ""
end if
set pdfcrop to pdfcropi & pf & pdfcropf
repeat with theFile in theFileList
if the kind of theFile is "PDF document" then
tell application "Finder" to set theFileAlias to theFile as alias
set theFilePath to POSIX path of theFileAlias
set theFileDir to my get_sourceFolder(theFilePath) & "/"
set theSource to the quoted form of theFilePath
set theFolder to the quoted form of theFileDir
set pdfCMD to pdfcrop & theFolder & " " & theSource
set theResult to do shell script pdfCMD
end if
end repeat
end run
on get_sourceFolder(theFilePath)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set theReturn to (text items 1 through -2 of theFilePath) as text
set AppleScript's text item delimiters to tid
return theReturn
end get_sourceFolder
Use an Automator Quick Action with a shell script to handle the OCR process, just right-click a file and run it. Set it to save the output in the same folder with your preferred naming format. If you want drag-and-drop, a simple AppleScript wrapper could work too. Keyboard Maestro is also great if you want more flexibility!
An embarrassment of riches. Thanks all! I’ll need to decide how important it is that the approach work on older OSes (which could rule out Shortcuts), and how much ease of implementation matters — l’ll probably be sharing this with others, some of whom may not be as tech savvy.
All of which is to says, this is great feedback. Thank you!