JavaScript can be used to automate tasks in both the Windows and Mac versions of CorelDRAW Graphics Suite. The simple JavaScript sample included in this tutorial will help you get started with JS scripts in CorelDRAW.
The JavaScript sample adds a watermark to the active page of your document. It calculates the page width and height, creates a new layer, and places two text objects on the page. One is the text Company Name that appears in the lower-right corner of the page. The other text object Watermark is placed in the middle of the page, at a 45-degree angle. The JS sample also specifies the color and transparency of the text objects.
To give the JavaScript sample a try, you will first need to create a JS script that includes the JS sample, from within CorelDRAW. Next, you will run the script.
Note: The procedures in this tutorial apply both to the Windows and Mac version of CorelDRAW, but the screen captures were taken on Windows.
1. Copy the following JS sample.
//Calculate the page width and height, and store the values in pWidth and pHeight.pWidth = host.ActivePage.SizeWidthpHeight = host.ActivePage.SizeHeight//alert("page width is: " + pWidth);//alert("page height is: " + pHeight);//alert("Page Size is: " + host.ActivePage.Paper);//Create a new layer, called “New Watermark”, on the active page.let lr2 = host.ActivePage.CreateLayer("New Watermark");//Create a rectangle the size of the page, and place it on the Watermark layer.let s1 = lr2.CreateRectangle(0, pHeight, pWidth, 0);//Add the artistic text Company Name to the Watermark layer, in the lower-right corner of the page.let s2 = lr2.CreateArtisticText(pWidth-3, 0.5, "Company Name");//Set the color of the text object to be 100% black.s2.Fill.UniformColor.CMYKAssign(0, 0, 0, 100);//Set the transparency of the text object to 70%.s2.transparency.ApplyUniformTransparency(70);//Add the artistic text Watermark to the Watermark layer.let s3 = lr2.CreateArtisticText(pWidth/2-3.4, pHeight/2, "Watermark", undefined , undefined, undefined, 100);//Set the color of the text object to be 100% black.s3.Fill.UniformColor.CMYKAssign(0, 0, 0, 100);//Set the transparency of the text object to 70%.s3.transparency.ApplyUniformTransparency(70);//Rotate the text object by 45 degrees.s3.Rotate(45);//Set focus back to base layer.host.ActiveLayer.Below().Activate();
//Calculate the page width and height, and store the values in pWidth and pHeight.
pWidth = host.ActivePage.SizeWidth
pHeight = host.ActivePage.SizeHeight
//alert("page width is: " + pWidth);
//alert("page height is: " + pHeight);
//alert("Page Size is: " + host.ActivePage.Paper);
//Create a new layer, called “New Watermark”, on the active page.
let lr2 = host.ActivePage.CreateLayer("New Watermark");
//Create a rectangle the size of the page, and place it on the Watermark layer.
let s1 = lr2.CreateRectangle(0, pHeight, pWidth, 0);
//Add the artistic text Company Name to the Watermark layer, in the lower-right corner of the page.
let s2 = lr2.CreateArtisticText(pWidth-3, 0.5, "Company Name");
//Set the color of the text object to be 100% black.
s2.Fill.UniformColor.CMYKAssign(0, 0, 0, 100);
//Set the transparency of the text object to 70%.
s2.transparency.ApplyUniformTransparency(70);
//Add the artistic text Watermark to the Watermark layer.
let s3 = lr2.CreateArtisticText(pWidth/2-3.4, pHeight/2, "Watermark", undefined , undefined, undefined, 100);
s3.Fill.UniformColor.CMYKAssign(0, 0, 0, 100);
s3.transparency.ApplyUniformTransparency(70);
//Rotate the text object by 45 degrees.
s3.Rotate(45);
//Set focus back to base layer.
host.ActiveLayer.Below().Activate();
2. In CorelDRAW, do one of the following:
3. In the Scripts docker or inspector, click JavaScript, and then click User Scripts.
4. Click the New button at the top of the docker or inspector, and click New Macro.
5. Paste the copied JS sample in the text editor that appears.
6. To give the script a logical name, do one of the following:
Renaming the script on Windows
You can run the Watermark script from the Scripts docker or the Scripts inspector.
1. In the Scripts docker or inspector, click JavaScript > User Scripts.
2. Double-click the script you just created.
For more information about using JavaScript with CorelDRAW, see Working with JavaScript (JS) scripts.