How to save a new jpg image from the clipboard

It seems like it should be simple but I can't find anywhere this has been documented.

I have an image on the clipboard and simply want to save it as a new image.

I've been using Image Events and I can get a file saved but it's always empty.

Can someone please post some sample code to do this?

imac 2.8, Mac OS X (10.5)

Posted on Mar 27, 2010 2:38 PM

Reply
3 replies
Sort By: 

Mar 29, 2010 8:07 AM in response to Kablooie

I'm sure there's a more elegant way to script this, but here is a script I just wrote real quick that should at least give you a start to an alternate way of dealing with clipboard data without having to launch Preview...

<pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
title="Copy this code and paste it into your Script Editor application.">
--see if clipboard is a file
set filePath to ""
try
 set clipPath to the clipboard as «class furl»
 set filePath to clipPath as alias
end try
if filePath is not "" then
 set newFile to getFileName("copied")
 do shell script "cp " & quoted form of POSIX path of filePath & ¬
 space & quoted form of POSIX path of newFile
 return --end
end if
--see if clipboard is image data
set jpegDATA to ""
try
 set jpegDATA to the clipboard as JPEG picture
end try
if jpegDATA is not "" then
 set newFile to getFileName("new")
 set theFile to open for access newFile with write permission
 write jpegDATA to theFile
 close access theFile
 return --end
end if
beep 1
display dialog ¬
 "No file or image data found on the clipboard." with icon ¬
 note buttons {"Whatever"} default button 1
on getFileName(type)
 choose file name with prompt ¬
 "Select a name and location for the " & type & ¬
 " jpeg:" default location (path to desktop) default name ¬
 type & ".jpg"
end getFileName
</pre>

Hope this helps...
Reply

Mar 27, 2010 5:30 PM in response to Kablooie

Oops… I forgot that you also wanted to save the picture. The following script should save the picture as a JPEG file on your desktop:

try
*tell application "Preview" to activate*
*tell application "System Events" to tell process "Preview"*
*keystroke "n" using command down*
*repeat until window 2 exists* -- window 1 in a hidden window
*delay 0.25*
*end repeat*
*keystroke "s" using command down*
*delay 0.25*
*keystroke "d" using command down* -- save to desktop
*tell pop up button 1 of group 1 of sheet 1 of window 2*
click -- open the pop up button menu
*click menu item "JPEG" of menu 1* -- select "JPEG"
*end tell*
*keystroke return*
*end tell*
*end try*

Hope it can help.
Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to save a new jpg image from the clipboard

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.