Copy link to clipboard
Copied
I read some other posts here and managed to get enough info to get a save as script working in acrobat to save a file and generate the filename based of a couple of form fields, however I need a bit of help with refining the script to make it work better.
Here is the script I have so far which works:
/* saveAS */
// determine the directory path for the current document
var directory = "";
this.path.substring(0, this.path.lastIndexOf('/') +1);
if (typeof(mySaveAs) == "function") {
mySaveAs(this, directory, "W.O." + " " + this.getField("W.O. #").value + " " + this.getField("Work Site Address").value + " " + this.getField("Date").value + ".pdf");
} else {
app.alert("Missing Save Function. Please contact forms administrator ");
}
The above script generates the filename, however I need it to subtract part of the data in the form field Work Site Address as the worksite address contains a string like "123 First St. Toronto, ON" for example. I'd like it to omit the city (Toronto) and province (ON) from the filename. Basically anything after the street designation which is followed by a period should be omitted. Is this possible to have it omit part of the field contents when creating the filename so I don't have to completely reformat the layout of my document to add new fields for city and province? I'd like to keep them all on 1 line in the above format if possible.
Also the date format should be set to mmm d, yyyy in the filename but still display as mmmm d, yyyy in the form field. How can I make it do this?
Right now if the field contains a comma it won't allow the script to save as. Why would this be the case? If I manually save as it allows commas in the filename.
Running Acrobat 11.0.23 & Window 8.1
Copy link to clipboard
Copied
If there's always a period in the text, and only one, then you can split using it as the delimiter and then use the first item in the resulting array, like this:
this.getField("Work Site Address").valueAsString.split(".")[0]
Re the comma bug: Yes, it's a very odd limitation of saveAs in older versions of Acrobat and Reader. I believe it was fixed later on, though.
Copy link to clipboard
Copied
This works. I still need the period in the filename though. I tried a few differnet things to get it to keep the period in there, best I could come up with is adding it before the space in the filename like so
this.getField("Work Site Address").valueAsString.split(".")[0] + ". "is this the best solution?
For the date I figured I can get it to work sorta by not putting the full date in the textfield. Basically enter Feb 1 2020 as the date rather than February 1, 2020 and change the field properties under format to display as mmmm d, yyyy in the form. This makes it display correctly in the form and won't affect the filename as long as the user doesn't put a comma when entering the date.
Anyone got any ideas how to get the script to format the date with a comma in the filename? Also ideally if different users enter different date formats could a script make a standard format just for the filename so it doesn't break the script function?
Copy link to clipboard
Copied
- Yes, that's a good way of adding the period back. There's no need to look for a complex solution if a simple one works just as well...
- Changing the Format setting of the field is not enough, as that does not affect the field's actual value, only how it is presented. I usually use a function I wrote to remove all illegal characters (including the commas) from text inputted by the user when I want to use it for a file's name. I suggest you do the same.
- And no, there's no way to force Acrobat to allow you to use the comma in the file-name. You just have to learn to live without it.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more