Page 1 of 1

A simple Save-Close Document & Quit OO without Crash/Recover

PostPosted: Mon Jan 11, 2021 6:16 am
by alf50
To run an Apple Script to Quit Open Office without I/O error (a problem many Mac Users have had over the past 10 years, who want to automate saving their document and quiting open office with a button while in Mac OS environments), this macro trigger a simple Apple Script in OpenOffice to quit OpenOffice or LibreOffice, then Saves the current Document you are working on, disposes of the document, then the Apple Script quits OpenOffice without crashing and forcing the user to recover their document the next time they open it. I use the following code:
Code: Select all   Expand viewCollapse view
Sub SaveClose
   dim CpathNm as string, CpNQoo as string, sPath1 as string
rem ********************************************************************
rem First, get the path to the Document you are currently in.
rem ********************************************************************
   GlobalScope.BasicLibraries.loadLibrary("Tools")
   CpathNm=DirectoryNameoutofPath(ThisComponent.getURL(),"/")
   L = LEN(CpathNm)
  '   MsgBox "CpathNm/L= " & CpathNm & "/ " & L
rem ********************************************************************
rem Next, The path returned starts with "file:///Users/MyDir/... then on to
rem   whichever directories your application/Document was started from and was to be saved in.
rem I keep the Qoo.app and QLo.app AppleScipts in an personal, Application Folder,
rem but to launch them, I must know where the "/" is after "MyDir" to get the launch Path right.
rem The following code handles the variable length of "MyDir" associated with different computers.
rem ********************************************************************
   If L < 15 then goto SnC2:
   for N = 15 to L
    If MID(CpathNm,N,1) = "/" then
      M = N
      CpNQoo = LEFT(CpathNm,M) & "Applications/Qoo.app"
     'msgbox "CpNQoo = " & CpNQoo
      goto SnC1:
     Else
    End If
   next N
   msgbox "Something is wrong, CpNQoo = " & CpNQoo
   goto SnC2:
rem ----------------------------------------------------------------------
SnC1:
   sPath1= CpNQoo
   oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
   oSvc.execute(sPath1, "" , 0)
rem ----------------------------------------------------------------------
rem In LibraOffice The apple script QLo is triggered with
rem  the following code:(without the rem's)
rem  Keep in mind, the line above must be changed to QLo.app/Contents/MacOS/applet
rem    instead of just Qoo.app.  I.E.:
rem    CpNQoo = LEFT(CpathNm,M) & "Applications/QLo.app/Contents/MacOS/applet"
rem ----------------------------------------------------------------------
rem   sPath1= CpNQoo
rem   shell(sPath1)
rem ----------------------------------------------------------------------
rem The AppleScript waits 3 seconds before quitting OpenOffice,
rem which gives enough time for your document to be
rem stored and disposed of before OO is terminated.
rem ----------------------------------------------------------------------
rem  on run
rem    delay 3
rem     tell application "OpenOffice"
rem      quit
rem     end tell
rem  end run
rem ----------------------------------------------------------------------   
   dim oDoc as Object
   dim oDocCtrl as Object
   oDoc = ThisComponent
   oDocCtrl = oDoc.getCurrentController()
   oDoc.store()                    ' Save...
   DisposeDocument(oDoc)   ' ... and close
SnC2:
End Sub


This is the first time I have provide a complete working Save & Close Subroutine, but there has been so many unusable suggestions out there over the years, that I hope this will really help Mac Users to use OpenOffice and LIbreOffice with macros, more effectively.

P.S. The code is really short without the REM Statements:
Code: Select all   Expand viewCollapse view
Sub SaveClose
   dim CpathNm as string, CpNQoo as string, sPath1 as string
   GlobalScope.BasicLibraries.loadLibrary("Tools")
   CpathNm=DirectoryNameoutofPath(ThisComponent.getURL(),"/")
   L = LEN(CpathNm)
  '   MsgBox "CpathNm/L= " & CpathNm & "/ " & L
   If L < 15 then goto SnC2:
   for N = 15 to L
    If MID(CpathNm,N,1) = "/" then
      M = N
      CpNQoo = LEFT(CpathNm,M) & "Applications/Qoo.app"
     'msgbox "CpNQoo = " & CpNQoo
      goto SnC1:
     Else
    End If
   next N
   msgbox "Something is wrong, CpNQoo = " & CpNQoo
   goto SnC2:
SnC1:
   sPath1= CpNQoo
   oSvc = createUnoService("com.sun.star.system.SystemShellExecute")
   oSvc.execute(sPath1, "" , 0)
rem ----- For Libra Office, replace the CpNQoo = Line with .... QLo.app and
rem        add /Contents/MacOS.applet to the end of the line. I.E. ------
rem CpNQoo = LEFT(CpathNm,M) & "Applications/QLo.app/Contents.MacOS.applet"
rem   sPath1= CpNQoo
rem   shell(sPath1)
rem ----------------------------------------------------------------------
   dim oDoc as Object
   dim oDocCtrl as Object
   oDoc = ThisComponent
   oDocCtrl = oDoc.getCurrentController()
   oDoc.store()                    ' Save...
   DisposeDocument(oDoc)   ' ... and close
SnC2:
End Sub