MacOS – Take screenshot of screenshot with AppleScript

applescriptmacosscreen capture

I want to open an image on the desktop with the 'Quick Look', then screenshot it, and then open the new screenshot to take another one of it. In resume, I want to know how to screenshot an image that has just been screenshoted, endlessly. How can I do it on AppleScript?

I know it sounds kind of weird, but its for artistic purposes. The resultant image is a feedback of the image itself being screenshoted. Also, it is important that the image is being opened as 'quick look' because it displays the top bar with the date and time of the screenshot, so the feedback is clear. Thank you so much.

(This image is a reference I made myself with this intention. Is exactly what I'm trying to automate]1]1

//The script I've accomplished by now is:

tell application "Finder"
    do shell script "qlmanage - p /cm.jpg"
    set dFolder to "~/Desktop/screencapture/"

    do shell script ("mkdir -p " & dFolder)

    set i to 0
    repeat 960 times
        set tTime to do shell script "date +%H%M%S"
        do shell script ("screencapture " & dFolder & "frame-" & i & ".png")
        delay 5
        set i to i + 1
    end repeat
end tell

// I still don't know how to make it work related to a specific image, and then related to each new screenshot.

Best Answer

As with any script that tries to control the UI, this script is brittle and relies on sufficient delays to give the Quick Look window time to form and the image to be painted. Although the script does check for the window's existence, it cannot check to make sure that the contents of the window has been painted, so I have had variable results in testing, including some good successes:

tell application "Finder"
    if selection = {} then return
    try
        make new folder with properties {name:"Infinity"}
    end try
    set sourcefile to duplicate item 1 of (get selection) to folder "Infinity"
    select the sourcefile
    activate
end tell

repeat with i from 1 to 50
    tell application "System Events"
        keystroke space
        set _W to (a reference to window "Quick Look" of process "Finder")
        repeat until _W exists
            delay 0.5
        end repeat

        delay 1 -- Give time for window to paint

        set [x, y] to _W's position
        set [w, h] to _W's size

        set my text item delimiters to ","
        set R to {x, y, w, h} as text
    end tell

    set my text item delimiters to {".", ":"}
    set filename to "screen shot " & ((current date) as «class isot» as string) & ".jpg"
    set filename to text items of filename as text

    do shell script "screencapture -R " & R & " -t jpg ~/Desktop/Infinity/" & ¬
        (quoted form of the filename)

    tell application "System Events"
        keystroke space
        repeat until not (_W exists)
            delay 0.5
        end repeat
    end tell

    tell application "Finder" to select file filename in folder "Infinity"
end repeat

You must ensure the necessary accessibility privileges are granted for this script to run.


Note: This script was written and tested using AppleScript version: 2.7 System version: 10.13.6

Script Result