Cleanup recent files

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
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Cleanup recent files

Post by hanya »

This code removes non-existing files from the history list. The list of the history is stored in the configuration node of /org.openoffice.Office.Common/History. This code also does not affect at the runtime like the changing the size of the history. So, you need to shutdown the OpenOffice.org completely after the code execution. Because the PopupMenuController implementation of the Recent Files menu keeps the list of the history and they would be back if you choose the File menu (not only the File menu but any menu including the Recent Files menu entry).

Code: Select all

Sub CleanUpHistory()
 Dim oCP As Object
 Dim oCUA As Object
 Dim aProps(0) As New com.sun.star.beans.PropertyValue
 
 ' get configuration of the file history
  oCP = GetProcessServiceManager().createInstanceWithContext( _
    "com.sun.star.configuration.ConfigurationProvider", GetDefaultContext() )
  aProps(0).Name = "nodepath"
  aProps(0).Value = "/org.openoffice.Office.Common/History"
  oCUA = oCP.createInstanceWithArguments( _
    "com.sun.star.configuration.ConfigurationUpdateAccess", aProps )
  
  ' clean up PickList
  CleanUpList( "PickList", "PickListSize", "p", oCUA )
  
  ' clean up List
  CleanUpList( "List", "Size", "h", oCUA )
  
  oCUA.commitChanges()
End Sub

' sListName: PickList or List
' sSizeName: PickListSize or Size
'   sPrefix: p or h
Sub CleanUpList( sListName As String, sSizeName As String, _
    sPrefix As String, oAccess As Object)
  
  Dim oList As Object
  Dim oItem As Object
  Dim nSize As Long
  Dim sName As String
  Dim nCounter As Integer
  
  oList = oAccess.getPropertyValue( sListName )
  nSize = oAccess.getPropertyValue( sSizeName )
  
  nCounter = 0 'existing items
  
  For i = 0 To nSize - 1 step 1
    sName = sPrefix & CStr(i)
    If oList.hasByName( sName ) Then
      oItem = oList.getByName( sName )
      If FileExists( oItem.URL ) Then
        
        CreateAsNewItem( oList, oItem, sPrefix & CStr(nCounter) )
        
        If nCounter <> i Then
          oList.removeByName( sName )
        End If
        nCounter = nCounter + 1
      Else
        oList.removeByName( sName )
      End If
    End If
  Next
End Sub


' make new item and insert/replace it in the sName position
Sub CreateAsNewItem( oList As Object, oItem As Object, sName As String )
  oNewItem = oList.createInstance()
  With oNewItem
    .URL = oItem.URL
    .Filter = oItem.Filter
    .Title = oItem.Title
    .Password = oItem.Password
  End With
  If oList.hasByName( sName ) Then
    oList.replaceByName( sName, oNewItem )
  Else
    oList.insertByName( sName, oNewItem )
  End If
End Sub
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Cleanup recent files

Post by hanya »

Dummy post to remove from the unanswered list.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Post Reply