Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old November 5th, 2012, 01:17 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Determine if a text frame exists

Is it possible to determine whether a text frame of a certain name exists?

I'm populating (via OnRecordStart) a series of text frames with variable information and if the text frame doesn't exist for a version, I don't want FP to attempt to assign content to it. I realize that this won't be an issue during composition but I'm mainly asking in order to not get errors at validation.

Here's what I'm trying to do:
Code:
(FindTextFrame("Rate1_" + Field("version")).exists) ? FindTextFrame("Rate1_" + Field("version")).content = Field("rate1") : "";
(FindTextFrame("Rate2_" + Field("version")).exists) ? FindTextFrame("Rate2_" + Field("version")).content = Field("rate2") : "";
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #2  
Old November 5th, 2012, 02:20 PM
scubajbc scubajbc is offline
Junior Community Member
 
Join Date: Sep 2008
Posts: 32
Default Re: Determine if a text frame exists

You should be able to use...

try
{
whatever code
}

catch (e)
{
if whatever code errors, run this code
}
Reply With Quote
  #3  
Old January 12th, 2018, 12:26 PM
ThomasLewis's Avatar
ThomasLewis ThomasLewis is offline
Senior Contributor
 
Join Date: Feb 2011
Posts: 213
Default Re: Determine if a text frame exists

Sorry to pull up such an old post but I was unable to find anything similar. I'm running into the same issue where I need to check if a frame exists in validation.

Using the Try/Catch method still causes the validator to return "null". Is there any way around that?
Reply With Quote
  #4  
Old January 16th, 2018, 10:23 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,409
Default Re: Determine if a text frame exists

Quote:
Originally Posted by ThomasLewis View Post
Sorry to pull up such an old post but I was unable to find anything similar. I'm running into the same issue where I need to check if a frame exists in validation.

Using the Try/Catch method still causes the validator to return "null". Is there any way around that?
I don't know what you mean. I'm not very good at guessing what's in your rule without seeing it.

A try/catch statement doesn't make a rule return anything, unless there's a return statement inside the catch block.

Also, you should be calling FindTextFrame inside of the OnRecordStart callback rule, which doesn't need to return anything anyway.

So... I'm not sure what the problem is in some code that I can't see.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #5  
Old January 16th, 2018, 04:08 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,409
Default Re: Determine if a text frame exists

Maybe this will help. Here's how I would answer the original question. Again, these examples would all be in the OnRecordStart rule.
Code:
try
{
    FindTextFrame("Rate1_" + Field("version")).content = Field("rate1");
}
catch (e)
{
}

try
{
    FindTextFrame("Rate2_" + Field("version")).content = Field("rate2");
}
catch (e)
{
}
Or, more generally:
Code:
var myFrame;
try
{
    myFrame = FindTextFrame("your frame name");
}
catch (e)
{
    Print('Text frame not found!'); // <- or skip this if you want
}
if (myFrame)
{
    myFrame.content = "Hello there!"; // <- or whatever else you need to do
}
You could even make a function:
Code:
function TryToFindTextFrame(name)
{
    try
    {
        return FindTextFrame(name);
    }
    catch (e)
    {
        Print('Text frame "' + name + '" not found!'); // <- or skip this if you want
        return null;
    }
}
And call it like so:
Code:
var myFrame = TryToFindTextFrame("your frame name");
if (myFrame)
    myFrame.content = "Hello there!";
Or you could make a function to do whatever you need:
Code:
function SetTextFrameContent(frameName, content)
{
    try
    {
        FindTextFrame(frameName).content = content;
    }
    catch (e)
    {
        Print('Text frame "' + frameName+ '" not found!'); // <- or skip this if you want
    }
}
And simply call that:
Code:
SetTextFrameContent("your frame name", "Hello there!");
Or, back to the original question:
Code:
SetTextFrameContent("Rate1_" + Field("version"), Field("rate1"));
SetTextFrameContent("Rate2_" + Field("version"), Field("rate2"));
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

Last edited by Dan Korn; January 16th, 2018 at 05:48 PM..
Reply With Quote
  #6  
Old January 17th, 2018, 11:50 AM
ThomasLewis's Avatar
ThomasLewis ThomasLewis is offline
Senior Contributor
 
Join Date: Feb 2011
Posts: 213
Default Re: Determine if a text frame exists

Those examples were all very helpful.

I'll try to explain where the issue is. I have a series of templates that all use very similar javascript for the OnRecordStart callback but also require the operator to modify the text frames on a per job basis.

Instead of maintaining all the code in each template I am using the Load function to bring in a single JS file. These templates will be used by operators that would be unable to diagnose javascript issues but can understand errors returned by validation.

The thought was to have them simply open OnRecordStart and tap the validate button. If everything checks out, great. But if one of the text frames is missing, then it would return an error specified in the script.

This is where my confusion came in. If you put return "test" directly into OnRecordStart, it will actually return "test" to the user. However, if you put return "test", or anything else, into an externally loaded JS file, it returns "null".

I was attempting the try/catch method and instead of nothing happening, I wanted to use return to return an actual "hey, you forgot to put in the text frame", instead it returns "null".

So, the examples all work fine, I guess the real issue is I am unable to return a custom message when text frame is not found.
Reply With Quote
  #7  
Old January 17th, 2018, 01:52 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,409
Default Re: Determine if a text frame exists

Quote:
Originally Posted by ThomasLewis View Post
Instead of maintaining all the code in each template I am using the Load function to bring in a single JS file. These templates will be used by operators that would be unable to diagnose javascript issues but can understand errors returned by validation.
Okay. The explanation is somewhat helpful. But still, I don't have any of your files, so I can only look at this through a very narrow keyhole.
Quote:
Originally Posted by ThomasLewis View Post
The thought was to have them simply open OnRecordStart and tap the validate button. If everything checks out, great. But if one of the text frames is missing, then it would return an error specified in the script.

This is where my confusion came in. If you put return "test" directly into OnRecordStart, it will actually return "test" to the user. However, if you put return "test", or anything else, into an externally loaded JS file, it returns "null".
That's not true. At least that's not true in my experience. I've written plenty of external JS files, and put returns in them, and had the return value show up when I validate a rule that's loading an external JS file and calling functions in it.

I suspect that you're putting the "return" in the wrong place. The thing about an external JS file is that it doesn't "return" anything. Your external JS file can have one or more functions in it, and your rules can call those functions, and each function can return something, but that's not the same as just putting a "return" statement in the JS file, outside of any function.

Again, though, instead of having to guess at what's in your JS file, it would be easier to for me diagnose it if I could look at it.

Furthermore, if there's really an error that you want the template designer to see, then instead of simply returning a message, you probably want to throw it instead, so that it'generates an exception. This has a couple of advantages over simply a return value: First, the box that pops up when you click Validate will have an error icon with it, which denotes an error condition much more explicitly than just the default message box with the title "Expressions OK". Second, the error message pops up when you try to save the rule, even if you don't click Validate. More about how to throw an exception instead of just returning a message below....

Also, putting on my moderator hat, it seems that your question is not really about determining whether a frame exists (the original question in this thread). Your question is really about how to return an error from an external JS file to the Rule Editor at validation time. So it really should be a new thread for this new question. And even if we figure it out the answer to your question here, that answer will only serve to confuse anyone who finds this thread later and just wants to know the answer to the original question, especially since it's highly unlikely that they'll be using an external JS file.
Quote:
Originally Posted by ThomasLewis View Post
I was attempting the try/catch method and instead of nothing happening, I wanted to use return to return an actual "hey, you forgot to put in the text frame", instead it returns "null".
Again, I think that's because you're returning something in the wrong place, but I can't really know for sure without seeing your code.
Quote:
Originally Posted by ThomasLewis View Post
So, the examples all work fine, I guess the real issue is I am unable to return a custom message when text frame is not found.
"... from an external JS file," is really the end of that statement. The code I provided in my previous post does answer the original question.

So, let's try to answer your question about returning an error from an external JS file.

First of all, what's wrong with the exception message that gets thrown by default when the a frame with the name passed to FindTextFrame isn't found? You know, "Error in FindFrame, no frame named 'Rate1_whatever'"?

Anyway, since you don't like that message, you can certainly throw whatever exception message you want, like so:
Code:
function TryToFindTextFrame(name)
{
    try
    {
        return FindTextFrame(name);
    }
    catch (e)
    {
        throw 'Hey, you forgot to put in the text frame named "' + name + '"!';
    }
}
You can even add to the default exception message if you want:
Code:
function TryToFindTextFrame(name)
{
    try
    {
        return FindTextFrame(name);
    }
    catch (e)
    {
        throw "Error calling external JS function: " + e;
    }
}
Or you can throw an exception for any condition you want, such as if some kind of business logic is not met, or a field value doesn't conform to some kind of pattern, or basically anything. It doesn't even have to be in an existing "catch" block; you can just throw whenever; for instance:
Code:
if (!Field("version"))
    throw "Invalid data!";
Learning how to throw and handle exceptions effectively is an important part of writing code, especially code that other people will be using. Professional programmers often talk about a variation of the "80/20 rule," which says that you'll spend 20 percent of the time writing the code for any program to handle cases where all of the inputs are in the correct and expected formats, and 80 percent of the time writing code to try to gracefully handle invalid data and other error conditions and report meaningful error messages back to the user of your code, or the end user of the program.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});

Last edited by Dan Korn; January 17th, 2018 at 02:21 PM..
Reply With Quote
  #8  
Old January 17th, 2018, 04:01 PM
ThomasLewis's Avatar
ThomasLewis ThomasLewis is offline
Senior Contributor
 
Join Date: Feb 2011
Posts: 213
Default Re: Determine if a text frame exists

Attached is my template with the external JS file. When you open OnRecordStart and click validate, it returns "null". If I paste the code from the JS file directly into OnRecordStart, it returns the error as you would expect.

I get where you are coming from trying to keep things organized. Hopefully this will make more sense with the attached sample.
Attached Files
File Type: zip errortest.zip (3.7 KB, 3 views)
Reply With Quote
  #9  
Old January 17th, 2018, 04:54 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Determine if a text frame exists

Try putting your logic in a function (called "validateTextFrames" for example) and then call it from your OnRecordStart callback.

JavaScript file:
Code:
function validateTextFrames() {
  // Your required frames.
  var frames = [
    'Your First Text Frame Name',
    'Your Second Text Frame Name'
  ];

  for (var i in frames) {
    try {
      var frameName = frames[i];
      if (FindTextFrame(frameName))
        delete frames[i];
    } catch (e) {}
  }
  if ((frames = frames.filter(String)).length)
    ReportError('Missing required text frame(s):\n"' + frames.join('"\n"') + '"');
}
OnRecordStart callback:
Code:
Load("errortest.js");
validateTextFrames();
As far as returning a value from your external JS file, you have to return the function in your FP callback:
JS file:
Code:
function testReturn() {
  return 'hello from your external js file!';
}
OnRecordStart:
Code:
Load("errortest.js");
return testReturn();
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #10  
Old January 17th, 2018, 05:27 PM
ThomasLewis's Avatar
ThomasLewis ThomasLewis is offline
Senior Contributor
 
Join Date: Feb 2011
Posts: 213
Default Re: Determine if a text frame exists

Adding that extra function call directly to OnRecordStart did the trick. It's working perfectly now. It's just a matter of updating all the templates to include that extra line. Thanks so much for all the help
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:02 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.