Problem with screenshot of window with title/name

Problem with screenshot of window with title/name

PDF Capture.kmmacros (1.9 KB)

Keyboard Maestro Export

I have window in Preview application (with PDF) and want to save their screenshot to system clipboard based on window's title, but next step "Show Clipboard" displays old value put to the clipboard. I've tried to setup "Contains" and "Matching" - it doesn't work.

I've got window name by running Apple Script from this forum - are title and name the same value. If not - how can I get list of all windows title to use in the macro?

Piotr

Your macro works fine on my Mac, just as you wrote it. No problem.

Let me point out you turned off "Failure aborts macro" in your first action so that means you now need to look in your Engine.log file to see what the error is (that's why it's displaying the old value, because the first action is failing.) Do you know how to check your log file? My guess is that you don't have a window with the string "AN51G5G03" in it. Maybe you mistyped it.

THis is strange.
I create d another macro where I just display title of front window in widow, then copy the window name to previous macro - so title is exactly as seen be KM - macros to gest screenshot still doesn't work. I test it ona latest macOS Sonoma 15.5.

I've tried it wit "Windows Name" and "Window name contains".
In log I see

2025-05-26 11:15:39 Screen Capture failed to capture screen with error Screen Capture failed to find window AN51G5G01.pdf – Strona 26 z 27. Macro “PDF Capture” cancelled (while executing Screen Capture Window “AN51G5G01.pdf – Strona 26 z 27”).

image

Piotr

That's an excellent idea! And it still doesn't work? [Thinks....]

Okay, I think I know what the problem is. In order for the "window" options to work, the action requires that the frontmost app be the application that contains the window. When I did my test, that was the case, but today when I hid the window behind another one, it failed like yours.

In this case, all you have to do is activate the Preview app before your two actions.

Give that a try. I'm 80% sure it will work, but my advice isn't always right. The KM documentation doesn't say that the app must be frontmost for the window feature to work, so I'm worried that I might be wrong.

The situation is complicated :smiley:

When I Textedit, write something and save to file, Let's say 'Test.txt', the file name is also window name/title (checked by other KM macro). I've put this title to Macro and run it by hotkey:

  • Window visible on the screen, but other Textedit window selected - window found and snapshot done
  • Window not visible on the screen (hidden under other window), but other Textedit window selected - window found and snapshot done
  • Textedit not active - not found, snapshot not done.

But the same with Preview doesn't work any way - doesn't matter is it visible or not.

Anyway even contains and matching doesn't work with Preview :frowning:

Piotr

It works for me.

What type of document do you have in Preview? PDF, image?

Piotr

I was using jpg files. It didn't occur to me that doing it with PDF files might fail if jpg files worked. But now that I'm trying it on PDF files, I'm getting your symptom.

I'm not sure if the problem lies with the KM action or with the Preview app. But regardless of the source of the problem there should be a way to address it within KM. Instead of using the KM screen capture action, use the KM Execute Shell Script action with the "screencapture" macOS command in it.

I don't think the macOS "screencapture" command has a flag to let you specify "the front window" so you may have to get the coordinates of the front window using a KM token and then pass those coordinates to the macOS screencapture command. If you can't understand that, let me know and I will help you out.

Yes - I just discovered that if I have window with image (jpg) - the script works!

I will try your solution later - anyway thanks for help

Piotr

I've opened 3 windows in Preview.app - one with jpg and two with pdf.

Next I use Apple Script as below to see windows names:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/09/17 17:11
# dMod: 2021/09/17 17:11 
# Appl: System Events
# Task: List All Windows of All Applications in the Current Desktop (Space).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @List, @Windows, @Desktop
--------------------------------------------------------

tell application "System Events"
	tell (application processes whose background only is false)
		set appNameList to name
		set appWindowNameList to name of windows
	end tell
end tell

set _cntr to 1

repeat with appName in appNameList
	set contents of appName to {"# " & appName & " #" & linefeed} & item _cntr of appWindowNameList & ""
	set _cntr to _cntr + 1
end repeat

set AppleScript's text item delimiters to linefeed

return appNameList as text

What I see in section for Preview.app is:

# Preview #

BBEdit User Manual (15.1.4).pdf – Strona 336 z 450
Aneks1.pdf – 1 strona
IMG_0072.JPG

and only JPG window us serviced by macro.

Maybe @peternlewis looks into the problem - the question is”What see an application in window name?".

Piotr

That question is going to need to be rephrased for me to answer it.

The FrontWindowName token is the result of kAXTitleAttribute from the accessibility object for the window.

This is the same title used by the window with title matching.

I presume Preview is actually at the front both times, because only the front application windows are available to the action.

Yes, in both cases (pdf/image), application is in the front.

But today I discovered thet when I change the macro step to Screen Capture Front Window, next select window Preview with PDF. and run macro with assigned key - I see previous clipboard value, not current window screenshot. When I do the same with Preview window with JPG file opened, I see screenshotwith that image.

image

The log entries for the action with PDF:

025-05-27 09:00:52 Execute macro “PDF Capture with name” from trigger The Hot Key ⌃⇧⌘M is pressed
2025-05-27 09:00:52 HScreenArea::CaptureCGImage failed to capture front window
2025-05-27 09:00:52 Action 15803975 failed: Screen Capture failed to capture screen with error failed to capture front window
2025-05-27 09:00:52 Screen Capture failed to capture screen with error failed to capture front window. Macro “PDF Capture with name” cancelled (while executing Screen Capture Front Window).
2025-05-27 09:00:58 Execute macro “StreamDeck” from trigger The Focused Window Title Changes
2025-05-27 09:00:58 Execute macro “Move window” from trigger The Focused Window Title Changes

Piotr

It seems to be a general problem screenshotting PDF documents in Preview -- this also fails:

image

...but works fine with a PNG open.

For now, try working around it with a shell script action that targets the frontmost window of Preview:

image

...changing that first action's "matching" text to bring the correct window to the front.

The shell script is:

screencapture -c -l$(osascript -e 'tell app "Preview" to id of window 1')
1 Like