Search code examples
applescriptscreenshot

Take a screen shot and save to desktop with current time as the name


I am trying to make a script that will take a screen shot, save the image to the desktop, and name it the date. Similar to what would happen if I used cmd + shift + 3. The only issue is the name of the image is only "Screen" instead of the entire name I specified. Anyone know how to fix this?

on run
    set theDesktop to POSIX path of (path to desktop as string)
    set theCurrentDate to current date
    set shellCommand to "/usr/sbin/screencapture " & theDesktop & "Screen Shot" & theCurrentDate & ".png"
    do shell script shellCommand
end run

Solution

  • Put the complete file path in double quotes, like this:

    on run
        set theDesktop to POSIX path of (path to desktop as string)
        set theCurrentDate to current date
        set shellCommand to "/usr/sbin/screencapture \"" & theDesktop & "Screen Shot" & theCurrentDate & ".png\""
        do shell script shellCommand
    end run
    

    The file name contains white space, hence, in your version, the command line interprets it as multiple arguments to /usr/sbin/screencapture.