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

Create a screenshot and display it immediately UNIX
I always missed the ability to create a screenshot and have the grabbed picture displayed immediately on my desktop. I couldn't find any way using AppleScript to control Preview to execute its File -> Grab menu item -- anybody? So I wrote a little csh script instead:
#!/bin/csh
rm -rf ~/grab.jpg
screencapture -i ~/grab.jpg
open /Applications/Preview.app ~/grab.jpg
To make sure I could assign a shortcut to it, I used an AppleScript to execute this shell script -- and the AppleScript was then saved as an application:
do shell script "~bin/grabscreen"
Make sure you change the path and name for the script to reflect your script's location and anme. Now you can assign a shortcut to it using your favorite shortcut tool. This will create a screengrab (by selection; press the Space Bar for window mode), and open the resulting frame in Preview. I'd love to hear any alternatives to this hoop jumping...

[robg adds: I used Butler's ability to directly run AppleScripts and assign a keyboard shortcut to test this hint. In that context, at least, I found I had to use the full path to the file (/Users/robg/bin/grabscreen), not the shortcut to my home directory, in the AppleScript.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[12,099 views]  

Create a screenshot and display it immediately | 8 comments | Create New Account
Click here to return to the 'Create a screenshot and display it immediately' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a screenshot and display it immediately
Authored by: aalegado on Jan 04, '06 08:41:34AM
IMHO: I wouldn't use
rm -rf
in the script to delete an existing screenshot file...there's no need for recursion especially since the exact file's path is known. With the '-r' (recurse all sub-directories) switch on, there is potential for disaster if the script is typed in wrong, e.g.
rm -rf ~ /grab.jpg
Note the space. With this particular typo, the script will wipe out one's home directory. Better to use
rm -f ~/grab.jpg
.

[ Reply to This | # ]
Create a screenshot and display it immediately
Authored by: kbradnam on Jan 04, '06 10:27:33AM

Actually, I don't think there is any need for the 'rm' line at all. If you specify a file for the screencapture to write to, and that file already exists, then it will just be overwritten. A safer alternative.



[ Reply to This | # ]
Create a screenshot and display it immediately
Authored by: dkulp on Jan 04, '06 01:27:39PM
No need for AppleScript to call a shell script. Just pass the commands to the shell.

Save to /tmp and use the shell's process ID to allow for multiple simultaneous grabs. /tmp will get cleaned up during the next reboot.

Use PNG for lossless screen capture. PNG is usually better for screenshots since it's often vector graphics.

grab.scpt:
do shell script "screencapture -i /tmp/grab.$$.png; open -a Preview /tmp/grab.$$.png"

[ Reply to This | # ]

Create a screenshot and display it immediately
Authored by: metiure on Jan 05, '06 05:54:33AM

great tips! thanks to all.

Vic



[ Reply to This | # ]
multiple files
Authored by: sjk on Jan 05, '06 01:11:15PM

The script could be modified to use mktemp if you want to keep multiple files instead of overwriting one.



[ Reply to This | # ]
Create a screenshot and display it immediately
Authored by: ryanerwin on Jan 05, '06 10:36:51PM

Modified this a bit to default the screenshot to window mode and to save multiple files inside a single directory. Files are named sequentially, based on time and stored in the pictures folder. Just paste it into Script Editor. I save it with a bunch of other scripts in my ~/Favorites folder and use LaunchBar to access them.

do shell script "DATE=`date '+%Y%d%m-%H%M%S'`;
FILE=~/Pictures/screenshot-${DATE}.png;
screencapture -i -W -x $FILE;
if [ -e $FILE ]; then
open /Applications/Preview.app $FILE;
fi"

Other scripts I have in that directory include:
* Activate Plantronics Headset.scpt (Great for switching to your USB headset)
* Each Email in Address Book.scpt (Handy to export address book to my SpamAssassin whitelist)
* Grab Screenshot and Show it Now.scpt (The one above)
* Maximize Frontmost Window.scpt (Integrated with iKey and CMD+SHIFT+Z)
* Foreign Entry Requirements - Visa - USA State Dept.webarchive (Decide which country I'll visit this month. Looks like Malaysia next week...)
* UNESCO World Heritage Center Sites - Map.webloc (Some amazing places to see!)
* World Clock - Time Zones.webloc (What time is it back in the states???)



[ Reply to This | # ]
Create a screenshot and display it immediately
Authored by: blafusel on Jan 06, '06 11:04:59PM

So much more elegant! Is this bash shell? Can it be changed to excute tcsh or csh instead?

Thanks for all the comments and suggestions! This is great!



[ Reply to This | # ]
Create a screenshot and display it immediately
Authored by: zottel on Jan 06, '06 04:48:46AM
Robg, using
~bin/
in the AppleScript as shown in the hint won't work because ~bin means "the home directory of the user named bin". If you want to access the directory bin in your own home directory,
~/bin/
is correct.

[ Reply to This | # ]