Jump to content

Saving a rule


Recommended Posts

I have a customer we regular do postal mailings for. They provide two columns in their excel spreadsheet for zip code: one for the standard zip, and one for the plus four zip. Not every field has the plus four data, so I wrote a rule to add a hyphen and plus four data only when it exists:

 

if ((Field("Location ZIP Code") != "") && (Field("Location ZIP+4") != ""))

return Field("Location ZIP Code")+"-"+Field("Location ZIP+4");

else if ((Field("Location ZIP Code") != "") && Field("Location ZIP+4") == "")

return Field("Location ZIP Code");

 

I have to find the last job and create and cut and paste this rule each time. Is there a way to save it to my rules list and make it *permanent* so it's always there?

Link to comment
Share on other sites

A couple things: First, that rule can be a one-liner:

return [Field("Location ZIP Code"),Field("Location ZIP+4")].filter(String).join("-");

As far as saving rules, you can't do that exactly, but you can store user-defined functions, or rule templates, or import rules from other jobs. These threads have more info about those options:

http://forums.pti.com/showthread.php?t=550

http://forums.pti.com/showthread.php?t=1512

http://forums.pti.com/showthread.php?t=3125

Link to comment
Share on other sites

I read through the posts, and I think creating a function sounds like what I need to do, because the fields are not always names the same way (but will contain the same kind of variable data). I got this far, but I don't know if it's correct or how to proceed, because every time I hit "OK" I get a syntax error and can't continue. see screenshot.

 

[edit]Also, to be clear, I won't always have a +4, and in that instance, I do not want the hyphen, which is why I had the longer script you see above. Not certain how this one-liner handles that, as I have not yet used it.

zipplus4function.jpg.0d980ae614f90c099577aad64d022177.jpg

Link to comment
Share on other sites

I read through the posts, and I think creating a function sounds like what I need to do, because the fields are not always names the same way (but will contain the same kind of variable data).

Okay, so you want to parameterize the field names then. That does sound like something for which you would want a function. Whether that function should also be "saved" for use only by other rules in the same job, or for use by other jobs as well, is what I'm not sure you're asking about.

I got this far, but I don't know if it's correct or how to proceed, because every time I hit "OK" I get a syntax error and can't continue. see screenshot.

You're confusing the syntax for adding a function to the JavaScript Globals with the syntax for adding a function to the RuleTemplates.English.js file. If you want to add a function to the JavaScript Globals, so that it can be called from any rule in the job, then you can just use syntax like this:

function ZIPPlusFour(BaseFieldName, PlusFourFieldName)
{
   return [Field(BaseFieldName), Field(PlusFourFieldName)].filter(String).join("-");
}

Then, your rule can call the function like so:

return ZIPPlusFour("Location ZIP Code", "Location ZIP+4");

If you wanted to add the function to your RuleTemplates.English.js file, you could do that, but it requires a different syntax, where you need to do things like escape double-quotes in the .js file. But I don't think you need to use the RuleTemplates.English.js approach, so I won't go into further detail about it.

[edit]Also, to be clear, I won't always have a +4, and in that instance, I do not want the hyphen, which is why I had the longer script you see above. Not certain how this one-liner handles that, as I have not yet used it.

The example from my first post would not handle that. But in a function, it's easy to handle the second parameter as optional, like so:

function ZIPPlusFour(BaseFieldName, PlusFourFieldName)
{
   if (!PlusFourFieldName)
       return Field(BaseFieldName);
   //else
   return [Field(BaseFieldName), Field(PlusFourFieldName)].filter(String).join("-");
}

Then, your rule can call the function like so:

return ZIPPlusFour("Location ZIP Code", "Location ZIP+4");

Or like so, without the +4:

return ZIPPlusFour("Some other ZIP Code");

Although that last example seems so trivial that I'm not sure it's worth having a special mode of the function to handle it. I would just go with the first version of the function above, and if you don't have a +4 field, simply return the other field, like so:

return Field("Some other ZIP Code");

Also, are you really using FusionPro 6.2? That's a pretty old version. If so, then you will also need to add the Array.filter function to your JavaScript Globals. You can find it here:

http://forums.pti.com/showthread.php?t=2570

Link to comment
Share on other sites

Okay - First off, thanks for the help so far. But I've read your reply 4 times, and I am now 100% lost. I just simply, for lack of a better explanation, "don't get it."

 

The rule I described, (to add the +4 to a zip and add a hyphen (when the field exists)) will be a rule I will use regularly, so whenever I launch Fusion Pro, I do not have to recreate it each time. The customer always sends us an .xls file and names the fields differently every time, but the fields contain the same data regardless of the different name. I'd like to be able to go to the Create Rules --> New --> and then select my unique rule from the list, simply change the field names to the proper field names and have it add the hyphen when the +4 exists and add the +4 field, and have it only show the zip (without the hyphen) when there is no data in the +4 field.

 

I can think of other regular rules I use (like for IMBs) that I would also use in this fashion once I figure this out and understand how it works.

 

Here's my confusion:

You're confusing the syntax for adding a function to the JavaScript Globals with the syntax for adding a function to the RuleTemplates.English.js file. If you want to add a function to the JavaScript Globals, so that it can be called from any rule in the job, then you can just use syntax like this:

So I click on JavaScript Globals and past this code in... then what? I see no way to save it, and don't know how to recall it for use.

 

Then, your rule can call the function like so:
So I am creating the rule for every job, every time? I thought we were permanently adding it... Maybe I DO need to add it to the RuleTemplates.English.js file as I will be using my unique rule several times a month.

 

Also, are you really using FusionPro 6.2? That's a pretty old version. If so, then you will also need to add the Array.filter function to your JavaScript Globals. You can find it here:

http://forums.pti.com/showthread.php?t=2570

Again, like above, I click on JavaScript Globals and past this code in... then what?
Link to comment
Share on other sites

Okay, I will try to explain this better.

 

Anything you add to the JavaScript Globals is global to that job. So if you add a function in there, you can call it from any other rule in the job. There's nothing to "do" after entering code in there, other than clicking OK, and then using the functions or variables defined there in other rules.

 

However, the JavaScript Globals do not carry over to other jobs. If you want to make something available to all jobs, there are several ways you can do it, from least to most recommended:

 

  1. Add code to the RuleTemplate.English.js file.
  2. Create a custom .js file in the Plug-ins folder.
  3. Create a custom XML template rule in the Plug-ins/TemplateXML folder.

The syntax for all of these is different. Also, if you're using any kind of workflow where you upload the job to another machine for composition, such as FusionPro VDP Producer (FP Direct), Producer API (FP Server), or a web-to-print system such as MarcomCentral, then the first two options (the RuleTemplate file or a custom .js file) won't work, as the code in there is available only on that one machine where the file exists. Plus, the first two options only allow you to define functions, not actual rules.

 

So, your best bet is probably to create a custom XML template rule. The rule will be available in every job you create on your machine, in the New Rule dialog, and once you add it to a job, it will be carried over with that job, even on a different machine.

 

I've attached an XML Template rule file that I think will do what you want. I had to give it a ".txt" extension to upload it here, so you'll want to download it, then rename it to remove the ".txt." extension (so that it has the extension ".xml"), and place it in the Plug-Ins/TemplateXML folder on your machine, which for your OS and FusionPro version, should be "/Library/Application Support/Printable/FusionPro/Plug-Ins/TemplateXML".

 

Once the file is there, you should see "Zip Plus Four Rule" in the list in the New Rule dialog. If you want to make changes to the rule, you can simply edit the file, although there are some "gotchas" in terms of escaping things for XML which are not always obvious.

ZipPlusFour.xml.txt

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...