2

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
Tyler Mullins
  • 77
  • 3
  • 10
  • I just did a script for something very similar. I didn't bother to give the full path name for "screencapture" and it worked just fine. – Isaac Rabinovitch Feb 05 '18 at 04:51

4 Answers4

4

The proper way to pass the path above is by using quoted form of:

on run
    set theDesktop to POSIX path of (path to desktop as string)
    set theCurrentDate to current date
    set shellCommand to "/usr/sbin/screencapture " & quoted form of (theDesktop & "Screen Shot" & theCurrentDate & ".png")
    do shell script shellCommand
end run
adayzdone
  • 11,120
  • 2
  • 20
  • 37
  • 1
    This should have been the accepted answer: much more elegant way also. (Note the blank space at the end of `"/usr/sbin/screencapture "` since Terminal separates variables using blank spaces) – rvrvrv Apr 23 '18 at 12:20
3

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.

Entropia
  • 154
  • 9
1

I just use a shell command like this:

screencapture -i ~/Desktop/$(date +%Y%m%d%H%M%S).png

-i is interactive mode (like ⇧⌘4). The default file name format is like this on my installation:

date '+Screen Shot %Y-%m-%d at %-H.%M.%S %p.png'

See man screencapture and man strftime.

If you use AppleScript, the run handler is not needed, /usr/sbin/ is on the path by default, and you can escape arguments with quoted form of.

"Screen Shot " & (current date) & ".png"
do shell script "screencapture ~/Desktop/" & quoted form of result

If the file name looks like Screen Shot Wednesday, May 29, 2013 4/47/15 AM.png in Finder, it's because HFS uses colon as a pathname separator. : in shells appears as / in Finder and vice versa.

Lri
  • 26,768
  • 8
  • 84
  • 82
1

Emulate your keyboard:

-- Take Screenshot
tell application "System Events"
    -- Shift-Command-3
    key code 20 using {shift down, command down}
end tell

key code 20 is the same as number 3

Result:

Screen Shot 2018-11-18 at 12.47.39 pm

Complete list of key codes can be found here:

https://eastmanreference.com/complete-list-of-applescript-key-codes

320up
  • 31
  • 2