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

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

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

Post 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
Last edited by musikai on Tue Aug 20, 2019 7:53 pm, edited 1 time in total.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

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

Post 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
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

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

Post 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.
Last edited by musikai on Tue Aug 20, 2019 7:40 pm, edited 1 time in total.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
User avatar
MrProgrammer
Moderator
Posts: 4903
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

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

Post 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
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

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

Post 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.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

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

Post 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.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

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

Post 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.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
User avatar
RusselB
Moderator
Posts: 6646
Joined: Fri Jan 03, 2014 7:31 am
Location: Sarnia, ON

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

Post 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.
OpenOffice 4.1.7, LibreOffice 7.0.1.2 on Windows 7 Pro, Ultimate & Windows 10 Home (2004)
If you believe your problem has been resolved, please go to your first post in this topic, click the Edit button and add [Solved] to the beginning of the Subject line.
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

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

Post 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 5337 times
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
Post Reply