I am currently using the following script (bound to a side key) to perform OCR, and it functions properly. The only issue is that the qlmanage application does not open in the top-level window, as shown in the image. It defaults to open, but gets obstructed by other windows. I would like to add a feature to make the qlmanage window stay on top after running this script, but the built-in option in BTT to activate a specific window is not working.
qlmanage -p /tmp/screenshot.png >/dev/null 2>&1 &
osascript <<EOF
tell application "System Events"
set timeoutDate to (current date) + 3
repeat while ((current date) < timeoutDate)
delay 0.01
if ((name of processes) contains "qlmanage") then
tell application process "qlmanage"
set frontmost to true
end tell
exit repeat
end if
end repeat
end tell
EOF
#!/usr/bin/env bash
# Save the screenshot to a specific path
screencapture -i /tmp/screenshot.png
# Use qlmanage to open the screenshot and bring it to the front
qlmanage -p /tmp/screenshot.png >/dev/null 2>&1 &
# Automatically copy the screenshot to the clipboard
osascript <<EOF
set imageFile to POSIX file "/tmp/screenshot.png"
set theClipboard to (read imageFile as JPEG picture)
set the clipboard to theClipboard
EOF
# Optimization: Quickly detect qlmanage and set it to the frontmost window
osascript <<EOF
tell application "System Events"
set timeoutDate to (current date) + 2 -- Shorten the timeout period
repeat while ((current date) < timeoutDate)
delay 0.001
if ((name of processes) contains "qlmanage") then
tell application process "qlmanage"
set frontmost to true
end tell
exit repeat
end if
end repeat
end tell
EOF
Features and Explanation
This script streamlines the macOS screenshot and preview process with additional features. Here’s what it does:
Interactive Screenshot:
Uses screencapture -i to allow interactive screenshot selection, saving the result to /tmp/screenshot.png.
Quick Preview:
Leverages qlmanage to open the screenshot immediately using macOS's Quick Look functionality, bringing the preview to the forefront.
Auto-Copy to Clipboard:
Automatically copies the captured screenshot to the system clipboard, so it’s instantly available for pasting into documents or applications.
Enhanced Performance:
Optimized the detection of qlmanage using AppleScript, ensuring it appears on top as quickly as possible by reducing detection delays to 1ms.
Streamlined Workflow:
Ideal for quick screenshot previews, OCR tasks, or scenarios where you need the screenshot readily available for editing or sharing.
How to Use
Copy the script to a file (e.g., screenshot_tool.sh).
Make it executable:
chmod +x screenshot_tool.sh
Run the script when you need to take and process screenshots:
./screenshot_tool.sh
This tool is perfect for users who frequently use screenshots, especially if they need to:
Quickly preview screenshots.
Use OCR capabilities with macOS’s built-in tools.
Copy screenshots to the clipboard automatically.
Feel free to share this with others who can benefit from a faster and more efficient screenshot workflow!
PS: The problem was that &>/dev/null does not prevent the script from stopping while Quick Look is displayed. You need to use >/dev/null 2>&1 & to continue the script. Maybe the BTT actions would have worked as well, but it's faster this way.
I use Shottr. One of the few tools (such as PopClip) that I have installed alongside BTT and not replaced. I have stored shortcuts in Shottr for the most important things, which I then send via BTT, as I like to use F-keys for such things.
I use Quick Look in some of my scripts. That's why I knew the problem.