Running Scripts From the Script Console
The Script Console provides a single view for executing Groovy scripts and printing their output. It has predefined variables that facilitate working with widgets and users. The following topics get you started with the Script Console:
- Running a Sample Script in the Script Console
- Predefined variables available in the Script Console
- Tips for running scripts in the Script Console
The Script Console is for system operations and maintenance—it’s not for end users. Limit Script Console access to portal administrators.
Start with running the Script Console’s sample script.
Running a Sample Script in the Script Console
Here’s how to run the sample script in the Script Console:
-
Sign in as an administrator.
-
In the Product Menu, navigate to Control Panel and select System → Server Administration.
-
Click on Script. This is the Script Console. The default sample script prints the User count to the console output.
// ### Groovy Sample ### number = com.liferay.portal.kernel.service.UserLocalServiceUtil.getUsersCount(); out.println(number);
-
Click Execute and check the Script Console Output for the User count.
The Groovy sample invokes the Liferay service utility UserLocalServiceUtil
to get the user count. Then it uses out
(a built-in PrintWriter
) to write the count to the Script Console.
If you use System.out.println instead of out.println, your output is printed to Liferay’s log file rather than to the Script Console.
Predefined Script Console Variables
Here are the predefined variables available to scripts in the Script Console:
Variable | Class |
---|---|
out | java.io.PrintWriter |
actionRequest | javax.portlet.ActionRequest |
actionResponse | javax.portlet.ActionReponse |
portletConfig | javax.portlet.PortletConfig |
portletContext | javax.portlet.PortletContext |
preferences | javax.portlet.PortletPreferences |
userInfo | java.util.Map<String, String> |
Variable Usage
This script demonstrates using the actionRequest
variable to get the portal instance’s Company
:
import com.liferay.portal.kernel.util.*
company = PortalUtil.getCompany(actionRequest)
out.println("Current Company:${company.getName()}\n")
out.println("User Info:")
userInfo.each {
k,v -> out.println("${k}:${v}")
}
Tips
Keep these things in mind when using the Script Console:
- There is no undo.
- There is no preview.
- Permissions checking is not enforced for local services.
- Scripts are executed synchronously. Avoid executing scripts that might take a long time.
Use the Script Console cautiously and test your scripts on non-production systems before running them on production.
The script engine can be used outside of the Script Console, such as in a Kaleo Workflow. Using the script engine in workflows is next.