Newbie question re replace in string
Copy link to clipboard
Copied
I created an "action", to get a document's file and path by copying the following code (which I found somewhere on the web) into the console:
/* Put script title here */
console.show(); // open console
console.clear(); // clear contents
console.println(this.path); // display the current pdf's path
The action generates the following text in the console output window:
/F/VB/PythonAndPySide_Info/Python_Info/Introduction to Python Classes (Part 1 of 2) - Python Central.pdf
I am beginning to try to learn javascript. (I'm comfortable in vba and python, but I'm having trouble learning Acrobat javascript).
I want to use javascript to (1) insert a ":" after the drive letter "F"; (2) remove the first forward slash ("/"); and (3) replace the replace the remaining forward slashes with back slashes.
I tried unsuccessfully to do #3 first, but the following code produced an error message:
/* Put script title here */
console.show(); // open console
console.clear(); // clear contents
var fullname = this.path;
fullname = fullname.replace("/", "\");
console.println(fullname); // display the current pdf's path
The error message was:
SyntaxError: unterminated string literal
5: at line 6
I would very much appreciate any help anyone could give me. I don't know where on the web I could learn this stuff.
Marc
Copy link to clipboard
Copied
The JavaScript Escape character is "\". This character is used to signal JavaScript to create characters that you cannot in JavaScript like control characters (new line, return, tab, etc.) or that the next character is to be treated as a part of the string and not a terminating string character. Sine the "\" is a special character one needs to prefix it with a "\" to have the string value include the "\" as a character in the string.
You should be getting an error message in the JavaScript console indicating the the line in the JavaScript with the error.
/* Put script title here */
console.show(); // open console;
console.clear(); // clear contents;
var fullname = this.path;
fullname = fullname.replace("/", "\\");
console.println(fullname); // display the current pdf's path;
This will only replace the first occurrence of the "\". To replace the "\" globally you need to use the RegExp with the global flag.
Copy link to clipboard
Copied
Thank you. I'll read the pages to which Thom Parker referred me, one of which I think shows how regular expressions are used in Javascript (https://acrobatusers.com/tutorials/text-matching-regular-expressions)
1. Thank you for telling me:
gkaiseril wrote
This will only replace the first occurrence of the "\". To replace the "\" globally you need to use the RegExp with the global flag.
That does not explain why Acrobat gives me this error message:
SyntaxError: unterminated string literal
5: at line 6
If one replacement was made by my code, why do I get an error message?
2. I think there's a typo in the line "You should be getting an error message in the JavaScript console indicating the the line in the JavaScript with the error." i.e., I don't understand what you are saying (and I apologize for being obtuse).
3. Could I impose on you to show me how to perform this replace? Of if not, can you help me find some examples of javascript (in an acrobat context) where a regular expression replace is performed? I know how to do these things in vba and python. I will read the links that Thom Parker gave me.
Copy link to clipboard
Copied
1. Use "\\" not "\"
Copy link to clipboard
Copied
"You should be getting an error message in the JavaScript console indicating the the line in the JavaScript with the error." You did get such a message, it was
SyntaxError: unterminated string literal
5: at line 6
The thing about unterminated string literals is that JavaScript doesn't know it's unterminated until the end, so such errors need careful review of your code to ensure that all quotes are paired and all escapes (\ in strings) properly used.
Copy link to clipboard
Copied
Try the following code:
var fullname = this.path;
console.println(fullname);
fullname = fullname.replace("/","\\");
console.println(fullname);
It replaces the first occurrence of "/" with "\" and not all of the occurrences.
To replace all of the "/" and to remove the very first occurrence of the "/" you need a script like:
var fullname = this.path; // get full path name;
console.println(fullname);
fullname = fullname.replace("/", ""); // replace first "/" with a null string;
var aFullname = fullname.split("/"); // make an array of the full name;
aFullname[0] = aFullname[0].concat(":"); // add the ":' in front of the drive letter;
fullname = aFullname.join("\\"); // rejoin the array of name elements delineated using the "\";
console.println(fullname);
Copy link to clipboard
Copied
I can't thank you enough.
Can you recommend a URL where I could find tutorials on Javascript for Acrobat that start from the absolute beginning?
Or a book that I could purchase?
Thank you again,
Marc
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You'll find video tutorial, script, samples files, and JavaScript tool for Acrobat here: pdfscripting.com
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Read this:
https://acrobatusers.com/tutorials/file-paths-acrobat-javascript
And This:
https://acrobatusers.com/tutorials/splitting-and-rebuilding-strings
And this:
https://acrobatusers.com/tutorials/text-matching-regular-expressions
Use the Acrobat JavaScript Reference early and often

