Copy link to clipboard
Copied
Aloha All! Im looking for some assist on this issue
Im trying to run this script but i get this error (pasted below)
I check the preferences and under javascript i look for trusted functions buttonto fix the error, but its missing
Any idea why the button would be missing? i have adobe acrobat pro
Below i pasted the error and the script i use to run the files im trying to run with javascript in adobe pro
Thanks!
Error im getting after running script
Exception in line 2 of function top_level, script Document-Level:splitPDFByName
NotAllowedError: Security settings prevent access to this property or method.
App.trustedFunction:2:Document-Level:splitPDFByName
Script i use to run file:
// Define a trusted function for saving the document
var saveAsTrusted = app.trustedFunction(function(doc, path) {
try {
doc.saveAs(path);
console.println("Saved: " + path);
return true;
} catch (e) {
console.println("Error saving document: " + e.toString());
return false;
}
});
// Function to extract only the employee's name, stopping at the country code
function extractNameFromPage(pageNum) {
var numWords = this.getPageNumWords(pageNum);
var foundNameHeader = false;
var extractedName = "";
for (var i = 0; i < numWords; i++) {
var word = this.getPageNthWord(pageNum, i, true).trim(); // Get word with spaces preserved
if (foundNameHeader) {
// Stop capturing when reaching a two-letter uppercase country code (e.g., "US", "GB", "CA")
if (/^[A-Z]{2}$/.test(word)) {
return extractedName.trim();
}
// Exclude "Reports To" and "Country" sections
if (word.toLowerCase() !== "reports" && word.toLowerCase() !== "to" && word.toLowerCase() !== "country") {
extractedName += (extractedName ? " " : "") + word;
}
}
// Identify "Name" column header and start capturing
if (word.toLowerCase() === "name") {
foundNameHeader = true;
}
}
return extractedName.trim() || null; // Return null if no valid name was found
}
// Function to split PDF by detected employee name
function splitPDFByName() {
var numPages = this.numPages;
var lastPage = 0;
var lastName = null;
for (var i = 0; i < numPages; i++) {
var extractedName = extractNameFromPage(i);
if (extractedName) {
console.println("Captured Name on Page " + i + ": " + extractedName); // Debugging log
// If the name changes (or it's the first name found), create a new split
if (extractedName !== lastName) {
if (lastName !== null) {
saveSplitDocument(lastName, lastPage, i - 1); // Save previous name's pages
}
lastPage = i;
lastName = extractedName;
}
}
}
// Save the last segment after looping through all pages
if (lastName !== null) {
saveSplitDocument(lastName, lastPage, numPages - 1);
}
app.alert("PDF splitting complete!", 3);
}
// Function to save the split PDF in the specified directory
function saveSplitDocument(name, startPage, endPage) {
if (!name || startPage > endPage) return; // Prevent invalid saves
var sanitizedFileName = name.replace(/[^a-zA-Z0-9_-]/g, "_"); // Replace invalid characters with underscores
var savePath = "C:/temp/" + sanitizedFileName + ".pdf"; // Save to a temporary directory
console.println("Attempting to save to: " + savePath); // Debugging log
try {
var newDoc = this.extractPages(startPage, endPage);
if (newDoc) {
if (saveAsTrusted(newDoc, savePath)) {
console.println("Saved: " + savePath);
} else {
console.println("Failed to save: " + savePath);
}
newDoc.closeDoc();
} else {
console.println("Error: Failed to extract pages " + startPage + " to " + endPage);
}
} catch (e) {
console.println("Error: " + e.toString());
}
}
// Run the script
splitPDFByName();
Copy link to clipboard
Copied
You can't put a trusted function in a document level script, it must be in a folder level script.