Search code examples
safariapplescriptscreenshotkeystroke

Screen capture with AppleScript


Working on a AppleScript that opens a webpage in Safari and makes a screen capture of a specific area (using Cmd+Shift+4). But I'm having trouble making the screen capture part automatic.

I'm using MouseTools to click and move the mouse around.

set x to POSIX file ((POSIX path of (path to me)) & "/.." & "/MouseTools") as text
set pathName to POSIX path of x

tell application "Safari" to activate
open location "http://google.com"
delay 1
#do shell script quoted form of pathName & " -x 500 -y 167"
delay 1
tell application "System Events"
    #keystroke "3" using {command down, shift down}
end tell
delay 1
#do shell script quoted form of pathName & " -leftClick"

etc...

The keystroke command is working for everything BUT the screen capture. Next problem is moving the mouse while holding leftclick down. Has somebody got this to work or is there a better approach?


Solution

  • I'm happy to see you're using MouseTools. That's my program! However there is a command line tool to take screen shots called screencapture. I'm sure you should be using that instead of your gui approach. For example, here's how to get a picture of your whole desktop...

    set imagePath to (path to desktop as text) & "screenCapture.png"
    do shell script "screencapture -mx -T0 " & quoted form of POSIX path of imagePath
    

    I'm sure there's ways to capture just a certain area of the screen using screencapture. A little searching of its man page or google should turn up something on that.