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

JS Add text to a document

New Here ,
Sep 19, 2008 Sep 19, 2008
Hi, New to Javascript and spent all morning trying to append a text box with a bit of text using Javascript.

I have an open AI doc with a text box with some text in and the cursor is placed after the last letter.

I would like to run a javascript and will add some text.

Can any one point me in the right direction.

Cheers Chris
TOPICS
Scripting
3.0K
Translate
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
Adobe
Community Expert ,
Sep 19, 2008 Sep 19, 2008
Try this:

activeDocument.textFrames[0].contents+="some text";
Translate
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
New Here ,
Sep 19, 2008 Sep 19, 2008
It worked, thank you so much. It has also helped me understand how to work with text in Javascript

Cheers Chris
Translate
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
Community Expert ,
Sep 19, 2008 Sep 19, 2008
You're welcome. I suggest you download and have a look at these files (assuming you're using CS3), if you haven't already:

Adobe_Intro_to_Scripting.pdf (General intro to scripting)
javascript_tools_guide_cs3.pdf (General CS3 JS info)
IllustratorCS3_Scripting_Guide.pdf (Genral AI CS3 scripting info)
IllustratorCS3_JavaScript_Reference.pdf (Detailed AI CS3 JS reference, this is probably the most important of them all)
Translate
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
Participant ,
Sep 19, 2008 Sep 19, 2008
Bear in mind, though, that when you use the += operator on text, you'll lose any tables or anchored frames within the text.

Better to use:

app.documents[0].textFrames[0].parentStory.insertionPoints[-1].contents = "some text";

Dave
Translate
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
Community Expert ,
Sep 19, 2008 Sep 19, 2008
[Edit: Sorry. Double post.]
Translate
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
New Here ,
Sep 19, 2008 Sep 19, 2008
Cheers, Thanks very much

My other message is about Converting Spot colours to process, can you help with that?

Cheers Chris
Translate
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
New Here ,
Sep 21, 2008 Sep 21, 2008
Hi Try67,

The code below places "Some text" at the end of text in the tex frame, how would force it to put "some text" where the cursor was or when you have some text highlighted?

activeDocument.textFrames[0].contents+="some text";

PS I have downloaded all JavaScript PDF and have started at the begining, once again thankyou
Translate
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
Community Expert ,
Sep 21, 2008 Sep 21, 2008
Look up "InsertionPoint".
Translate
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
New Here ,
Sep 21, 2008 Sep 21, 2008
I found small amount on "InsertionPoint" How would I use the ip part in below code to insert text where cursor was?
Cheers Chris

activeDocument.textFrames[0].contents+="some text";

// Creates a new document, adds text then inserts a
// space between each character using insertion points
var docRef = documents.add();
var textRef = docRef.textFrames.add();
textRef.contents = "Wouldnt you rather be scripting?";
textRef.top = 400;
textRef.left = 100;
textRef.textRange.characterAttributes.size = 20;
redraw();
// Add a space between each character using insertion points.
var ip;
for(var i=0; i<(textRef.insertionPoints.length); i+=2) {
ip = textRef.insertionPoints;
ip.characters.add(" ");
}
Translate
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
Participant ,
Sep 21, 2008 Sep 21, 2008
The selection in InDesign is represented by an array. When text is selected, the length of the array is just one object, the selected text object. So, your script needs to know:

1. Is there an open document? -- if not, there can't be a selection.
2. Is there a selection?
3. Is it text?

The way I code this (and there are many ways you could do it) is this:

if (app.documents.length > 0 &&
app.selection.length > 0 &&
app.selection[0].hasOwnProperty("baseline")) {
// text is selected;
// to replace the selection with some text, use:
app.selection[0].contents = "Hello ducky";

// or, to insert text at the first insertion point of the selection:

app.selection[0].insertionPoints[0].contents = "Hello ducky";

// or, to insert text at the last insertion point of the selection:

app.selection[0].insertionPoints[-1].contents = "Hello ducky";

}

Does that help?

Dave
Translate
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
New Here ,
Sep 22, 2008 Sep 22, 2008
Cheers Dave, You started by Talking about InDesign! Is code for Indesign or Illustrator. I'm using Illustrator CS3.

Just tried but got an error message 21 line one

Cheers Chris
Translate
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
Community Expert ,
Sep 22, 2008 Sep 22, 2008
chris,

If you have some text selected you can use the selection object.
It will return a textRange object and you can access the selected text by using its contents property.

So create a script with the following line:

alert(selection.contents);

Now create a new document and a new text box. Type in some text and select part of it. Run the script described above.
Translate
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
Community Expert ,
Sep 22, 2008 Sep 22, 2008
PS - Another way of doing this is by using the textSelection property of the textFrame.

Select some text and run this:

alert("All the text: " + activeDocument.textFrames[0].contents);
alert("The selected text: " + activeDocument.textFrames[0].textSelection[0].contents);
Translate
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
Participant ,
Sep 22, 2008 Sep 22, 2008
LATEST
Oh heck, I didn't realize what forum I'm in. I have no idea if the code I posted works with Illustrator. Sorry.

Dave
Translate
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