Use javascript console to create a folder rule script in Alfresco


Javascript Console is a nice tool for helping you code server-side javascript in Alfresco using the ‘repository javascript API’.  In this post I describe how to use the Javascript Console to create a script for use in a Folder Rule.  This post follows on from a previous post – Use Alfresco folder rules to manage admin tasks

Javascript console in Alfresco 5.0.d

Javascript console in Alfresco 5.0.d

Installing javascript console

Follow the javascript console link in the ‘Add-ons’ area in the Order of the Bee site to find and download the javascript console repo amp and share amp and copy to the necessary folders in Alfresco.  At the time of writing, the author ran the following commands to install in Alfresco 5.0.d.

sudo cp -v javascript-console-repo-0.6.0.amp /opt/alfresco-5.0.d/amps
sudo cp -v javascript-console-share-0.6.0.amp /opt/alfresco-5.0.d/amps_share

Install the 2 amps into Alfresco, and restart Alfresco.

cd /opt/alfresco-5.0.d/bin
sudo ./ -force
Module 'de.fme.alfresco.JavascriptConsole-repo' installed in '/opt/alfresco-5.0.d/tomcat/webapps/alfresco.war'
   -    Title:        fme Javascript Console Repository Extension
   -    Version:
   -    Install Date: Sun Oct 11 13:11:20 AEDT 2015
   -    Description:   Adminstration console module to execute arbitrary javascript code.
Module 'de.fme.alfresco.JavascriptConsole-share' installed in '/opt/alfresco-5.0.d/tomcat/webapps/share.war'
   -    Title:        fme Javascript Console Share Extension
   -    Version:
   -    Install Date: Sun Oct 11 13:11:27 AEDT 2015
   -    Description:   Adminstration console module to execute arbitrary javascript code.
sudo service alfresco restart

If Alfresco does not restart successfully and allow you to re-login to Share, reboot the server.

Writing and testing your javascript

If you already have a script in ‘Scripts’ under ‘Data Dictionary’ click ‘Load Script’ and select the script. Then select a folder for ‘run with var space’ at the top of the page – this would ordinarily be a folder in a site that contains a test file of the type that you need to test with – in my example the path started with ‘Sites/photos/documentLibrary’.

For convenience, I start the script with the following code, so that the script will work in both javascript console and a folder rule.

var doc;
if (typeof document === 'undefined') {  // in Javascript Console
	doc = space.children[0]; //0 = first file/subfolder in the 'space' defined above
} else {
	doc = document;                     // in Folder Rule

Following this is code to create thumbnails for JPGs that don’t yet have thumbnails.

// Create a small thumbnail if the file is a JPEG, if the thumbail doesn't exist already.
if (doc.mimetype=='image/jpeg') {
    // Does a doclib rendition (small thumbnail) exist for the file?
    var thumbnail = doc.getThumbnail('doclib');
    if (!thumbnail) {
        var async=true;
        // do not run asynchronously, else MANY .convert.bin processes are created and crash tomcat
        doc.createThumbnail("doclib", !async);

The logger.log() output is conveniently shown on the javascript console page just below the code, so is good for debugging.

Remember to click ‘Save script’ at some stage.

Drop-down methods list

A brilliant feature of javascript console is the drop-down display of available methods available after you type a dot after an object name. This list shows you the options in the Alfresco ‘repository javascript API’ and is a big time-saver and a great learning tool if you are unfamiliar with the API.

Javascript console showing the list of methods available for the document object

List showing some of the ‘Repository Javascript API’ methods

Documentation list

An extensive list of links to documentation is also available from within the javascript console.

List of available documentation on javascript

Javascript documentation links

Some notes on creating thumbnails

It was not my intention for this post to focus on thumbnails, but here are some observations that may be useful to some readers. The process of creating thumbnails is very CPU intensive – not something you would run on a production server in business hours. I found that if I ran ‘createThumbnail’ asynchronously, many .convert.bin processes were created and tomcat eventually crashed. When I ran the folder rule with ‘createThumbnail’ running synchronously, only one .convert.bin process would run at a time – which was desirable – but after about a minute the system would display a message – ‘Could not run rules’ – ignore this because if you run the ‘top’ command you can see that .convert.bin processes continue to be created, processing one image at a time.

You can also see the details of the ImageMagick command that is creating a 100×100 thumbnail ..

ps aux | grep "convert"
root     17453  0.0  0.1  74092  4716 pts/0    R    08:52   0:00 
-auto-orient -resize 100x100> 

Also, the thumbnail creation process eventually filled up the drive, and this was how I remedied it ..

sudo du -h -s /opt/alfresco-5.0.d/tomcat/temp/* | grep "G"
6.3G /opt/alfresco-5.0.d/tomcat/temp/Alfresco
sudo service alfresco stop
sudo rm -r /opt/alfresco-5.0.d/tomcat/temp/Alfresco
sudo reboot

Further reading

The Alfresco Wiki (although officially superseded) has good Javascript API examples –

Alfresco JavaScript API reference –

Order of the Bee – an independent organisation of the Alfresco community. Web-site includes good technical and non-technical posts and other information, focussed mainly on Alfresco Community Edition (CE).

4 thoughts on “Use javascript console to create a folder rule script in Alfresco

  1. Pingback: Use Alfresco folder rules to perform admin tasks | Geology FOSS Blog

  2. dan

    Thanks for your post — made some things easier for me.
    Do you know if I can bundle scripts for folder rules into an amp? The Alfresco documentation is a bit short on that and I’d like my users to have a handful of handy scripts for their folder rules.


  3. david8collins Post author

    I don’t know if that is possible. An alternative is to store all your useful scripts on a drive somewhere, and then simply upload them into the Data Dictionary/Scripts folder using Share when you set up an Alfresco server. The downside of this method is the ‘Title’ and ‘Description’ field for each uploaded script will be blank. Another alternative is to use the bulk import tool to upload your scripts – you can then specify a ‘Title’ and ‘Description’ for each script – use http://localhost:8080/alfresco/service/bulkfsimport to upload the scripts along with their metadata. The following article explains how to set up a metadata file for each of your scripts –



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s