How can I create a button to start a java script?

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
apee
User
Posts: 5
Joined: Tue Feb 28, 2017 6:20 pm

How can I create a button to start a java script?

Post by apee »

Hello,

I have found on this site a wonderful java script which reverses the order of the pages of a document:

for (i = this.numPages - 1; i >= 0; i--) this.movePage(i);

Can anyone give me a hint how to create a button to start this script?

Thank you and best regards,
Apee
User avatar
Patrick-Tracker Supp
Site Admin
Posts: 1645
Joined: Thu Mar 27, 2014 6:14 pm
Location: Vancouver Island
Contact:

Re: How can I create a button to start a java script?

Post by Patrick-Tracker Supp »

Hello Aapee,

Thanks for the post and welcome to our forums. Do you mean a toolbar button, or a button on the page of a PDF?

Thanks!
If posting files to this forum, you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded.
Thank you.

Cheers,

Patrick Charest
Tracker Support North America
Willy Van Nuffel
User
Posts: 2347
Joined: Wed Jan 18, 2006 12:10 pm

Re: How can I create a button to start a java script?

Post by Willy Van Nuffel »

If it goes about adding a button via the automatically generated "Add-on Tools" toolbar, then there was recently a very useful solution. You just have to incorporate the javascript in a text file with *.js extension and put this file in the JavaScripts folder of your PDF-XChange Editor installation.

Something like this:
C:\Program Files\Tracker Software\PDF Editor\JavaScripts\ReversePageOrder.js

The content of the file might be something like this (sample file in attachment):

function ReversePageOrder()
{
for (i = this.numPages - 1; i >= 0; i--) this.movePage(i);
}

app.addToolButton({
cName: "Reverse Page Order",
cExec: "ReversePageOrder()",
cTooltext: "Reverse pages",
cEnable: true,
nPos: 0
});

Best regards.
Attachments
ReversePageOrder.zip
(305 Bytes) Downloaded 170 times
apee
User
Posts: 5
Joined: Tue Feb 28, 2017 6:20 pm

Re: How can I create a button to start a java script?

Post by apee »

Hello Patrick (thank you for welcoming me) and hello Willy,

Wow, it works! I am absolutely delighted, that is exactly what I had hoped. Thank you for completed the script for me!

Best regards
Apee
User avatar
Patrick-Tracker Supp
Site Admin
Posts: 1645
Joined: Thu Mar 27, 2014 6:14 pm
Location: Vancouver Island
Contact:

Re: How can I create a button to start a java script?

Post by Patrick-Tracker Supp »

Hi Willy,

You deserve an award for that one! It works absolutely perfectly :)

Take care!
If posting files to this forum, you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded.
Thank you.

Cheers,

Patrick Charest
Tracker Support North America
Willy Van Nuffel
User
Posts: 2347
Joined: Wed Jan 18, 2006 12:10 pm

Re: How can I create a button to start a java script?

Post by Willy Van Nuffel »

It was Will - Tracker Support who made us aware of this ...
https://forum.pdf-xchange.com/ ... pt#p110263

So, he deserves an award too!

:-)
apee
User
Posts: 5
Joined: Tue Feb 28, 2017 6:20 pm

Re: How can I create a button to start a java script?

Post by apee »

Awards for all of you!

I am very pleased to see so many helpful people here!
User avatar
Patrick-Tracker Supp
Site Admin
Posts: 1645
Joined: Thu Mar 27, 2014 6:14 pm
Location: Vancouver Island
Contact:

Re: How can I create a button to start a java script?

Post by Patrick-Tracker Supp »

:D
If posting files to this forum, you must archive the files to a ZIP, RAR or 7z file or they will not be uploaded.
Thank you.

Cheers,

Patrick Charest
Tracker Support North America
User avatar
yogi108
User
Posts: 74
Joined: Thu Mar 09, 2017 9:13 am
Location: Austria, Theiß
Contact:

Re: How can I create a button to start a java script?

Post by yogi108 »

Hi there,

Just saw the thermometer object, tried it, here is the result for the sorting function-
good idea to see something on the screen even just sorting 10 pages ....

Code: Select all

function ReversePageOrder()
{
var box=app.alert("Change sortorder?",2,2,"reverse page order");

if(box==4){  // yes

	var t = app.thermometer; // acquire a thermometer object
	t.duration = this.numPages;
	t.begin();
	var i1=0;
	for (i = this.numPages - 1; i >= 0; i--)
	{
		i1++;
		t.text = "Page " + (i1);
		t.value = i1;
		this.movePage(i);
		
		if (t.cancelled) break;
	}
	t.end();   
  
} 
else if(box==false){ 

	app.alert("Exit");

} 

}
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17823
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: How can I create a button to start a java script?

Post by Tracker Supp-Stefan »

Thanks for this Yogi,

It is indeed a cool feature - tried it on a 750 page document - looked lovely :)

Cheers,
Stefan
Karyudo
User
Posts: 13
Joined: Sun Oct 13, 2019 9:48 pm

Re: How can I create a button to start a java script?

Post by Karyudo »

Is there a video or page that explains how to get ReversePageOrder working? Because there's not enough information in this thread.

I don't want to make this my life-long pursuit; I just want the feature to work. Every second I spend in forums reading half-explained hacks annoys me more. Especially when I follow directions pretty exactly, and don't get the results that everyone else raves about.

Ideally, I wouldn't have to screw around with JavaScript at all, and a ReversePageOrder button would just exist in the base software. But if I'm here already, I guess that's not going to happen.

Second best would be complete explanations that don't assume ANYTHING, and step a casual user through the setup. A little help, someone...?
User avatar
Radi - Tracker Supp
Site Admin
Posts: 597
Joined: Tue Mar 03, 2015 12:46 pm

Re: How can I create a button to start a java script?

Post by Radi - Tracker Supp »

Hi Karyudo,

Please check my answer in your dedicated topic about this Javascript button:
viewtopic.php?f=7&t=33303&p=137305#p137305

Regards,
Radi
User avatar
David.P
User
Posts: 1510
Joined: Thu Feb 28, 2008 8:16 pm

Re: How can I create a button to start a java script?

Post by David.P »

Hello Yogi and @all,
yogi108 wrote: Wed Mar 29, 2017 10:35 am Just saw the thermometer object, tried it, here is the result for the sorting function-
good idea to see something on the screen even just sorting 10 pages ....

Code: Select all

function ReversePageOrder()
{
var box=app.alert("Change sortorder?",2,2,"reverse page order");

if(box==4){  // yes

	var t = app.thermometer; // acquire a thermometer object
	t.duration = this.numPages;
	t.begin();
	var i1=0;
	for (i = this.numPages - 1; i >= 0; i--)
	{
		i1++;
		t.text = "Page " + (i1);
		t.value = i1;
		this.movePage(i);
		
		if (t.cancelled) break;
	}
	t.end();   
  
} 
else if(box==false){ 

	app.alert("Exit");

} 

}
Thanks for this code.

Should this work as described here:
https://forum.pdf-xchange.com/viewtopic.php?p=137305#p137305
:?:

With this JavaScript, no button appears for me in PDF-XChange Editor. Am I doing something wrong?

Best regards
David
David.P
PDF-XChange Pro
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: How can I create a button to start a java script?

Post by TrackerSupp-Daniel »

Hello, David.P

This code does not include and directions to create a JS button, so it would simply try to run once and then stop. To add a JS button to the UI, you need to structure the Button logic into the JS you are writing.
Here is some sample code for a re-order pages tool, with button logic included:

Code: Select all

// Change file ext. to .js. Then save to C:\Program Files\Tracker Software\PDF Editor\JavaScripts

function ReorderPagesA1B1()
{
   //Reorder Pages A1, B1, A2, B2, A3, B3...

	for (i = 0; i < ((this.numPages - 1)/2); i++) 
	{
		this.movePage(Math.ceil(this.numPages/2)+i,2*i);
	}
}

app.addToolButton({
    cName: "Re-order Pages",
	cLabel: "Re-Order Pages",
    cExec: "ReorderPagesA1B1()",
    cTooltext: "Reorder Pages A1, B1, A2, B2, A3, B3",
    cEnable: true,
    nPos: 0
});
Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
User avatar
David.P
User
Posts: 1510
Joined: Thu Feb 28, 2008 8:16 pm

Re: How can I create a button to start a java script?

Post by David.P »

Hello Daniel,

thanks very much, that works now.

However, I thought Yogi's code would produce a prompt and something like a progress bar.

This no longer seems to be included in your code, correct?
David.P
PDF-XChange Pro
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

Re: How can I create a button to start a java script?

Post by TrackerSupp-Daniel »

Hello, David.P

My code example is not adapted to include anything from this thread, I was sharing the button code sepcifically, the re-order pages section was an unrelated bonus shared for posterity.

If you wish for a progress bar to appear however, that woudl also need to be coded. Nothing performed with JS will show any dialogues by default, all messages, buttons, date selection fields, and error messages need to be hand coded into the JS itself in order to be displayed.

Kind regards,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
User avatar
David.P
User
Posts: 1510
Joined: Thu Feb 28, 2008 8:16 pm

Re: How can I create a button to start a java script?

Post by David.P »

Thank you Daniel,

now I get how to do it.

For once, I have extended Yogi's example with the code block "app.addToolButton({..." from your code. This way, Yogi's code also appears as a button in PDF-XChange Editor.

Yogi's code also indeed creates a prompt and a progress bar.

However, your code is easily a hundred times faster in execution than Yogi's. I don't know if this is due to the algorithm or due to the implementation of the progress bar.

Anyway, the issue is resolved from my side.

Thanks again
David
David.P
PDF-XChange Pro
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8436
Joined: Wed Jan 03, 2018 6:52 pm

How can I create a button to start a java script?

Post by TrackerSupp-Daniel »

:)
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
Post Reply