Unable to keep a specific application window on top.

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.

#!/usr/bin/env bash
screencapture -i /tmp/screenshot.png
qlmanage -p /tmp/screenshot.png &>/dev/null

@Andreas_Hegenberg

Sir, is there a way to make qlmanage.app open as the topmost window?

Try this:

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

Tested with the "Run Shell-Script" Action.

#!/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:

  1. Interactive Screenshot:

    • Uses screencapture -i to allow interactive screenshot selection, saving the result to /tmp/screenshot.png.
  2. Quick Preview:

    • Leverages qlmanage to open the screenshot immediately using macOS's Quick Look functionality, bringing the preview to the forefront.
  3. Auto-Copy to Clipboard:

    • Automatically copies the captured screenshot to the system clipboard, so it’s instantly available for pasting into documents or applications.
  4. Enhanced Performance:

    • Optimized the detection of qlmanage using AppleScript, ensuring it appears on top as quickly as possible by reducing detection delays to 1ms.
  5. 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

  1. Copy the script to a file (e.g., screenshot_tool.sh).
  2. Make it executable:
    chmod +x screenshot_tool.sh
    
  3. 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!

Thank you, sir. You can give my new script a try. I believe it is more user-friendly than third-party OCR and screenshot software.

Works fine.

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.

Thank you for your suggestion. What solution do you use for the same functionality (quick screenshot and OCR)?.

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.

I used CleanShot X, but I now believe that third-party screenshot tools are completely unnecessary.//

It depends on the use case. Shottr has a lot of useful tools like Crop, Blur/Erase, Hightliter etc. and I've been using it for years.