Highlighted

Run JavaScript From Action Wizard Instead Of Application Level File

Explorer ,
Jun 21, 2020

Copy link to clipboard

Copied

Hey all,

 

I've been using this code in a JavaScript file saved in the application folder and calling the function from the Action Wizard but I want to paste the code from the JavaScript file into Action Wizard so I can transfer the Action Sequence across multiple computers and not have to worry about the JavaScipt file as well. Any ideas on how I would change this so runs inside of the Action Wizard? Thanks 🙂

 

 

// Enter and validate 8 digit numeric case number and add to bookmarks
function enterCase() {
	if (this.bookmarkRoot==null || this.bookmarkRoot.children==null || this.bookmarkRoot.children.length==0) {
		app.alert("Error! There are no bookmarks in this file.",0,0,"COMPANY NAME");
		event.rc = false;
		return;
	}
	var resp = "";
	while (resp=="") {
		
		var dialog1 = {
			initialize: function (dialog) {
			},
			
			commit:function (dialog) {
				var results = dialog.store();
				resp = results["txt1"];
			},
			
			description: {
				name: "COMPANY NAME",
				align_children: "align_center",
				elements: [
				{
					type: "view",
					align_children: "align_left",
					elements: [
						{
							type: "static_text",
							name: "Enter case number:",
							bold: false,
							font: "dialog"
						},
						{
							item_id: "txt1",
							type: "edit_text",
							alignment: "align_fill",
							bold: true,
							font: "dialog"
						},
						{
							alignment: "align_right",
							type: "ok_cancel",
							ok_name: "OK",
							cancel_name: "Cancel",
							bold: false,
							font: "dialog"
						}
					]
				}]
			}
		}

		if (myTrustedExecDialog(dialog1)!="ok") {
			app.alert("Error! Enter case number cancelled by user.",0,0,"COMPANY NAME");
			event.rc = false;
			return;
		}
		if (/^\d{8}$/.test(resp)==false) {
			app.alert("Error! Invalid case number. Please try again.",0,0,"COMPANY NAME");
			resp = "";
		}
	}
	for (var i=0; i<this.bookmarkRoot.children.length; i++) {
		this.bookmarkRoot.children[i].name = resp + " " + this.bookmarkRoot.children[i].name;
	}
}

myExecDialog = app.trustPropagatorFunction(function(dialogName){
	app.beginPriv();
	return app.execDialog(dialogName);
	app.endPriv();
});

myTrustedExecDialog = app.trustedFunction(function(dialogName) {
	app.beginPriv();
	return myExecDialog(dialogName);
	app.endPriv();
});

 

 

 

 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You don't need to change anything, beside add a call to the function. You can also get rid of the trusted functions and replace them with regular calls, as an Action runs in a privileged context by definition.

However, if you use this code in an Action it will prompt you for each file you process, which kind of defeats the purpose of a batch process. The code (which I believe I wrote) can be modified to only prompt the user once and then use the same information for all files.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, How to

Views

159

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Run JavaScript From Action Wizard Instead Of Application Level File

Explorer ,
Jun 21, 2020

Copy link to clipboard

Copied

Hey all,

 

I've been using this code in a JavaScript file saved in the application folder and calling the function from the Action Wizard but I want to paste the code from the JavaScript file into Action Wizard so I can transfer the Action Sequence across multiple computers and not have to worry about the JavaScipt file as well. Any ideas on how I would change this so runs inside of the Action Wizard? Thanks 🙂

 

 

// Enter and validate 8 digit numeric case number and add to bookmarks
function enterCase() {
	if (this.bookmarkRoot==null || this.bookmarkRoot.children==null || this.bookmarkRoot.children.length==0) {
		app.alert("Error! There are no bookmarks in this file.",0,0,"COMPANY NAME");
		event.rc = false;
		return;
	}
	var resp = "";
	while (resp=="") {
		
		var dialog1 = {
			initialize: function (dialog) {
			},
			
			commit:function (dialog) {
				var results = dialog.store();
				resp = results["txt1"];
			},
			
			description: {
				name: "COMPANY NAME",
				align_children: "align_center",
				elements: [
				{
					type: "view",
					align_children: "align_left",
					elements: [
						{
							type: "static_text",
							name: "Enter case number:",
							bold: false,
							font: "dialog"
						},
						{
							item_id: "txt1",
							type: "edit_text",
							alignment: "align_fill",
							bold: true,
							font: "dialog"
						},
						{
							alignment: "align_right",
							type: "ok_cancel",
							ok_name: "OK",
							cancel_name: "Cancel",
							bold: false,
							font: "dialog"
						}
					]
				}]
			}
		}

		if (myTrustedExecDialog(dialog1)!="ok") {
			app.alert("Error! Enter case number cancelled by user.",0,0,"COMPANY NAME");
			event.rc = false;
			return;
		}
		if (/^\d{8}$/.test(resp)==false) {
			app.alert("Error! Invalid case number. Please try again.",0,0,"COMPANY NAME");
			resp = "";
		}
	}
	for (var i=0; i<this.bookmarkRoot.children.length; i++) {
		this.bookmarkRoot.children[i].name = resp + " " + this.bookmarkRoot.children[i].name;
	}
}

myExecDialog = app.trustPropagatorFunction(function(dialogName){
	app.beginPriv();
	return app.execDialog(dialogName);
	app.endPriv();
});

myTrustedExecDialog = app.trustedFunction(function(dialogName) {
	app.beginPriv();
	return myExecDialog(dialogName);
	app.endPriv();
});

 

 

 

 

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You don't need to change anything, beside add a call to the function. You can also get rid of the trusted functions and replace them with regular calls, as an Action runs in a privileged context by definition.

However, if you use this code in an Action it will prompt you for each file you process, which kind of defeats the purpose of a batch process. The code (which I believe I wrote) can be modified to only prompt the user once and then use the same information for all files.

TOPICS
Acrobat SDK and JavaScript, Edit and convert PDFs, How to

Views

160

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 21, 2020 1
Adobe Community Professional ,
Jun 21, 2020

Copy link to clipboard

Copied

Paste the code there where you call the function.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 21, 2020 2
Most Valuable Participant ,
Jun 22, 2020

Copy link to clipboard

Copied

You don't need to change anything, beside add a call to the function. You can also get rid of the trusted functions and replace them with regular calls, as an Action runs in a privileged context by definition.

However, if you use this code in an Action it will prompt you for each file you process, which kind of defeats the purpose of a batch process. The code (which I believe I wrote) can be modified to only prompt the user once and then use the same information for all files.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 22, 2020 2
Explorer ,
Jun 22, 2020

Copy link to clipboard

Copied

Thanks Bernd and Try for your reply. You are correct Try this is your code from a few years back I actually emailed you for some rework but I didn't hear back and I really needed to get this sorted by today. I took both of your advice and I still cannot get it to run without error. Bernd I tried pasting the code as is it won't run. I also tried calling the function at the top of the code using enterCase(); and I keep getting "redeclaration of const" and "return not in function" errors. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 22, 2020 1
Most Valuable Participant ,
Jun 22, 2020

Copy link to clipboard

Copied

I didn't see any email from you... Are you sure you used the right email address (try6767 at gmail.com)?

Try again, please.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 22, 2020 1
Explorer ,
Jun 22, 2020

Copy link to clipboard

Copied

Yup thats the one sent on Thursday of last week. I included a text file but I can't see that being an issue. I just forwarded it again.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 22, 2020 1
Community Beginner ,
Oct 19, 2020

Copy link to clipboard

Copied

Hi there - I'm having a similar issue moving a script into Action Wizard. You mention altering the script so that it only prompts the user once. I've written the below to watermark files, and the dialog is prompting the user for each file when multiple files are selected. I'd love to know how one can alter this so that it works for all selected files with only one prompt. Many thanks in advance for your help!

function addWMfun() {
    // Add watermark
    event.target.addWatermarkFromText({
        cText: WaterName,
        nTextAlign:app.constants.align.center,
        cFont: "Arial",
        nScale: -1,
        nRotation: 45,
        nOpacity: 0.06
    });
}

function saveWMfun(){
    // Build File Name addition from Name
    var cExt = (" - " + WaterName + ".pdf");

    //Add the extension to the end of the file name and create save path
    var cDstName = event.target.documentFileName.replace(/\.pdf$/,cExt);
    var cDstPath = "/Users/Ben/Desktop/" + cDstName;

    // Save the file
    event.target.saveAs(cDstPath); 
}

var WaterName = app.response({ cQuestion: "Enter Watermark Text", cTitle: "Watermark text"});
addWMfun();
saveWMfun();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 0
Community Beginner ,
Oct 19, 2020

Copy link to clipboard

Copied

Hi there - I'm having a similar issue moving a script into Action Wizard. You mention altering the script so that it only prompts the user once. I've written the below to watermark files, and the dialog is prompting the user for each file when multiple files are selected. I'd love to know how one can alter this so that it works for all selected files with only one prompt. Many thanks in advance for your help!

function addWMfun() {
    // Add watermark
    event.target.addWatermarkFromText({
        cText: WaterName,
        nTextAlign:app.constants.align.center,
        cFont: "Arial",
        nScale: -1,
        nRotation: 45,
        nOpacity: 0.06
    });
}

function saveWMfun(){
    // Build File Name addition from Name
    var cExt = (" - " + WaterName + ".pdf");

    //Add the extension to the end of the file name and create save path
    var cDstName = event.target.documentFileName.replace(/\.pdf$/,cExt);
    var cDstPath = "/Users/Ben/Desktop/" + cDstName;

    // Save the file
    event.target.saveAs(cDstPath); 
}

var WaterName = app.response({ cQuestion: "Enter Watermark Text", cTitle: "Watermark text"});
addWMfun();
saveWMfun();

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 0
Most Valuable Participant ,
Oct 19, 2020

Copy link to clipboard

Copied

You can use a global variable, and check if it's filled in or not. Then at the end of the process you will need to use a different script to delete it, or just restart Acrobat.

So replace this line:

 

var WaterName = app.response({ cQuestion: "Enter Watermark Text", cTitle: "Watermark text"});

With this:

if (global.WaterName==null) global.WaterName = app.response({ cQuestion: "Enter Watermark Text", cTitle: "Watermark text"});

 

And then change this line:

 

cText: WaterName,

 

With this:

 

cText: global.WaterName,

 

When the Action is done run this code to reset this variable (from the JS Console, for example):

 

delete global.WaterName;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 0
Community Beginner ,
Oct 19, 2020

Copy link to clipboard

Copied

Thanks very much, this works a treat.

One issue: Is there any way to automatically erase the global variable once the process is complete? I'm making this for users who will not be able to clear it in the console themselves.

Perhaps if I put a second Javascript action in the Action as a whole which clears the variable?

Thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 0
try67 LATEST
Most Valuable Participant ,
Oct 19, 2020

Copy link to clipboard

Copied

No, because you can't know which is the last (or first) file...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 19, 2020 0