Skip to main content
Inspiring
February 20, 2025
Question

Testing JavaScript on MAC OS

  • February 20, 2025
  • 5 replies
  • 2558 views

Hello, 

 

I have been editing a script originally made by Karl Heinz (link to the original script) and it works flawlessly on Windows OS. But after sharing this script with another pearson on MAC OS, they can't make it work. 

 

I have made sure that the obtained file path would be compatible with both Windows and MAC OS via the replace() method. 

I can't check it personally (I do not own/have access to MAC) and further investigation is quite delayed due to a different time-zone. 

 

Could anyone check the script and tell me what's going on?

app.addMenuItem ({cName: "DataMerge", cParent: "Edit", cExec: "DataMergeFx()"});
function DataMergeFx() {
	var sFilePath = this.path;
	var sMetadata = this.metadata;
	var newDoc = this.extractPages(0);
	newDoc.metadata = sMetadata;

	var tmpField = newDoc.addField("Directory", "text", 0, [0,0,100,100]);
	tmpField.hidden = true;
	tmpField.fileSelect = true;
	tmpField.browseForFileToSubmit(); 
	var txtValue = tmpField.valueAsString;
	var txtPath = "/" + txtValue.replace(/:\\/g,"/").replace(/\\/g,"/").replace(/:/g,"/");

	var err = 0;
	var idx = 0;
	while (err == 0) {
		err = this.importTextData(txtPath, idx);
		newDoc.flattenPages();
		if (err == -1)
			app.alert("Error: Cannot Open File");
		else if (err == -2)
			app.alert("Error: Cannot Load Data");
		//else if (err == -3) {}
		else if (err == 1)
			app.alert("Warning: User Cancelled File Select");
		else if (err == 2)
			app.alert("Warning: User Cancelled Row Select");
		else if (err == 3)
			app.alert("Warning: Missing Data");
		else if (err == 0) {
			newDoc.insertPages({cPath: sFilePath});
			idx++;
		}
	}
	newDoc.deletePages(0);
    var nPages = newDoc.numPages;
    for(i = nPages - 1; i>0; i--) {
        newDoc.movePage(0, i);
    }
	this.resetForm();
	this.dirty = false;
}

The person has enabled menu items JavaScript execution privileges and in the last conversation, they mentioned that the console error message looks like this: 

TypeError: this.extractPages() is not a function
5:Folder-Level:App:DataMerge.js

...which does not make sense to me. Nithing like this appears on Windows and the extractPages() method is well defined. 

 

Everything (the script file and a testing file with random namelist) can be accessed via this link

 

Thanks a lot, 
David

5 replies

Inspiring
February 20, 2025

UPDATE from the person:

They tried it both with an open document and without. 

On a question “if they are using Acrobat Reader or not” they did not respond. I guess they are using Acrobat Pro, they are using it professionaly and work in printing. 

PDF Automation Station
Community Expert
Community Expert
February 20, 2025

What is the error with an open document?

Inspiring
February 20, 2025

The same. I will as them again about the Acrobat Reader.

It seems to be it.

PDF Automation Station
Community Expert
Community Expert
February 20, 2025

"it works flawlessly on Windows OS"

I don't see how it can since the function is not a trusted function.  You have two methods that require a privileged context:

1)  Setting the field fileSelect flag

2) doc.importTextData when the cPath is specified.

Inspiring
February 20, 2025

I have thought about that but it works when you check the Enable menu items JavaScript execution privileges in the Preferences. 

 

When you check that, it does not require Privileged context. I have tried it on multiple machines. 

Also, the console would mentiontion this: 

I will post a new reply at the top. I have some more information from that person. 

PDF Automation Station
Community Expert
Community Expert
February 20, 2025

Oh, that's what that's for. ☺

PDF Automation Station
Community Expert
Community Expert
February 20, 2025

I would start by having them run this.extractPages(0) in the console and see what happens.

Inspiring
February 20, 2025

OK, that would also verify the point that try67 made about the Acrobat Reader.

 

Would you mind checking the code (if you have MAC OS, that is), if the MAC file path is correctly converted to the Acrobat Device Independent Path? 

PDF Automation Station
Community Expert
Community Expert
February 20, 2025

I'm not on a Mac.  Is it necessary when the path is coming from browseForFileToSubmit()?

Bernd Alheit
Community Expert
Community Expert
February 20, 2025

You will get the error message when no document is open.

try67
Community Expert
Community Expert
February 20, 2025

I thought about that too, but expected that the previous lines would cause the same issue. However, it might be different for properties than for methods... So yes, of course a file must be opened in the application for this to work.

try67
Community Expert
Community Expert
February 20, 2025

That is a very strange error. I don't think it's a Mac issue, though.

Are you sure they are running it in Acrobat, and not in the free Reader?

Also, in the code there are some issues with using the "this" keyword after creating the new document. At that point, "this" might not refer to the old file any longer, so it's better to keep a variable referencing it, and use that one, instead.

Inspiring
February 20, 2025

Although I'm quite certain they have access to Acrobat Pro, I'm not sure if they are using Acrobat Reader on their own machine. I will ask them about that. 

quote

Also, in the code there are some issues with using the "this" keyword after creating the new document. At that point, "this" might not refer to the old file any longer, so it's better to keep a variable referencing it, and use that one, instead.


By @try67

I never never encountered this, so thank you for pointing that out.

After checking the JavaScript Reference page and it indeed mention the word usually”

var sDoc = this;

Will this line of code suffice? 

 

Also, a related question to the script, but unrelated to this problem. 

I noticed that when you combine files, the file name mentioned in the tab is Binder1.pdf (also present after you activate the Save as dialog), but under the properties tab, the name is a temporary file with a random string of characters. Is there a way how to achieve something similar after extracting pages? 

try67
Community Expert
Community Expert
February 20, 2025

That would suffice, if you also changed all later references to "this" to be "sDoc", yes.

 

I believe Binder1 is the title of the file. The file-path is indeed a temporary file name. Which one of those do you want to achieve, exactly?