Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Take a screenshot of a SWF
-- Author: Michael Bianco
-- References:
on run argv
generate_window_screenshot(item 1 of argv)
end run
on generate_window_screenshot(outputPath)
set outputPath to quoted form of POSIX path of outputPath
-- get screen dimensions
copy {word 3 of (do shell script "defaults read /Library/Preferences/ | grep -w Width") as number, (word 3 of (do shell script "defaults read /Library/Preferences/ | grep -w Height") as number)} to {scrn_w, scrn_h}
tell application "System Events"
set proc to name of first process whose frontmost is true
-- flash player window border is approx 15px on the bottom & right and 22px on the top area
set crop_adjustment_top_w to 0
set crop_adjustment_bottom_w to 15
set crop_adjustment_top_h to 22
set crop_adjustment_bottom_h to 15
tell application proc to activate
tell application "System Events" to tell process proc
copy position of window 1 to {ini_x, ini_y}
copy size of window 1 to {win_w, win_h}
set adjusted_win_w to win_w - (crop_adjustment_top_w + crop_adjustment_bottom_w)
set adjusted_win_h to win_h - (crop_adjustment_top_h + crop_adjustment_bottom_h)
-- center window because the sips crop command automatically centers the cropped area in the image
set position of window 1 to {round ((scrn_w - win_w + (crop_adjustment_bottom_w - crop_adjustment_top_w)) / 2), round ((scrn_h - win_h + (crop_adjustment_bottom_h - crop_adjustment_top_h)) / 2)}
end tell
-- capture the screen, crop the image
do shell script "screencapture" & space & "-wx" & space & outputPath
if (scrn_w - adjusted_win_w) / 2 as integer is not (scrn_w - adjusted_win_w) / 2 then
-- then the image will be off 1/2 pixel since sips crops & centers the image, we have to adjust the window width a pixel
set win_w to win_w + 1
-- then the image centers on an even pixel and no blurring will occur
end if
if (scrn_h - adjusted_win_h) / 2 as integer is not (scrn_h - adjusted_win_h) / 2 then
-- then the centered image will be 1/2 off
set win_h to win_h + 1
-- pixel alignment is fine
end if
do shell script "sips -c" & space & adjusted_win_h & space & adjusted_win_w & space & outputPath & space & "--out" & space & outputPath
-- open the created screenshot
-- do shell script "open" & space & outputPath
-- revert window position
--tell window 1 of process proc to set position to {ini_x, ini_y}
end tell
end generate_window_screenshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment