Resize image to clipboard?

Hi,
I often copy images from DT into other applications and almost always I want to paste the image in a smaller size or scale than the original image. I do this manually, but since the scale I want is usually the same it would be very handy if it could be done with a script.

I have seen this can be done to the image file, but I don’t know how it would be done to the clipboard without affecting the file?

You might write a script that

  • checks to see if the current clipboard contains an image
  • if not, fail with “sorry, can’t proceed”
  • if yes, then "Tell ‘Image Events’ to modify the image dimensions of contents of the clipboard [probably load the clipboard into an image variable, modify that, then load the result back into the clipboard]

It seems it is not possible to handle images on applescript clipboard because they aren’t recognized as images. Instead I tried to duplicate the file into TemporaryItems and tell Finder to copy that (found some examples), but the file doesn’t get duplicated.

Everything works (tmp folder gets created) until the duplication, which fails.


tell application id "com.devon-technologies.thinkpro2"
    set itemList to selection
    if itemList is {} then
        tell application "System Events" to display dialog "Select something." buttons "OK" default button "OK"
    else
        repeat with anItem in itemList
            set DT_file to the path of anItem
            set DT_name to the name of anItem
        end repeat
    end if
end tell

tell application "Finder"
    make new folder at (path to temporary items) with properties {name:"tmp_DTImageExport"}
    set tmp_Folder to (POSIX path of ((path to temporary items))) & "tmp_DTImageExport/"
    set tmp_File to tmp_Folder & DT_name
    set tmp_Folder to POSIX file ("\"" & tmp_Folder & "\"") as alias
    set DT_file to POSIX file ("\"" & DT_file & "\"") as alias
    duplicate DT_file to tmp_Folder
end tell

set the target_length to 10
try

 tell application "Image Events"
 -- start the Image Events application
 launch
 -- open the image file
 set this_image to open tmp_File
 -- perform action
 scale this_image to size target_length
 -- save the changes
 save this_image with icon
 -- purge the open image data
 close this_image
 end tell
on error error_message
 display dialog error_message
end try

tell application "Finder"
    select files of tmp_Folder
    activate
    tell application "System Events" to keystroke "c" using command down
    delete tmp_Folder
end tell

Managed to get it working. For anyone interested:


try
tell application id "com.devon-technologies.thinkpro2"
    set itemList to selection
    if itemList is {} then
        tell application "System Events" to display dialog "Select something." buttons "OK" default button "OK"
    else
        repeat with anItem in itemList
            set DT_file to the path of anItem
            set DT_name to the name of anItem
        end repeat
    end if
end tell

tell application "Finder"
    make new folder at (path to temporary items) with properties {name:"tmp_DTImageExport"}
    set tmp_Folder to (POSIX path of ((path to temporary items))) & "tmp_DTImageExport/"
    set tmp_File to tmp_Folder & DT_name
    set tmp_Folder to POSIX file tmp_Folder as alias
    set DT_file to POSIX file DT_file as alias
    duplicate DT_file to tmp_Folder
end tell

set the target_length to 100

 tell application "Image Events"
 -- start the Image Events application
 launch
 -- open the image file
 set this_image to open tmp_File
 -- perform action
 scale this_image to size target_length
 -- save the changes
 save this_image with icon
 -- purge the open image data
 close this_image
 end tell

tell application "Finder"
    select files of tmp_Folder
    activate
    tell application "System Events" to keystroke "c" using command down
    delete tmp_Folder
    quit
end tell

on error error_message
 display dialog error_message
end try