23 December 2014

Delete Styles En Massé

About a year ago, playing around with styles in a document, and deleting unused styles, I reduced the size of the ODF by almost 2 megabytes. Most of that was from manually deleting a number of unused styles. Guessing, I'd say that I manually deleted more than 5,000 paragraph, character, list, and page styles. 

Needless to say, manually deleting styles is a very long, error-prone process. More than once I accidentally deleted a style that I wanted to keep.

Around a month ago, I started playing with more fonts, looking for the perfect set of fonts for documents.  Something that would look good in print, on the web, PDF, ePub, and Mobi formats. Whilst no such critter exists, some fonts do work better than others.

As part of switching fonts, I created a new set of templates.  Applying the new templates to existing documents, simply replaced existing styles. Whilst that is the expected action, it wasn't quite what I wanted.


So I went searching for how to do mass deletes.  Nothing in any of the various extension repositories.

I did find "Wish: Purge Unused Styles" at
http://ask.libreoffice.org/en/question/2196/wish-purge-unused-styles/?answer=16767#post-id-16767
which, in turn, led to Bug 46719
https://bugs.freedesktop.org/show_bug.cgi?id=46719,
which in turn led to Remove unused custom styles
https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=42571.

And there it is.
A macro that removes all styles that are not used by the document in question.

However, Kumāra's Comment 9
https://bugs.freedesktop.org/show_bug.cgi?id=46719#c9
has a good question:  "How do I import this?"

Screenshots are from:
  • Web browser is Firefox 34.0, as mutilated by Ubuntu;
  • Office Suite is EuroOffice 2014.0.0, from MultiRáció;
  • Operating System is Xubuntu 14.10;

I initially tested this out on Apache OpenOffice 4.1.1

I make no guarantees that this will work on other setups.

More specifically, this does not work on either Android Open Office 1.6.3 or EuroOffice for Tablet 1.2  as they do not have the required functionality to do this.

Opening the program, you have:
Default EuroOffice Screen

 Then:
In EuroOffice,:
 ">Tools >Macros >Organize Macros >EuroOffice Basic"
In Apache OpenOffice:
 ">Tools >Macros >Organize Macros >OpenOffice Basic"
In LibreOffice:
 ">Tools >Macros >Organize Macros >LibreOffice Basic"

Select Edit
Select Edit 

That brings up the macro editor screen

Macro Editor Screen
Delete all of the text on that screen.
Then type
"REM https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=42571#"

That way you know where you obtained the code from.

In your web browser, go to https://forum.openoffice.org/en/forum/viewtopic.php?f=21&t=42571#

Carefully read each of the descriptions of the code snippets, before deciding which one you want to use.
  • Hagar Delest's first macro deletes styles by family, after verification;
  • Hagar Delest's second macro automatically deletes all unused styles;
  • Hagar Delest's third macro enables unused custom styles to not be deleted;

I used the second macro.

Cut and paste the appropriate macro.
LibreOffice does not always accept a "cut and paste" operation.
This is a security measure, and as such is a good thing.
Except when you can't paste anything.

The post has a "Select All" option. Click on it, and then select "Copy" with your browser. Typing "Control C" should copy it. Typing "Control V" should paste it.
Select The Macro to Copy
After typing <CTRL-C>
Switch to OpenOffice
On the macro editor, type <CTRl-V>
If that doesn't work, then use ">Edit >Paste"

Your screen should look like this:

Pasted Macro


Rename the macro by clicking on "Module 1", and then "Rename"
I called it PurgedStyles. The only criteria for the name, is that you can tell what it does, without running it. 

Then click on the floppy disk icon, (Third icon from the left hand side, on the top row of icons) to save it.

Now close the editor.  ">Files >Close".

Now to use it.
I like keyboard shortcuts, so I assigned it to F4.

To do that:
Create a new document:
">File >New >Text Document"
I use a text document, because keyboard assignments can be component specific.
An existing document can be used. I don't like using existing documents, because of the potential of damage to them. Once, I accidentally deleted a couple of pages, by pressing the wrong key, in the wrong place.

The only reason for having an open document, is so that something can be assigned to the keyboard.

">Tools >Customize >Keyboard"

The screen should display something akin to:

Configure Keyboard
To modify the keyboard:

There is a radio button for either "Writer" or "Euroffice".
I tend to make macros component specific.  Other people prefer making them available to all components.
Check the appropriate radio button.

Select the Shortcut Key.
I like to use F1 through F5 for macro's I've imported from elsewhere, and F10 through F12 for quick and dirty macros I've written, and will delete when I've finished for the day.
Scroll down the list of available shortcut keys.
  • F#;
  • Shift F#;
  • CTRL-character;
  • CTRL-SHIFT-character;
  • ALT-character;
  • ALT-SHIFT-character;
  • CTRL-ALT-character;
  • CTRL-ALT-SHIFT-character;

In this instance, I am going to use F4.
In the screenshot below, you can see the macro name highlighted next for F4.

Switch to the the Category window <ALT-C>.
Scroll down until you find "EuroOffice Macros", "LibreOffice Macros", "OpenOffice Macros", as appropriate.

Opening that, there will be:
  • User;
  • Share;
  • Document-name;
Scroll down to "User". 
Move into "Standard".
Select "PurgeStyles", or whatever you called the macro.
I've highlighted it, in the below screenshot.
Customization Details

Switch to the Function window  <ALT-F>.
For this macro, select "MainRemoveUnusedStyles".
I've highlighted it, in the above screenshot.
Other macros will have a different name for the main function.
Usually, but not always, it is prefixed by "Main".

Then click on the Modify button <ALT-M>.
Then click on the Save button <ALT-S>.
Save Keyboard Configuration
Strictly speaking, it is not necessary to save the keyboard configuration.
The main reason for doing so, is when one switches between environments.
It is much easier to copy configuration files, then recreating them from scratch.

Now click on the OK button at the bottom right of centre.

Your macro should be saved, and available for use with other documents.

Some caveats:
  • Always back up your work;
  • Always use a different file-name for your backups;
  • Keep backups on a different drive;
I use document-name-date for backups.



Screenshots were taken with Xfce4 Screenshooter.

Some screenshots were cropped using GwenView 4.1.1.

No comments:

Post a Comment