OO-Snippets: Howto get the URL of a loaded document

Commons

Keywordsurl, current document
LanguageJava
ApplicationOffice
AuthorsTom Schindl (initial)
Supported Versions
Supported OS
Question How does one get the URL of the active document

I tried using the com.sun.star.document.MediaDescriptor but got a com.sun.star.beans.UnknownPropertyException

XPropertySet documentProps =  (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,document);
System.out.println(documentProps.getPropertyValue("URL")); 
Answer

The URL-Property is marked OPTIONAL in the IDL. You are better of using the XModel to get the URL

Code-Snippet-Listing (snippet-source)

import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XModel;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class Snippet {

    public void snippet(XMultiComponentFactory xmcf, XComponentContext ctx) throws Exception {
        Object desktop = xmcf.createInstanceWithContext("com.sun.star.frame.Desktop", ctx);
        XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
        XComponent document = xDesktop.getCurrentComponent();
        XModel xmodel = (XModel) UnoRuntime.queryInterface(XModel.class, document);

        if (xmodel != null) {
            System.out.println(xmodel.getURL());
        }
    }
}

Changelog

DateUserModification
2005-03-14tomsontomRemoved unneeded code
2005-03-11tomsontomInitial version

and