LockFocus

Compiling Opacity Images

Opacity is great for creating graphics for use in applications, but how do you ship those images with the application? Including the .opacity file itself would be bad (that would be like shipping your source code with the application), a native image format like TIFF would be better. At the same time, though, it’d be nice to be able to directly edit a .opacity source file and not have to manually rebuild the associated .tiff file each time. Luckily there’s an easy way to make this happen automatically with Opacity.

Opacity includes opacityutil, a command line utility that can convert images between the formats supported by Opacity. You can use this functionality in Xcode to create a Build Rule that will dynamically convert .opacity images in your project to TIFF files (or any other supported format) at compile time.

To do this, first open your application’s project in Xcode. Add any .opacity images you want to the project (and include them in the appropriate target). Then, double click the application’s target to edit it. Select the Rules tab and click the plus button to add a Build Rule. Change the “Process:” popup to “Source files with names matching:” and enter *.opacity in the field (to match all .opacity files). Then change the “using:” popup to “Custom script:” and enter the following script (which should be all on one line):

opacityutil -save -out
  ${DERIVED_FILES_DIR}/${INPUT_FILE_BASE}.tiff ${INPUT_FILE_PATH}

Finally, add an output file (by clicking the plus button). Change this output file to:

$(DERIVED_FILES_DIR)/$(INPUT_FILE_BASE).tiff

The result should look something like this:

Build Rule

With this setup, all .opacity files included in the target will get converted into TIFF images that will be added to the application. You can change the format for the built images by changing the two .tiff extensions above to an extension for another supported format, like .png for PNG images.

Opacity’s Xcode support was inspired by this post by Peter Hosey.

Of course if you don’t use Xcode, you can also manually build your .opacity image to a different format with Factories in Opacity.