Exporting Graphics and using graphics exporters

Whenever a window or graphic document is created an associated graphic exporter is also created. This graphic exporter belongs to the graphic or window document and all of the graphic exporter properties are manipulated using the graphic or window document object. A graphic importer does not create a graphic export automatically when it is created but a graphics importer can create a graphic exporter that belongs to it, and all the graphic exporter properties are manipulated through the graphic importer object.

The following line of AppleScript instructs a graphics importer to create a graphics exporter:

tell thisImporter to make exporter

The above line creates a JPEG graphics exporter because the default graphics exporter is JPEG. Many of the graphic exporter properties can be set when the graphics exporter is being created, this is a little faster as the graphics exporter is created and configured using one apple script command. The following AppleScript code segment creates and configures a TIFF graphics exporter.

tell thisImporter to make exporter with properties {export file type:"TIFF", export compression method:packbits, export folder location:(path to desktop folder), export file name:"MytiffFile.tif", export bit depth:24, export resolution:{72.0, 72.0}}

The above line configures the TIFF graphic exporter to use the packbits compression method, to set the folder where the file is to be saved to be the desktop and sets the file name. The export bit depth is set to 24 and sets the ppi (points per inch) resolution to 72. For a tiff graphics exporter the default bit depth is 24, and the default resolution is 72 so setting both these properties in the above case is unnecessary. The default value for export compression method is not packbits so if you wanted to use the packbits compression method then setting this option is necessary. The packbits compression method is incompatible with exif meta data and will be ignored if exif data is set. For more information on creating files with exif and IPTC information see the Read/Wrtie Exif metadata documentation section.

The following line of AppleScript will return a list of available graphic exporter types.

set exporterTypes to the available exporter types

If a graphic exporter has been created by a graphic importer, and the imported graphic file is JPEG and the export file type is JPEG then the graphic exporter will try and create the new image file without recompressing the data. This will only happen if there is no scaling, if the rotation is a multiple of 90 degrees. Cropping can occur as long as the dimensions of the cropped image are multiples of 8 pixels. From iMagine Photo version 2.1 the source file and destination file can be the same file. This last point is significant because it means that iMagine Photo can modify the properties of the image file like the exif metadata and resolution without having to recompress the image data. This means that iMagine Photo is signficantly faster and there is no gradual degradation of the image data that can result when processing JPEG image files in place.

The following script allows you to import a graphics file, choose what graphic type you want to export the file as, where to save the file, and to specify the exported file name. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set thisFile to choose file with prompt "Choose an image file to export: "
  
tell application "iMagine Photo"
    
set exporterTypes to the available exporter types
    
set exportType to item 1 of (choose from list exporterTypes with prompt "Choose the type you want to export your graphic as: ")
    
set theFolder to choose folder with prompt "Choose the folder where you want to save the new file: "
    
set fileName to text returned of (display dialog "The name for the new graphic file (without extension): " default answer "MyNewFile")
    
set thisImporter to import graphic thisFile
    if the component error of thisImporter is not equal to 0 then
      
close thisImporter
      
return
    end if
    
tell thisImporter to make exporter with properties {export file type:exportType, export folder location:theFolder}
    
set theExtension to the export file extension of thisImporter
    
set the export file name of thisImporter to (fileName & theExtension)
    
export thisImporter
    
close thisImporter
  
end tell
end run

Creating new image files which are scaled, cropped or rotated versions of the original can be easily done. But the scaling, cropping or rotating are all properties of the graphic importer not the graphic exporter. To learn how to do any of those operations see the importing graphics documentation.

The graphic exporter has two error properties to determine if the last operation failed or if the graphic exporter is no longer useable. For further information on handling errors see the Error handling documentation.

If you are exporting your graphic files as JPEG you can specify the compression quality or the target data size. The minimum export compression quality setting creates small jpeg files but don't look very good, the maximum setting creates a larger file that looks better. There is also a lossless setting which is better again than maximum but I am not convinced that it is actually lossless. If instead of compression quality you set the export target data size then the JPEG graphics exporter will attempt to create an image file where the image data size is approximately that specified. The JPEG graphics exporter may have to try a few times at exporting the data before it creates a file that is close to being the desired size, so this method can be slower.

When exporting from a graphic or window document there are no options for scaling, cropping or rotating. When the graphic or window document is created it must be created at the size you want to export your final image at. Any cropping scaling or rotating options must be configured for the graphics importer when drawing from a graphics importer to a graphic or window document

The following script creates a new image file that is a blend of two image files and is half the width and half the height of the first image file. The script demonstrates using a graphic exporter that is associated with a graphic or window document. Panther users can open the following script in a new Script Editor window by clicking here.

on run
  
set thisFile to choose file with prompt "Choose first image file: "
  
set thisFile2 to choose file with prompt "Choose second image file: "
  
tell application "iMagine Photo"
    
set exporterTypes to the available exporter types
    
set exportType to item 1 of (choose from list exporterTypes with prompt "Choose the type you want to export your graphic as: ")
    
set theFolder to choose folder with prompt "Choose the folder where you want to save the new file: "
    
set fileName to text returned of (display dialog "The name for the new graphic file (without extension): " default answer "MyNewFile")
    
set thisImporter1 to import graphic thisFile
    
if the component error of thisImporter1 is not equal to 0 then
      
close thisImporter1
      
return
    
end if
    
set thisImporter2 to import graphic thisFile2
    
if the component error of thisImporter2 is not equal to 0 then
      
close thisImporter2
      
return
    
end if
    
set {x, y, xDim, yDim} to the natural bounds of thisImporter1
    
set the scale of thisImporter1 to {0.5, 0.5}
    
set the top left point of thisImporter1 to {0, 0}
    
set xDim to xDim div 2
    
set yDim to yDim div 2
    
set thisDocument to make new window document with properties {dimensions:{xDim, yDim}}
    
set the drawing destination of thisImporter1 to thisDocument
    
draw thisImporter1
    
close thisImporter1
    
set the scale of thisImporter2 to {0.5, 0.5}
    
set the top left point of thisImporter2 to {0, 0}
    
set the drawing destination of thisImporter2 to thisDocument
    
set the drawing mode of thisImporter2 to {graphics mode:blend, opcolor:{32767, 32767, 32767}}
    
draw thisImporter2
    close thisImporter2
    
set the export file type of thisDocument to exportType
    
set the export folder location of thisDocument to theFolder
    
set theExtension to the export file extension of thisDocument
    
set the export file name of thisDocument to (fileName & theExtension)
    
export thisDocument
    --
close thisDocument
  
end tell
end run

For the graphics formats that support it, graphic and window documents can be exported with alpha channel (transparency) information. To learn how to do this see the Transparency documentation page.

Version 2.1 of iMagine Photo adds the ability to a graphic exporter to set the icon of the exported file to the exported image. To add an icon to an image file do:

set the export custom icon of thisImporter to true

before exporting the image.

The ability to open scripts in a new Script Editor window is provided by an application called "Convert Script to Markup Code" and can be obtained from http://homepage.mac.com/jonn8/as/

keywords: AppleScript, Apple Script, exporting images, Macintosh, Mac, Quicktime, JPEG, JP2, TIFF, Targa, PNG, Photoshop