CorelDRAW Community
CorelDRAW Community
  • Site
  • User
  • Site
  • Search
  • User
Developer Area
Developer Area
Docs & Tutorials Using JavaScript with CorelDRAW
  • Forums
  • Wikis
  • API References
  • Tags
  • More
  • Cancel
  • New
Developer Area requires membership for participation - click to join
  • +Addons: Extending Application Functionality with VBA and VSTA
  • -General Articles
    • Controlling CorelDRAW or Corel DESIGNER Applications From Other Processes
    • Creating Color Palettes
    • Creating Custom Outline Enhanced Pattern Styles
    • Creating VBA Macros with User Interface in CorelDRAW and Corel DESIGNER
    • Macro Management in CorelDRAW Graphics Suite and CorelDRAW Technical Suite
    • Storing Custom Information in Documents
    • Using Corel Query Language (CQL) to Search for Objects in CorelDRAW and Corel DESIGNER Documents
    • Using JavaScript with CorelDRAW

Using JavaScript with CorelDRAW

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.

To create the script

1. Copy the following JS sample.

//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);

//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();

2. In CorelDRAW, do one of the following:

  • (Windows) Click Window > Dockers > Scripts.
  • (macOS)  Click Window > Inspectors > Scripts.

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:

  • (Windows) Click twice the new script under UserScripts in the Scripts docker, and type Watermark.
  • (macOS) In Finder, choose the Go menu > Go to Folder, and then navigate to Library > Application Support > Corel > CorelDRAW Graphics Suite 2019 > Draw > Scripts. Click the script twice, and type Watermark.

Renaming the script on Windows

Notes: 

  • On Windows, user-created JS scripts are saved to the respective user folder, in the following subfolder:
    • AppData\Roaming\Corel\CorelDRAW Graphics Suite 2019\Draw\Scripts
  • On the macOS, user-created scripts are saved to
    • /Users/<<USER>>/Library/Application Support/Corel/CorelDRAW Graphics Suite 2019/Draw/Scripts
  • You can try this JS sample with CorelDRAW Technical Suite 2019. Simply copy the JS file to your user AppData\Roaming\Corel\CorelDRAW Technical Suite 2019\Draw\Scripts folder.

To run the script

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.

From here...

For more information about using JavaScript with CorelDRAW, see Working with JavaScript (JS) scripts.

  • Share
  • History
  • More
  • Cancel
Related
Recommended

© Corel Corporation. All rights reserved. The content herein is in the form of a personal web log ("Blog") or forum posting. As such, the views expressed in this site are those of the participants and do not necessarily reflect the views of Corel Corporation, or its affiliates and their respective officers, directors, employees and agents. Terms of Use / Privacy​ ​/ ​Cookies / Terms and Conditions / User Guidelines.