l i v e c o d e

How to Export a Card Image (Not a Screenshot!)
Mac OS 9 Mac OS X Windows Linux

Update 2/3/06
This Tip may crash on earlier versions of the MC engine and has not been comprehensively tested on all platforms under all versions of MC/Rev, so please test before using!

Additionally, stacks whose cantModify property is set to true cannot have their screen buffer updated, and so stacks in this state may cause a crash.

There are many times you might want to export the contents of a card to a JPG or PNG file, and MetaCard/Revolution’s built in export snapshot command takes a literal screenshot, so things like palettes and toolbars can get in the way of getting a clean image of the card. Additionally, you might have a stack window which is larger than the current monitor, and need to export the card image; export snapshot just won’t cut it in these cases.

The following script allows you to export a card image to disk:

on ExportCard pStackPath,pExportPath,pImageType
  -- pStackPath is the path to the stack whose card you want to export
  -- pExportPath is where you want the image to go
  -- pImageType is one of the three formats supported by the export
  --   command: paint, png or jpeg
  put the alwaysBuffer of stack pStackPath into tOldBuffer
  -- The next two lines force the current card image into the offscreen buffer
  set the alwaysBuffer of stack pStackPath to false
  set the alwaysBuffer of stack pStackPath to true
  create invisible image
  -- Here's the 'meat' of the handler:
  set the imagePixMapID of last image to (the pixMapID of stack pStackPath)
  select last image
  do "export" && pImageType && "to file pExportPath"
  delete last image
  set the alwaysBuffer of stack pStackPath to tOldBuffer
  choose browse tool
end ExportCard

Note that this will only export what is visible in the stack window; that is, if you have a scrolling group on the card, you won't get everything inside the group - you would need to resize the stack to accomodate everything before you did an ExportCard.

Posted 3/31/03 by Ken Ray, based on original code by Brian Yennie (thanks, Brian!)


Update: 8/17/03: It turns out that his works with stack windows that are hidden or totally offscreen.
Update: 12/24/03: Richard Gaskin discovered that this causes MC to crash when used in OS 10.2.8 with MC 2.5.1. This was confirmed by Ken Ray on 10.3 with MC 2.5.1.
Update: 2/3/06: Richard Gaskin discovered that the location of the last 'set the alwaysBuffer' line, if listed before the "do export" line causes MC and Rev to crash when used in OS 10.4.4. As such, this line was moved to just before the choose browse tool line.

 Print this tip

News and Rumors Products Services Developer Resources Contact STS About STS