2

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.

4
  • How did you create the linked image in your OP? Commented Apr 23, 2019 at 16:44
  • I took a screenshot. Then took a screenshot of that screenshot. Then a screenshot of the screenshot of the screenshot. I just want to do that same process with code.
    – tuf
    Commented Apr 23, 2019 at 16:50
  • What scripting have you attempted so far?
    – Solar Mike
    Commented Apr 23, 2019 at 16:56
  • @tuf you should show that script in your original post, formatted then people may follow it easier.
    – Solar Mike
    Commented Apr 23, 2019 at 17:32

1 Answer 1

3

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

4
  • Thank you @CJK .Do you know if there's any way of naming each new file with the date+time?
    – tuf
    Commented Apr 23, 2019 at 22:19
  • Done.­­­­­­­­­­­
    – CJK
    Commented Apr 23, 2019 at 23:34
  • Hi @CJK I'm having trouble trying to run the code in another computer. I already set the accessibility privileges on but the code is just opening the selected image and then nothing else happens. Do you have any idea of what may be wrong?
    – tuf
    Commented Oct 3, 2019 at 20:14
  • @tuf "Nothing else happens" isn't something I can diagnose easily. Something does happen, you just need to articulate with more precision so I can get clues to formulate possible problems and potential fixes. So... What happens after the selected image opens (in QuickLook, I presume) ? Does the script terminate ? If so, does it exit cleanly or with an error message ? Does the script continue to run indefinitely ? Also of importance: how are you running the script to determine what's happening ? If the answer isn't Script Editor, then those questions will be difficult to answer.
    – CJK
    Commented Oct 12, 2019 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .