• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Scripting Error

Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

I'm having difficulty figuring this one out.

This script works:

   var myFormat = app.documents[0].crossReferenceFormats.item("Paragraph Text");

   var myXref = app.documents[0].hyperlinks[0];

   myXref.source.appliedFormat = myFormat;

This script gives me an error message:

   var myFormat = app.documents[0].crossReferenceFormats.item("Paragraph Text");

   var myXref = app.documents[0].crossReferenceFormats[0];

   myXref.source.appliedFormat = myFormat;

Now, according to the Document model, crossReferenceFormats is a property and [] is a method for that property. So, it seems to me that as hyperlinks is referring to the index of a specific hyperlink, then crossReferenceFormats should be referring to the index of a specific CrossReferenceFormat.

What am i missing?

TOPICS
Scripting

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Aug 22, 2013 Aug 22, 2013

Your words are good, your code doesn't come close to doing what your words say. What is your obsession with crossReferenceFormats[0]?

A document has cross reference formats so you can use them to format your cross references.

You need something like this:

myLinks = app.activeDocument.hyperlinks;

myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

for (j = myLinks.length - 1; j >= 0; j--) {

          if (myLinks.source instanceof CrossReferenceSource) {

                    if (my

...

Votes

Translate

Translate
Contributor ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Have you looked at the description of a crossReferenceFormat object in the ESTK Object Model Viewer?

It is very easy to confuse yourself in a script if you give a variable a bad name -- calling your selected crossReferenceFormat "myXref" is something that will be very confusing later in the script. A Hyperlink has a source property, a crossReferenceFormat doesn't, so attempting to set the appliedFormat of a non-existent source is going to cause an error.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

I have, but not being a very sophisticated scripter, it's a bit difficult to follow.

It does appear to me that crossReferenceFormats[0] should refer to the cross reference format with an index of 0, so I am having a little difficulty understanding why hyperlinks[0] is a source property, but crossReferenceFormats[0] is not.

I also did see something about crossreference id but I am not sure how to get it.

My problem is connected to the other discussion we were having a couple of days ago.

I need to figure out a scrip that will let me change the assignment of cross reference formats. The script you gave me will work only if I know the index number for every cross reference. I have discovered, however, then when you add a cross reference, InDesign re-alphabetizes the crossreferences so that their index numbers change.

What I am seeking is a way to search a document, find each instance of a cross reference format of "Paragraph Text & Page Number" and change it to just "Paragraph Text".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Dave,

As a second follow up. You gave me this bit of code:

var myFormats = app.activeDocument.crossReferenceFormats;

$.writeln(myFormats.everyItem().name.join("\n"));

;

and it works.

You also gave me this bit of code:

var myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

var myXref = app.activeDocument.hyperlinks[0];

myXref.source.appliedFormat = myFormat;

and it also works.

Just for the fun of it, I changed your first bit of code to:

var myFormats = app.activeDocument.hyperlinks;

$.writeln(myFormats.everyItem().name.join("\n"));

;

and it works.

So for the life of me, I cannot understand why the following does't work:

var myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

var myXref = app.activeDocument.crossReferenceFormats[0];

myXref.source.appliedFormat = myFormat;

since according to the DOM, crossReferenceFormats and hyperlinks are both properties of the Document, and both have identical [ ] (index) methods.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Ice cream trucks have names and so do ice creams, but ice creams do not have front-left tires.

The reason your script fails is because a crossReferenceFormat does not have a source property and so you cannot set the appliedFormat of that source because it doesn't exist. You're trying to set the pressure of the front-left tire of an ice cream.

That's all I have time for this morning -- have to do my own work.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Dave,

I sincerely appreciate all the time you've given me even though I haven't quite caught on as to why cross reference formats don't have a source property. I also tried this script, and it worked:

var myFormats = app.activeDocument.crossReferenceFormats;

$.writeln(myFormats.everyItem().index.join("\n"));

;

so I'm afraid I'm at sea about a cross reference not having a source property when it is, at its heart, as best as I can understand, a hyperlink. But, I'll keep digging.

Thanks again for all your time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

cross references do have sources.

cross reference formats don't.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Ah, I finally see the cause of your confusion:

All cross references are hyperlinks.

But: all hyperlinks are not cross references.

What you need to do is look at each hyperlink, determine if it is a cross reference and if it is of the kind you're interested in, and if so, change it in the way you want.

That's an outline of how your script should operate.

Dave

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Dave,

Great minds think alike. I finally realized that I was coming at this from the wrong angle.

What I need to do is determine the number of hyperlinks in the document, and, as you said, examine each one and and determine its cross-reference format and then change it accordingly.

I'm hacking at the script now. There are some things I'm not sure of yet.

I know I need to start by determining the number of hyperlinks in the document and then do a for loop to sort through each one.

Then, I think I need to determine the index of its cross reference format and re-assign it accordingly.

Here's the snippet I've come up with so far:

var NumHyper = app.activeDocument.hyperlinks.count();

for (i=0; i<NumHyper; i++){

    if (app.activeDocument.crossReferenceFormats[0].isValid){

        var Link = ???

        Link.source.appliedFormat = "Paragraph Text"}

    }

What I'm not sure of is how to get the hyperlink contents using the hyperlink index as the starting point. Am I on the right track, finally?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

Your words are good, your code doesn't come close to doing what your words say. What is your obsession with crossReferenceFormats[0]?

A document has cross reference formats so you can use them to format your cross references.

You need something like this:

myLinks = app.activeDocument.hyperlinks;

myFormat = app.activeDocument.crossReferenceFormats.item("Paragraph Text");

for (j = myLinks.length - 1; j >= 0; j--) {

          if (myLinks.source instanceof CrossReferenceSource) {

                    if (myLinks.source.appliedFormat.name == "Full Paragraph & Page Number") {

                              myLinks.source.appliedFormat = myFormat;

                    }

          }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 22, 2013 Aug 22, 2013

Copy link to clipboard

Copied

LATEST

Don't have obsession with formats[0]; just thought I'd do this using indexes. You way works, I'll just have to add some "else" coding to handle the various possibilities. I was just focusing on one to get the basic approach right. Now, I'll need to extend it to cover my various cases.

I cannot tell you how much I appreciate your help!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines