4

Is there a way for adding new Processing scripts into Processing toolbox using PyQGIS?

In the GUI, I can click on "Add script to toolbox", but I want to add scripts using PyQGIS.

Add script by GUI

QGIS version: 3.16

Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Jan 18, 2021 at 12:13
1

3 Answers 3

7

If you want to have the same action as Add Script to Toolbox..., execute the code below :

# import the script
from processing.script.AddScriptFromFileAction import AddScriptFromFileAction
# get the reference to the current processing toolbox
toolbox = iface.mainWindow().findChild(QgsDockWidget, "ProcessingToolbox")
# create an instance
script_add = AddScriptFromFileAction()
# add the reference to the current toolbox
script_add.toolbox = toolbox
# execute the script to open a Python script
script_add.execute()

You can look at what the AddScriptFromFileAction do in this script.

answered Jan 18, 2021 at 13:31
2
  • 1
    Thank you for your solution, but i want only actualize scripts in scripts folder, because i copy script files in openProject macro function. Problem is in copying files, when files are copied after open project. Commented Jan 18, 2021 at 13:39
  • Look at this code line : github.com/qgis/QGIS/blob/… Commented Jan 18, 2021 at 13:40
5

You can do that using the code snippet below (adapted from the QGIS Resource Sharing plugin):

import os.path
import shutil
import glob
from processing.script import ScriptUtils
my_scripts_dir = "/path/to/your/scripts/"
count = 0
qgis_scripts_dir = ScriptUtils.defaultScriptsFolder()
# Copy scripts from your script dir to QGIS script dir
for filename in glob.glob(os.path.join(my_scripts_dir, '*.py')):
 try:
 shutil.copy(filename, qgis_scripts_dir)
 count += 1
 except OSError as e:
 print("Couldn't install script '{}'!".format(filename))
# Finally, refresh the algorithms for the Processing script provider
if count:
 QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
 

Note: If you are going to add your scripts while QGIS is starting, you will need to check that the script provider is already set. Something like:

if QgsApplication.processingRegistry().providerById('script'):
 ...
answered Jan 18, 2021 at 15:05
1

Just copy the pythonscript to the user profiles folder, e.g. under windows it would be: C:\Users\username\AppData\Roaming\QGIS\QGIS3\profiles\profilename\processing\scripts

as a python script it would be something like:

import shutil
shutil.copy2('/src/dir/myprocess.py', 'C:/Users/username/AppData/Roaming/QGIS/QGIS3/profiles/profilename/processing/scripts/myprocess.py')

you have to adjust username and profilename in the path. the default profilename is in QGIS: default

answered Jan 18, 2021 at 13:24
2
  • 1
    Yes, is good solution, but in opened QGIS project does not work it. Script (file) is not changed after copying. Changes in script will be accepted after reopen project. Commented Jan 18, 2021 at 13:30
  • ok that is true... Commented Jan 18, 2021 at 13:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.