在Automator的截屏动作中使用“捕获选定窗口”选项而非“捕获屏幕区域”。
示例代码:
tell application "System Events" set frontmostApplication to first application process whose frontmost is true set visibleWindowList to visible window of frontmostApplication set windowCount to count visibleWindowList if windowCount is not 0 then set frontmostWindow to item 1 of visibleWindowList set imageFilePath to (POSIX path of (path to desktop)) & "screenshot.png" do shell script "screencapture -l" & (id of frontmostApplication) & " " & quoted form of imageFilePath return imageFilePath else error "No windows are open" end if end tell
以上代码使用了AppleScript来实现捕获当前前景应用程序的选定窗口并将其保存为PNG文件。在Automator中将该代码用于“运行AppleScript”动作中即可实现捕获选定窗口功能。