File Conversion
When working with images, it is very common to make duplicates in various file formats and to convert images between formats. The save command in Image Events can accomplish this easily.
From the Image Events scripting dictionary:
save v : Save an image to a file in one of various formats
save (reference) : the object for the command
[as (BMP/JPEG/JPEG2/PICT/PNG/PSD/QuickTime Image/TIFF)]: file type in which to save the image (default is to make no change)
[icon (boolean)]: Shall an icon be added? (default is false )
[in (disk item)]: file path in which to save the image, in HFS or POSIX form
[PackBits (boolean)]: Are the bytes to be compressed with PackBits? (default is false, applies only to TIFF)
--> alias reference
The supported file formats for reading an image with the Image Events application are: PICT, BMP, QuickTime Image, GIF, JPEG, MacPaint, JPEG2, SGI, PSD, TGA, Text, PDF, PNG, and TIFF.
The supported file formats for saving an image with the Image Events application are: JPEG2, TIFF, JPEG, PICT, BMP, PSD, PNG, and QuickTime Image.
IMPORTANT: when saving image data in a format other than its original format, always save the image data to a new file, do not attempt to save the image data into the existing source file.
Here’s a script that relies on user-interaction to convert a chosen image file to a TIFF image:
set this_file to choose file without invisibles
set the target_file to (choose file name default name "newimage.tif")
-- convert file reference in alias format to path string
set the target_path to the target_file as Unicode text
try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- save in new file
save this_image as TIFF in target_path with icon
-- purge the open image data
close this_image
end tell
on error error_message
display dialog error_message buttons {"Cancel"} default button 1
end try
Note in the previous script that the reference to the destination file must be converted to an HFS path string or POSIX path, as the in parameter of the save command will not accept file references generated by methods other than its own Disk-Folder-File suite.
Here’s a script that “converts” an image to another format by saving a copy of the image in the same parent directory as the source image and then deleting the original image. Note that the derive_filename sub-routine is particularly useful for determing the name for the new file based on the original filename using a different name extension, such as tif, jpg, etc. Use it in your scripts when you need to create or save a file which may have pre-existing copies in the destination folder.
Be sure to change the value of the new_format and nme_ext properties to the your desired file format and name extension.
set this_file to choose file without invisibles
try
tell application "Image Events"
-- start the Image Events application
launch
set the new_format to TIFF
set the nme_ext to "tif"
-- derive new name for the new image file
copy my derive_filename(this_file, nme_ext, "-", "") to {new_name, target_HFSpath}
-- open the image file
set this_image to open this_file
-- save in new file. The result is a file ref to the new file
set the new_image to save this_image as new_format in file target_HFSpath with icon
-- purge the open image data
close this_image
end tell
tell application "Finder"
-- delete the original file
delete this_file
end tell
on error error_message
display dialog error_message buttons {"Cancel"} default button 1
end try
on derive_filename(this_item, new_extension, increment_separator, target_folder)
-- a sub-routine used for deriving the name and path of a new file using the name of an existing file
-- Pass in file ref in alias format, the new name extension, an increment separator, and any target directory (in alias format)
-- Name and HFS path for new file are returned. The name is incremented if a file exists in the target location.
-- Pass a null string for the target directory to use the item's parent directory
-- Pass a null string for the new name extension to use the item's current name extension
tell application "Finder"
if target_folder is "" then
set the target_folder to the container of this_item
end if
set the file_name to the name of this_item
set file_extension to the name extension of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
set extension_separator to ""
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
set extension_separator to "."
end if
if the new_extension is "" then
set target_name to file_name
set target_extension to file_extension
else
set target_extension to new_extension
set target_name to (the trimmed_name & extension_separator & target_extension) as Unicode text
end if
if (exists document file target_name of target_folder) then
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & increment_separator & (name_increment as Unicode text) & extension_separator & target_extension) as Unicode text
if not (exists document file new_name of the target_folder) then
set the target_HFSpath to ((target_folder as Unicode text) & new_name)
return {new_name, target_HFSpath}
else
set the name_increment to the name_increment + 1
end if
end repeat
else
set the target_HFSpath to ((target_folder as Unicode text) & target_name)
return {target_name, target_HFSpath}
end if
end tell
end derive_filename
You can use a variation of the previous file conversion script to create a thumbnail image from a source image for use on a website. Note that this script uses the derive_filename sub-routine that is contained in the previous example.
set this_file to choose file without invisibles
try
tell application "Image Events"
-- start the Image Events application
launch
set the new_format to PNG
set the nme_ext to "png"
set the thumb_size to 128
-- derive new name for the new image file
copy my derive_filename(this_file, nme_ext, "-", "") to {new_name, target_HFSpath}
-- open the image file
set this_image to open this_file
-- scale to thumnail size
scale this_image to size thumb_size
-- save in new file. The result is a file ref to the new file
set the new_image to save this_image as new_format in file target_HFSpath with icon
-- purge the open image data
close this_image
end tell
tell application "Finder"
-- delete the original file
delete this_file
end tell
on error error_message
display dialog error_message buttons {"Cancel"} default button 1
end try