1

I am new to macros.

Objective:

Make the python script locally on the system and add code to it(to spawn a shell) and finally execute the script all using LibreOffice macros.

Code:

Sub Main

    Shell("bash -c 'touch openshell.py'")
    Shell("bash -c 'echo import os > openshell.py'")
    Shell("bash -c 'echo os.system("gnome-terminal 'sudo apt-get update'") >> openshell.py'")
    Shell("bash -c 'python -m openshell.py'")

    
End Sub

Error:

BASIC syntax error.
Parentheses do not match.

Interestingly, openshell.py script is created and the import os command is working.

But there is an issue with " escaping in the Shell("bash -c 'echo os.system("gnome-terminal 'sudo apt-get update'") >> openshell.py'")

How to properly escape and add the above python code to the already created openshell.py without an error using macros and execute it ?

Danish Xavier
  • 1,225
  • 1
  • 10
  • 21
  • This is just a syntax error - look at the image of the code in your question. Do you see? Did the codeformatter highlight the word gnome-terminal in a different color? This is what the message "Parentheses do not match" says - double quotes inside a string in double quotes must be specified twice. `Shell("bash -c 'echo os.system(""gnome-terminal 'sudo apt-get update'"") >> openshell.py'")` – JohnSUN Mar 19 '21 at 06:17
  • Thanks, I guess this works as I run the code there was no error generated. But when I opened the openshell.py the command ```os.system("gnome-terminal 'sudo apt-get update'")``` is not present only ```import os``` is present. why is it not appending the code to thee py script and how to execute it . can you give it as an answer, I can upvote the answer if it works? – Danish Xavier Mar 19 '21 at 06:25
  • 2
    Perhaps `Shell (bash echo ...)` is not the best way to create a file. See an [**example**](https://help.libreoffice.org/7.1/en-US/text/sbasic/shared/03020103.html) – JohnSUN Mar 19 '21 at 06:39
  • Try double single quotes also (apostrophes). I mean, probably `'sudo apt-get update'` should be written as `''sudo apt-get update''` - this is just an assumption. The proposal to use the "classic" basic techniques to create a text file remains in force. – JohnSUN Mar 19 '21 at 07:11
  • This is a bad idea, being a possibly serious security issue. The python script should be delivered separately, allowing the user to install it themselves, with the ability to inspect it first. What is to stop you creating an on-the-fly script that says `rm -r *`? I'm not suggesting you would but the possibilty is there. – Rolf of Saxony May 26 '21 at 08:37

1 Answers1

0

String and file handling syntax in Basic is tricky. The good news is that LibreOffice macros can be written in python. Use APSO to run the following macro.

def create_and_run_script():
    filename = "path/to/openshell.py"
    with open(filename, 'w') as out_file:
        out_file.write("""
import os
os.system("gnome-terminal 'sudo apt-get update'")
""")

Then see What is the best way to call a script from another script?.

However, that seems unnecessarily complex. The code below is a perfectly legitimate macro and could be run with either APSO or Tools -> Macros -> Run Macro.

import os

def do_update():
    os.system("gnome-terminal 'sudo apt-get update'")
Jim K
  • 12,824
  • 2
  • 22
  • 51