Page 1 of 1

Extract content.xml into same folder and open in default app

Posted: Tue Aug 20, 2019 12:34 pm
by musikai
This Macro will extract the "content.xml" of an odf into the same folder and open the folder and the "content.xml" in the default (associated) xml-Editor. Or open it in OO/LO.

This is just a convenient way to quickly have a look at a "content.xml" file.

Code: Select all

rem---extract content.xml into same folder and open in default app (or in Open/LibreOffice)
sub extract_content_xml
    dim args(0)
  	dim args1(0) As New com.sun.star.beans.PropertyValue
oDoc=ThisComponent
if len(oDoc.getURL())=0 then
 msgbox "Save document first!"
 exit sub
end if

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
workdirfull = DirectoryNameoutofPath(oDoc.getURL(),"/")

    rem otmp_url = CreateUnoService("com.sun.star.util.PathSettings")
    rem stmp_url = otmp_url.temp
    stmp_url = workdirfull
    z = CreateUnoService("com.sun.star.packages.Package")
    args(0)= oDoc.URL
    z.initialize(args())
    oInputstream=z.getByHierarchicalName("content.xml").GetInputStream
    
    oSimpleFileAccess = createUnoService("com.sun.star.ucb.SimpleFileAccess")
 
    oSimpleFileAccess.WriteFile(stmp_url & "/" & "content.xml", oInputstream)
    
   
oSyShell = CreateUnoService("com.sun.star.system.SystemShellExecute")
 
rem---open Folder with content.xml:
oSyShell.execute(workdirfull, "", 0)

rem---open content.xml in external editor
 oSyShell.execute(workdirfull & "/" & "content.xml", "", 0)

rem--- or instead open content.xml in Open/LibreOffice:
' newoDoc = StarDesktop.loadComponentFromURL(workdirfull & "/" & "content.xml","_default", 0, args1())
end sub
After you have edited and closed the "content.xml" you can use the following macro to insert it back into the document (but on a copy). With your original document still open, it will create a copy of it, inserts the "content.xml" and opens the file in OO/LO.

Code: Select all

rem--- create a copy of the current document and insert the content.xml (lying in the same folder, created by the first macro) into it, open the new doc
sub replace_content_xml
Dim args1(0) As New com.sun.star.beans.PropertyValue
oDoc=ThisComponent
if len(oDoc.getURL())=0 then
msgbox "Save document first!"
exit sub
end if
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
workdirfull = DirectoryNameoutofPath(oDoc.getURL(),"/")
docname = GetFileNameWithoutExtension(oDoc.getURL(),"/")
Doc_ext = GetFileNameExtension(oDoc.getURL(),"/")
DateiNeu = workdirfull & "/" & docname & "(new)."  &  Doc_ext
oDoc.storeToURL(DateiNeu, Array())
service_manager = CreateObject("com.sun.star.ServiceManager")
ooo_zipper = service_manager.createinstance("com.sun.star.packages.Package")
Dim argsx(0)
argsx(0) = ConvertToUrl(DateiNeu)
ooo_zipper.Initialize(argsx())
Dim argsx2(0)
argsx2(0) = False
InputStream = ooo_zipper.createInstanceWithArguments(argsx2())
oUcb = service_manager.createinstance("com.sun.star.ucb.SimpleFileAccess")
x1Datei = oUcb.OpenFileRead(workdirfull & "/" & "content.xml")
InputStream.setInputStream(x1Datei)
ParentFolder = ooo_zipper.getByHierarchicalName("")
ParentFolder.replaceByName("content.xml", InputStream)
ooo_zipper.commitChanges

rem--- open new doc
newoDoc = StarDesktop.loadComponentFromURL(DateiNeu,"_default", 0, args1())
end sub

Re: extract content.xml into same folder and open in default

Posted: Tue Aug 20, 2019 2:01 pm
by John_Ha
Nice one!

I can edit a content.xml with five clicks. I have 7-ZIP set in my right-click context menu so I r-click a .od? file (click 1) and select 7-ZIP (click 2). I drag (click 3) content.xml to the desktop and r-click (click 4) it where I also have Notepad++ set in my context menu and I select it (click 5). I also have HxD set in my context menu so I can open the .of? file in a binary editor whenever I want to look at thousands of nuls in a corrupted file ...
Clipboard01.gif

Re: extract content.xml into same folder and open in default

Posted: Tue Aug 20, 2019 2:56 pm
by musikai
Yeah, 7zip and Notepad++ etc makes a nice combination

You even can make it faster if you set Notepad++ as Editor in 7 zip, then you don't have to drag the "content.xml" anywhere but can directly press F4 in 7zip to edit it in the set editor.

Extract content.xml into same folder and open in default app

Posted: Tue Aug 20, 2019 6:06 pm
by MrProgrammer
musikai wrote:This Macro will extract the "content.xml" of an odf into the same folder and open the folder and the "content.xml" in the default (associated) xml-Editor.
On a Mac, using the Terminal application:
   cd DirectoryContainingTheFile
   unzip NameOfFile content.xml
   open content.xml

Re: Extract content.xml into same folder and open in default

Posted: Tue Aug 20, 2019 6:51 pm
by musikai
MrProgrammer wrote:
musikai wrote:This Macro will extract the "content.xml" of an odf into the same folder and open the folder and the "content.xml" in the default (associated) xml-Editor.
On a Mac, using the Terminal application:
   cd DirectoryContainingTheFile
   unzip NameOfFile content.xml
   open content.xml
Thank you!
That's 1/3 cool.

I always look for OS independant solutions so how are the commands on Windows and Linux?
Then build a macro where it calls the correct command on the used OS.

Re: extract content.xml into same folder and open in default

Posted: Tue Aug 20, 2019 9:34 pm
by RusselB
On Windows 95 - 7 (inclusive), the Mac instructions will work when using the DOS command prompt, accessed using CMD
I don't have a version higher than 7 to test with, but did test with the following Windows versions: 95, 98, XP, Vista and 7. ME was released between 98 and XP, but I don't have a system with that installed.

Re: extract content.xml into same folder and open in default

Posted: Wed Aug 21, 2019 12:12 pm
by musikai
Thank you!
RusselB wrote:On Windows 95 - 7 (inclusive), the Mac instructions will work when using the DOS command prompt, accessed using CMD
You are more lucky than me!
I just tried on Windows 7 the cmd command "unzip" and "open" and both couldn't be found.
Why? No, I don't want to dive into that.

Now as the code in 1st post works in both directions on all OS it's good enough for me.

Re: extract content.xml into same folder and open in default

Posted: Wed Aug 21, 2019 1:07 pm
by RusselB
Could be that I was accessing commands via the DOS prompt that are parts of other programs that I have installed rather than items that are part of a basic Windows installation.
Not worth my time and effort to try to figure it out.

Re: extract content.xml into same folder and open in default

Posted: Wed Aug 21, 2019 2:12 pm
by John_Ha
RusselB wrote:Could be that I was accessing commands via the DOS prompt that are parts of other programs that I have installed rather than items that are part of a basic Windows installation.
Not worth my time and effort to try to figure it out.
You have either placed unzip.exe (or unzip.bat, which calls unzip.exe; or unzip.com) in the directory in which cmd.exe opens, which is C:\Users\John for me; or you have defined a path to where unzip.exe etc is located. Typing unzip first searches in the folder in which cmd opens, namely C:\Users\John ..., and then searches all user defined paths for unzip.exe, unzip.com or unzip.bat.

I tested it by placing Notepad.exe in C:\Users\John. Typing notepad opened Notepad and restored the prompt in cmd.
Clipboard01.gif
Clipboard01.gif (9.1 KiB) Viewed 6197 times