Drawing Shapes

All shape drawing is done using the create composition element command and is applied to a particular graphic document. Many different types of composition elements are possible, both filter effects and shape drawing. Each composition element type is described on the Composition Elements 2 page.

This page is the example page for composition elements. The first example draws a red rectangle which is the same size as the window document bounds. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
set redColour to {65535, 0, 0}
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
tell thisDocument to create composition element with properties {class:filled rectangle, bounds rectangle:{0, 0, 800, 600}, color:redColour}
  
end tell
end run

The second example draws a green oval and uses quartz to do the drawing. Quartz drawing is anti aliased which produces smoother looking edges to the oval that is drawn. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
set greenColor to {0, 65535, 0}
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
tell thisDocument to create composition element with properties {class:filled oval, bounds rectangle:{0, 0, 800, 600}, color:greenColor, drawing type:quartz}
  
end tell
end run

The third example draws a green oval using quickdraw to do the drawing. One of the things that quickdraw drawing can do that quartz drawing can't is the ability to draw the background at the same time. This example draws the background blue, the background for drawing ovals is the area in the bounds rectangle not covered by the oval. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
set greenColor to {0, 65535, 0}
  
set blueColor to {0, 0, 65535}
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
tell thisDocument to create composition element with properties {class:filled oval, bounds rectangle:{0, 0, 800, 600}, color:greenColor, drawing type:quickdraw, background color:blueColor}
  
end tell
end run

The same effect could have been achieved by drawing a blue rectangle first and then drawing the oval. The background color option is most useful when drawing text.

The fourth example draws 4 rectangles, red, green, cyan, and blue, and then 4 ovals. The two ovals that are drawn last are partially transparent, the green one being drawn using quartz drawing and taking advantage of quartz's opacity option while the blue oval is drawn using quickdraw and takes advantage of quickdraws powerful blend graphics mode and opcolor. Also note that only one composition element command is performed as each composition element command can have as many composition element records as you require. Drawing shapes in this way is much faster than drawing individual composition elements. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
set redColor to {65535, 0, 0}
    
set greenColor to {0, 65535, 0}
    
set blueColor to {0, 0, 65535}
    
set cyanColor to {0, 65535, 65535}
    
set commandList to {}
    
copy {class:filled rectangle, bounds rectangle:{0, 0, 400, 300}, color:redColor} to end of commandList
    
copy {class:filled rectangle, bounds rectangle:{400, 0, 800, 300}, color:greenColor} to end of commandList
    
copy {class:filled rectangle, bounds rectangle:{400, 300, 800, 600}, color:blueColor} to end of commandList
    
copy {class:filled rectangle, bounds rectangle:{0, 300, 400, 600}, color:cyanColor} to end of commandList
    
copy {class:filled oval, bounds rectangle:{200, 0, 600, 300}, color:cyanColor} to end of commandList
    
copy {class:filled oval, bounds rectangle:{400, 150, 800, 450}, color:redColor, drawing type:quartz, opacity:1.0} to end of commandList
    
copy {class:filled oval, bounds rectangle:{200, 300, 600, 600}, color:greenColor, drawing type:quartz, opacity:0.5} to end of commandList
    
copy {class:filled oval, bounds rectangle:{0, 150, 400, 450}, color:blueColor, drawing type:quickdraw, graphics mode:blend, opcolor:{32767, 32767, 32767}} to end of commandList
    
tell thisDocument to create composition element with properties commandList
  
end tell
end run

iMagine Photo can also draw polygons, and this is demonstrated in script 5. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
set blueColor to {0, 0, 65535}
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{800, 600}, name:"Drawing Window"}
    
set polyPoints to {{40, 50}, {180, 120}, {50, 550}, {720, 300}, {720, 580}, {360, 550}, {720, 45}, {360, 200}}
    
set thePolyProperties to {class:filled poly, poly points:polyPoints, color:blueColor, drawing type:quartz, opacity:1.0}
    
tell thisDocument to create composition element with properties thePolyProperties
  
end tell
end run

iMagine Photo can also draw lines, pixels, multicolored pixels plus shapes made up of lines and cubic and quadratic beziers. The following script demonstrates these various different composition elements. Panther users can open this Apple Script in a new Script Editor window by clicking here.

on run
  
tell application "iMagine Photo"
    
set thisDocument to make new window document with properties {dimensions:{1024, 768}}
    
set darkRedColor to {32767, 0, 0}
    
set cyanColor to {0, 50000, 50000}
    
set darkGreen to {0, 25000, 0}
    
set quartzDrawingRecords to {{quartzType:quartz line, end point:{1000, 30}}, {quartzType:quartz cubic bezier, cp1:{650, 400}, cp2:{350, 400}, end point:{24, 36}}, {quartzType:quartz quadratic bezier, cp1:{350, 400}, end point:{24, 700}}, {quartzType:quartz line, end point:{24, 30}}}
    
tell thisDocument
      
create composition element with properties {class:standard line, start point:{64, 32}, end point:{968, 724}, color:darkRedColor, drawing type:quartz, line thickness:4}
      
set pixelPoints to {}
      
repeat with i from 1 to 1000
        
set xPos to (random number from 0 to 1024) as integer
        
set yPos to (random number from 0 to 768) as integer
        
copy {xPos, yPos} to the end of pixelPoints
      
end repeat
      
create composition element with properties {class:filledQuartz, quartzRecord:quartzDrawingRecords, start point:{24, 30}, color:cyanColor}
      
set coloredPixelPoints to {}
      
repeat with i from 1 to 1000
        
set xPos to (random number from 0 to 1024) as integer
        
set yPos to (random number from 0 to 768) as integer
        
copy {xPos, yPos} to the end of coloredPixelPoints
      
end repeat
      
create composition element with properties {class:pixel, pixel points:pixelPoints, color:darkGreen}
      
set theColors to {}
      
repeat with i from 1 to 1000
        
set redCol to (random number from 0 to 65536) as integer
        
set greenCol to (random number from 0 to 65536) as integer
        
set blueCol to (random number from 0 to 65536) as integer
        
copy {redCol, greenCol, blueCol} to the end of theColors
      
end repeat
      
create composition element with properties {class:multicolored pixel, pixel points:coloredPixelPoints, pixel colors:theColors}
    
end tell
  
end tell
end run

Many of the drawing commands when using Quickdraw (the default) rather than Quartz can draw using various graphic modes. The drawing text documentation page uses a number of these graphic modes in its examples.

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, shape drawing, Macintosh, Quartz, Quickdraw