wizards/source/scriptforge/SF_Session.xba |   26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

New commits:
commit e110974bc1c2a0b253d27cf3ad2643aa5208e2cd
Author:     Jean-Pierre Ledure <j...@ledure.be>
AuthorDate: Sat Mar 23 17:41:49 2024 +0100
Commit:     Jean-Pierre Ledure <j...@ledure.be>
CommitDate: Sun Mar 24 17:35:49 2024 +0100

    ScriptForge (session).RunApplication() redesign
    
    The RunApplication() method uses
    - either the Shell() Basic built-in function
      (only in asynchronous mode)
    - or the com.sun.star.system.SystemShellExecute()
      service
    in this order.
    The second is tried in case of failure of the first one.
    
    Benefits:
    - the method can start batch files or scripts
    - broader than Shell(): giving a user file as argument
      opens its application first based on the file suffix
    - more robust error handling
    
    Read discussion in tdf#160222.
    
    This change could require a light revisit of the
    sf_session help page.
    
    Change-Id: I1fb4717db4b971bd62885ad0e38b7c08a8e6f434
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165218
    Reviewed-by: Jean-Pierre Ledure <j...@ledure.be>
    Tested-by: Jenkins

diff --git a/wizards/source/scriptforge/SF_Session.xba 
b/wizards/source/scriptforge/SF_Session.xba
index 307fb7a8f4bf..cc6e576e1c5f 100644
--- a/wizards/source/scriptforge/SF_Session.xba
+++ b/wizards/source/scriptforge/SF_Session.xba
@@ -563,9 +563,12 @@ Public Function RunApplication(Optional ByVal Command As 
Variant _
 &apos;&apos;&apos;                     The method does not validate the given 
parameters, but only passes them to the specified command
 &apos;&apos;&apos;     Returns:
 &apos;&apos;&apos;             True if success
+&apos;&apos;&apos;     Exceptions:
+&apos;&apos;&apos;             UNKNOWNFILEERROR                Command could 
not be identified as a valid file
 &apos;&apos;&apos;     Examples:
 &apos;&apos;&apos;             session.RunApplication(&quot;Notepad.exe&quot;)
 &apos;&apos;&apos;             
session.RunApplication(&quot;C:\myFolder\myDocument.odt&quot;)
+&apos;&apos;&apos;             session.RunApplication(&quot;kate&quot;)        
                                                &apos;  (Linux)
 &apos;&apos;&apos;             session.RunApplication(&quot;kate&quot;, 
&quot;/home/me/install.txt&quot;)      &apos;  (Linux)
 
 Dim bReturn As Boolean                 &apos;  Returned value
@@ -585,9 +588,23 @@ Check:
        End If
 
 Try:
-       Set oShell = SF_Utils._GetUNOService(&quot;SystemShellExecute&quot;)
-       sCommand = SF_FileSystem._ConvertToUrl(Command)
-       oShell.execute(sCommand, Parameters, 
com.sun.star.system.SystemShellExecuteFlags.DEFAULTS)
+       &apos;  Cfr. discussion tdf#160222
+       &apos;  1) Try Shell(), always in not synchronized mode
+       &apos;  2) If failure   - check command existence as a valid file
+       &apos;                                  - try 
com.sun.star.system.SystemShellExecute
+       sCommand = SF_FileSystem._ConvertFromUrl(Command)
+       On Local Error GoTo Step2
+       Shell(sCommand, , Parameters, False)
+  Step2:
+       If Err &gt; 0 Then
+               On Error GoTo 0         &apos;  Reset error status
+               If SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
+               If Not SF_FileSystem.FileExists(Command) Then GoTo 
CatchNotExists
+               Set oShell = 
SF_Utils._GetUNOService(&quot;SystemShellExecute&quot;)
+               sCommand = SF_FileSystem._ConvertToUrl(Command)
+               oShell.execute(sCommand, Parameters, 
com.sun.star.system.SystemShellExecuteFlags.DEFAULTS)
+       End If
+
        bReturn = True
 
 Finally:
@@ -596,6 +613,9 @@ Finally:
        Exit Function
 Catch:
        GoTo Finally
+CatchNotExists:
+       SF_Exception.RaiseFatal(UNKNOWNFILEERROR, &quot;Command&quot;, Command)
+       GoTo Finally
 End Function   &apos;  ScriptForge.SF_Session.RunApplication
 
 REM 
-----------------------------------------------------------------------------

Reply via email to