Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to create uniquely-named screenshots Desktop
There are a few hints to manage screenshots, such as this one, but with the advent of the command-line screencapture command, this doesn't need to be so hard or complicated. What was I trying to do? I wanted to...
  1. Save each screenshot with a unique name
  2. Save the screenshot into ~/Pictures/
It turns out that this is simple to do with a shell script, which I made into an application using Script Editor and launch using LaunchBar.

This AppleScript will prompt you (as does Shift-Command-4) to select some area to be captured. When captured, it will be saved to your user's Pictures folder, with the name as "Screenshot YYYY-MM-DD_hour.minute.second.png," and opened in Preview.app.

[robg adds: This previous hint used a script to grab a shot and open it immediately. This version adds the unique naming convention and changes the save location, and the script worked as described in my testing.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,525 views]  

A script to create uniquely-named screenshots | 7 comments | Create New Account
Click here to return to the 'A script to create uniquely-named screenshots' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to create uniquely-named screenshots
Authored by: capitalj on Oct 05, '06 08:16:29AM

Thanks, luomat, for thoroughly commenting the script.





[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: mm2270 on Oct 05, '06 11:53:37AM

Nice script! Works just as expected.
The only odd thing, which is really no big deal, is that if you hit Escape after invoking it, to exit out of the screengrab crosshairs, you get an error from the script, presumably because it wasn't designed to handle cancels, so it can't locate the file it 'thought' it created.
As I said, no biggie, but just thought you should be aware of that.



[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: brucio on Oct 05, '06 04:35:13PM

Nice! Thanks for this.



[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: beccles on Oct 05, '06 06:01:07PM
To make it easier to change the fileformat, I added a variable
EXTENS="jpg"

So that FILENAME becomes
FILENAME="Screenshot $TIME.$EXTENS"

and the screencapture arguments become
-t$EXTENS

The original script suggested changing the file name extension would change the fileformat, but the command argument had -tpng. I don't know that changing the file extension would override the png argument ??

[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: macsadmn on Oct 06, '06 09:25:22PM

luomat - Thanks for the script! I've been wanting to do something like this for a long time, but didn't know how to go about it. I've always wanted to be able to name my screen shots at the time they are taken. Your script really got my wheels to turning... So, I modified it a bit and hope you don't mind. Below is my AppleScript "tweak" on your already well written script.

AppleScript starts below this line:


-- This script was written by user macsadmn on macosxhints.com and is a
-- modification of a script originally designed by luomat on macosxhints.com

-- Prompt for the new screenshot name
display dialog "Name for screenshot: " default answer "" buttons {"OK"} default button 1

-- assign the variable pic_name the string value entered by the user
set pic_name to text returned of the result

do shell script "

# If you want to change the filename format,
# do so by changing the .png to some other extension. Also note,
# that if you change the extension, then you need to
# change the format from PNG
# using the -t flag of screencapture (next to last line).

FILENAME=\"" & pic_name & ".png\"

# This is where files will be moved to
TARGET_DIR=\"$HOME/Desktop\"

# Line 1: Like cmd+shift+4, offer a crosshairs to select what will be captured
# and save it as a PNG
# Line 2: By default, the file gets saved to the top level of
# the users home directory. So, since we want it on
# the desktop, we move it to the TARGET_DIR as defined above.
# We use mv -n to NOT overwrite existing files.

/usr/sbin/screencapture -tpng -i \"$FILENAME\" &&
/bin/mv -n \"$FILENAME\" \"$TARGET_DIR/\"
"



[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: hypnopixel on Oct 07, '06 01:14:24PM

some refinements... now, less error-prone!

do shell script operates at working directory / so giving the screencapture command just a filename without a path saves it to boot root /filename which is considered sub-optimal.

eliminate the mv and ambiguity by specifying the full pathname of the image file to screencapture.






-- msb - manage screenshots better; save named screenshots somewhere other than the desktop

-- prompt for screenshot name; cancel stops the script from continuing

display dialog "Name your screenshot?" default answer "" buttons {"OK", "Cancel"} default button 1

set picName to text returned of the result

-- this just adds a . delimeter after the picName, if supplied

if (picName is not "") then set picName to picName & "."

do shell script "

# timestamp to make unique filename; format is like Sat.07.Oct.06_12.44.15

timestamp=$(/bin/date \"+%a.%d.%b.%y_%H.%M.%S\")

# screencapture -t pic type and filename extension; default is png

fileExtension=png

# save screenshots here

targetDir=\"$HOME/Pictures/screenshots\"

# if targetDir is missing, make it

if [ ! -d $targetDir ]; then /bin/mkdir $targetDir; fi

# craft the full /path/to/image/filename

filename=\"$targetDir/" & picName & "screenshot.$timestamp.$fileExtension\"

# emulate command-shift-4 screenshot function in interactive mode; see man screencapture

/usr/sbin/screencapture -i -t$fileExtension \"$filename\"

# open the image with preview.app; check for file in case the user cancels the screencapture with escape

if [ -f $filename ]; then /usr/bin/open -a preview \"$filename\"; fi
"

-- end




[ Reply to This | # ]
A script to create uniquely-named screenshots
Authored by: hypnopixel on Oct 07, '06 01:26:18PM

ack! regrets, variables $targetDir and $filename in the above if statements should be quoted \"foo\" like the others to be bullet proof.



[ Reply to This | # ]