Is it possible to open a Gallery using a Visual Basic script? Yes - but only if it is already open (but not visible).
If you are new to VB you might also want to pop the Object Browser (use the F2 key) and from the top drop down select "Portfolio_V5". You can now look around the Portfolio automation object model and get the correct declarations/syntax before reading further. A key concept here is that in the automation 'view' of a Catalogue, only Galleries rather than Catalogues exist as objects to actually work with - even if you never set up any Galleries when you created the Catalogue, in which case there will be one Gallery - 'Untitled'.
There is a VB 'Document' object that is arguably the equivalent of a Catalogue. However, the Gallery is the object you act on and the Gallery object's Name property includes both the Catalogue filename and Gallery name - as also seen displayed in the caption bar of a Gallery open in the program's UI. This does mean that you may want to do some precursor work early in the project and probably in Portfolio itself to set up your key Galleries as you want them before attempting any scripting.
The VB automation documentation PDF states "If the Catalogue has already been opened with this copy of Portfolio (either through scripting or manually), the first gallery of the Catalogue will come to the front and become the active Gallery.".
So, your Catalogue may be open but possibly with another Gallery as the active one. How then to find the one you need? Assuming the worst case (i.e. others may have subsequently added/deleted Galleries), use the Document object, Count the Galleries and then iterate (or use a For Each...Next construction) checking each Gallery object's Name (GalleryName As String) for that of your intended gallery. Now you've identified it, use the Document object to set your chosen gallery as the active one (Function SetActive(GalleryName As String) As Integer).
If you are certain of the Gallery name you can just check both the Catalogue and Gallery are open and set that name's Gallery as active using SetActive. The reference to the Gallery specifically needing to be open, leads us to one more automation glitch. The automation type library offers no method for opening an existing but closed Gallery (really dumb, I know!). As an aside, those using the Mac seem to be lucky as open gallery "name" in AppleScript does just that - opening a new window using the named gallery.
Another gotcha to watch for is that the VB (Mac?) automation object only assumes one Catalogue is open. There seems to be no way in code to switch from e.g., cat1.fdb -> cat2.fdb, as the 'active Catalogue or even fetch the name string of the current catalogue (.Document Count gives the number of Galleries in the Catalogue). However as the Gallery Name string is a concatenation of both the Catalogue's and Gallery's names, you could parse the Catalogue name from that.
Gallery names take a slightly unintuitive syntax given the fact you can only access the active Catalogue (even if more than one is open). The syntax is CatalogName[space][hyphen][space]GalleryName, e.g. "fubar.fdb - MyGallery". If you open more than one Catalogue and do a Document.Count you will get the sum of all the open Galleries in all the open Catalogues - which gives a bit more logic to the naming.
It seems they forget to update the TLB for v7 and indeed for v8! PortObj.GetActive is broken in v7/v8. Or rather, it only works if you've more than one window. You can't use it, v7-client-UI-style, to refocus the current window to a different gallery. Neither does the TLB have a command for opening an existing (i.e. saved) but unopened gallery - nor does it have a way to iterate the names of existing galleries. If you use the getActive command with only a single window open and trap the Error number returned you get #27 - kScriptGalleryNotFound, i.e. "Gallery Not Found". Note, the Mac scripting interface can switch the gallery focus of the current window - in case that offers an alternate approach for cross-OS users.
If you need more than one gallery open you'll have to ensure it is open - as a separate window - before the script is invoked. Such setup can sadly only be done manually via the client UI.
If you use a Find with 'open in new gallery' as 'true' then the gallery created is added to the catalogue - permanently - as a saved gallery (in the unintuitive naming sequence: Untitled, Untitled 2, Untitled 3, etc....). Thus when you close that new gallery window it's still in the saved gallery list; each time you run such a script you'll add a new gallery. Although v8 restores unsaved (scratchpad) galleries as in v5 I doubt a Find script-created new v8 gallery will be unsaved.
Bottom line - be very defensive in your code's assumption as to the gallery it is looking at and avoid using scenarios demanding scripting of more than one gallery concurrently.
Question: Opening Galleries with VB [FAQ00030.htm]
Last Update:- 31 May 2006
Site and articles © Mark Anderson 2001-2007 - Visit my home page