The MacView

Virtual Instrumentation from a Mac perspective

Friday, December 30, 2005

Advanced String VIs for Cross Platform Support

In vi.lib/AdvancedString there are some very useful VIs when it comes to cross platform development.


Command Line String To Path.vi

This will convert a path String generated by call to System Exec.vi (Connectivity -> Libraries & Executables) to a LabVIEW path. On non-Mac platforms, it does the same thing as the String To Path primitive. On the Mac, it asks the OS to convert it.


Path To Command Line String.vi

This will convert a Path to a path String that can be used in generating a command to pass to a call to System Exec.vi (Connectivity -> Libraries & Executables) to a LabVIEW path. On non-Mac platforms, it does the same thing as the Path To String primitive. On the Mac, it asks the OS to convert it.


Relative Path To Platform Independent String.vi

Takes a relative path (one that does not start with a disk name) and converts it to a URL-like path string (uses forward slashes as separators) on all platforms.


Normalize End Of Line.vi

Converts all the end of line characters (Mac [\r], UNIX [\n] or DOS [\r\n]) in a string into another line ending type. There are two line ending types that you can convert to that are a little different. The Native line ending type is the line ending character that LabVIEW uses on the platform (for the Mac, its carriage return [\r]). This is equivalent to using the End Of Line Constant (Programming -> String). There is also Native Command Line, which is the same as Native on non-Mac platforms. On the Mac, it is line feed [\n] (UNIX). This way you can generate shell scripts, for example, you could use this to make sure it would be in UNIX format.

Using this VI can help make your code line-ending-agnostic, which makes it more robust.

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.


Friday, December 23, 2005

Conditional Disable Structure

There is a great new feature in LabVIEW 8.0 for cross platform development: The Conditional Disable Structure. For those familiar with C, this is a #if. You can implement cross platform support in one VI, and the version for the platform it is on will be used. You can find the conditional disable structure in the functions palette under Programming -> Structures.

As an example, say you wanted to create a VI that will convert a Path into a String that could be used to build up a command for the System Exec.vi (Connectivity -> Libraries & Executables). You can just use Path To String primitive (Programming -> String -> Conversion) in most cases, but on the Mac, the command line takes UNIX style paths, and Path To String will produce a Classic style path. As it turns out, there is a VI, vi.lib/Platform/CFURL.llb/PathToUNIXPathString.vi, however this VI is only on the Mac.

If you wanted to make a VI that did the right thing for on every platform, before the Conditional Disable Structure existed, you would have a case structure that used the Property Node (Programming -> Application Control) with Application -> Target -> Operating System property to determine the OS at runtime. In the default case you would just use the Path To String primitive. In the Carbon (Mac OS X is Carbon, don't be fooled by the Mac [Classic] item) case, you would open a reference to PathToUNIXPathString.vi (Programming -> Application Control -> Open VI Reference), call the VI by reference (Programming -> Application Control -> Call by Reference Node) and then close the reference (Programming -> Application Control -> Close Reference). You would have to do this so the VI would not be broken on non-Mac platforms, since PathToUNIXPathString.vi is only there on the Mac.

There are a few problems with this method. The first is that you have to create a type specifier VI refnum to get the connector pane correct on the Call by Reference Node (passed to Open VI Reference). You also have to get the path to the VI to call correct (which may change if you build it into an Application or Shared Library). It is also a common mistake to forget to close the VI reference, and have a VI reference leak. Basically, the code is difficult to write, difficult to maintain and difficult to get right.

With the Conditional Disable Structure, you have two cases. The default case will just have the Path To String primitive. The second case will be TARGET_TYPE == Mac. In that case, just drop the PathToUNIXPathString.vi and wire it up. And there you have it.

Now there is a small annoyance with this method. On the non-Mac platforms, when the VI is loaded and needs to be re-compiled for any reason, it will do a quick search for PathToUNIXPathString.vi, but the VI will come up as runnable.

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.


Friday, December 16, 2005

New Mac Features in LabVIEW 8.0

With all of the bullet features added to LabVIEW in the latest release, I feel that a lot of the new Mac features were glossed over. Most of them are little ones, but they sure make life easier.

One cool new feature, that is Mac-only, currently, is the Goto LabVIEW Folder button in Open dialog boxes. If the dialog supports VIs, there will be a Goto LabVIEW Folder in the lower left corner. This is especially helpful if you have several versions of LabVIEW installed (7.0, 7.1, 8.0) and several running at the same time. You'll go right to the directory of the current LabVIEW.

Another nice feature is the scroll wheel. If you use a Apple's Mighty Mouse, or any scroll wheel mouse, you can scroll the block diagram or front panel. LabVIEW supports horizontal and vertical scroll wheels. You can also emulate the horizontal by holding down the shift key when scrolling.

Another Mac-only feature is the ability to use Keyboard shortcuts in the "Save Changes?" dialog. Using Command-S will perform the Save (or Save All) operation. Command-D will perform the Don't Save (or Don't Save All or Defer Decision) operation. Escape key should work on all platforms for cancel.

In vi.lib, we added a couple more fun Mac specific VIs. One VI tells the state of keyboard modifiers (ie Shift, Control, Command, Option). One tells the current state of the mouse buttons. The third will run an arbitrary AppleScript (as a string). These VIs can be found in vi.lib/Platform.

Many people do not know about the treasure trove of Mac specific things found in vi.lib/Platform. There are several Carbon APIs that are wrapped in VIs. Some are for the more advanced developer who is familiar with Mac APIs, but there are some higher level ones, like CFURL.llb/PathToUNIXPathString.vi and CFURL.llb/UNIXPathSTringToPath.vi. Another useful high level VI is FileSystem.llb/FindFolder to LV Path.vi. You can ask the system to tell you where certain special folders are on the system.

In the future I will post helpful hints on what you can do with LabVIEW and the Mac.

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.