More Flexibility with Magnets in Omnigraffle

Like a lot of Mac users I’m a big fan of the Omni Group’s Omnigraffle. One of the areas where Omnigraffle really excels over other diagramming software is in its handling of connections. Connections, which are so often a source of frustration in other software, are a breeze in Omnigraffle and one of the features that makes them so easy is magnets.

That being said, one limitation I’ve come up against is that Omnigraffle allows you a maximum of five magnets per side when you want to place and distribute them automatically. For some types of diagram, circuit or logic diagrams in particular, I will often need more than five connections from the side of a shape. Yes, I know I can add further magnets manually, and yes, I know I could use a grid to ensure they are distributed evenly. But frankly, who wants to waste their valuable time painstakingly placing individual magnets? Not me – that’s for sure.

AppleScript to the rescue! Fortunately, Omnigraffle has excellent AppleScript support, so with an afternoon’s work I was able to create a script that would allow me to have an arbitrary number of magnets along the side of a shape. Since I’ve gone to the trouble of creating a script, I’ve also programmed it to give me the option of including the corners of the shape or not and of selecting which sides of the shape the magnets should be applied to.

Here’s an example of the script at work.

I’m starting with a large rectangle with no magnets, which I select before running the script:

A rectangle in Omnigraffle with no magnets

When I run the script it asks me for the number of magnets I want on a side and whether I want them to be distributed including the corners or not including the corners.

Dialogue box asking for number of magnets per side and whether to include corners or not

Now it asks me to select the sides I want to apply them to.

A dialogue box asking which sides the magnets should be applied to

And that’s it – here’s my rectangle with the magnets applied and evenly distributed as requested.

The rectangle with magnets applied and evenly distributed

So what about a typical real world application. Well let’s say you are creating a circuit or logic diagram which includes a 40 pin integrated circuit. It would take you some time to set up a grid of the right size and apply twenty evenly spaced magnets to each side for the pin connections. But with my script it takes all of three seconds, and here’s the result:

A diagram of a 40 pin IC with one magnet in position for each pin

So you can see how useful the script is when you have a large number of connections to create, but even with fewer connections its flexibility is quite handy. Here are some examples:

Examples of output from the magnet script

Notice the behaviour with corners. If you select just one magnet per side including corners, then the magnet will be placed on the leading corner of the side as you progress round the shape in a clockwise direction. It’s also worth noting that when corners are included and you have magnets on adjacent sides then the script is smart enough to place a single magnet at the shared corner – not two. Another point worth noting is that this script is intended to work with rectangles. It can be applied to other shapes, but the distribution of the magnets will not be even, because of the distorted edge of the shape. Also it works on all the shapes that are currently selected, so if nothing happens, check you have at least one shape selected. Any lines in the selection will be ignored.

Here’s the script, which you are free to use for your own work in Omnigraffle. It’s been tested only with Omnigraffle Pro 5, but it should work with other recent versions.

This entry was posted in Application Development, Scripting and tagged , , , . Bookmark the permalink.

5 Responses to "More Flexibility with Magnets in Omnigraffle"

Leave a reply