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

Can there be scripts that perform multiple operations at once?

Enthusiast ,
Sep 13, 2020 Sep 13, 2020

Copy link to clipboard

Copied

Are you like this? After importing the text, first apply the "body" paragraph style (for example:mybody) created by yourself, and then apply the "none" character style. Sometimes we need to perform "clear style first".
We always repeat operations, can we use a script to automatically complete it at once.

Similarly, for tables,
We first need to set the minimum row height,
Then set the cell style to none,
Then apply the table style you created (for example: mytable)
Finally, you also need to perform "Clear Style Priority".

Maybe we also need to set the first row as the header
This should also be done automatically once with a script.

 

Hope the master can help solve this problem~

thank you very much!

TOPICS
Activation billing and install , Bug , How to , Scripting

Views

552

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 , Oct 05, 2020 Oct 05, 2020

Sorry for my late response.

I understand from your need.

You can try this code:

var myDoc = app.documents[0];
/// This is for paragraphs
if(myDoc.paragraphStyles.item('body').isValid){
    myDoc.stories.everyItem().paragraphs.everyItem().applyParagraphStyle(myDoc.paragraphStyles.item('body'), true);
    }
myDoc.stories.everyItem().characters.everyItem().applyCharacterStyle(myDoc.characterStyles[0]);
//================
// This is for table
var selectionIsInTable = false;
var myTable = null;
try{
...

Votes

Translate

Translate
Participant ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

you can use doscript:

app.doScript('main()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'working name')

function main(){
your code here
}

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
Enthusiast ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

I don’t understand, can you be more specific?

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 ,
Sep 14, 2020 Sep 14, 2020

Copy link to clipboard

Copied

You can try something like this:

var myDoc = app.documents[0];
/// This is for paragraphs
if(myDoc.paragraphStyles.item('body').isValid){
    myDoc.stories.everyItem().paragraphs.everyItem().applyParagraphStyle(myDoc.paragraphStyles.item('body'), true);
    }
myDoc.stories.everyItem().characters.everyItem().applyCharacterStyle(myDoc.characterStyles[0]);
//================
// This is for table
var myStories = app.documents[0].stories.everyItem();
var done = true;
if(myStories.tables.length > 0) done = false;
while(!done){
    myStories.tables.everyItem().rows.everyItem().autoGrow = true;
    myStories.tables.everyItem().cells.everyItem().autoGrow = true;
    myStories.tables.everyItem().cells.everyItem().appliedCellStyle = app.documents[0].cellStyles[0];
    if(myDoc.tableStyles.item('myTable').isValid){
        myStories.tables.everyItem().appliedTableStyle = myDoc.tableStyles.item('myTable');
        }
    myStories.tables.everyItem().headerRowCount = 1;
    if(myStories.tables.everyItem().cells.everyItem().tables.length > 0){
        myStories = myStories.tables.everyItem().cells.everyItem();
        }
    else{
        done = true;
        }
    }
//================

Best

Sunil

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
Enthusiast ,
Sep 17, 2020 Sep 17, 2020

Copy link to clipboard

Copied

Thank you very much. It works.

But it changed all the tables, did not clear the priority option, and added a blank row

--------------------

The word table for the test is here (21 day validity) 

 

Below is the operation

tab-pic.jpg

 

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
Enthusiast ,
Sep 17, 2020 Sep 17, 2020

Copy link to clipboard

Copied

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
Enthusiast ,
Sep 17, 2020 Sep 17, 2020

Copy link to clipboard

Copied

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 ,
Sep 18, 2020 Sep 18, 2020

Copy link to clipboard

Copied

Try this now:

var myDoc = app.documents[0];
/// This is for paragraphs
if(myDoc.paragraphStyles.item('body').isValid){
    myDoc.stories.everyItem().paragraphs.everyItem().applyParagraphStyle(myDoc.paragraphStyles.item('body'), true);
    }
myDoc.stories.everyItem().characters.everyItem().applyCharacterStyle(myDoc.characterStyles[0]);
//================
// This is for table
app.selection[0].rows.everyItem().autoGrow = true;
app.selection[0].cells.everyItem().autoGrow = true;
app.selection[0].rows[0].rowType = RowTypes.HEADER_ROW;
app.selection[0].cells.everyItem().appliedCellStyle = app.documents[0].cellStyles[0];
if(myDoc.tableStyles.item('myTable').isValid){
    app.selection[0].appliedTableStyle = myDoc.tableStyles.item('myTable');
    }
app.selection[0].rows.everyItem().height = "8mm";

Best

Sunil

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
Enthusiast ,
Sep 19, 2020 Sep 19, 2020

Copy link to clipboard

Copied

Hello Sunil_Yadav1~

Thank you so much!

I tried it, but it didn’t work. Help me to see it again.

 

For paragraphs:
    It works, and uses the body style, but there is no clear preference.

For table
   Prompt the following error:

   Object does not support the property or method'rows'
   cells are also not supported

tab-wrong.jpg

 

 

 

 

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
Community Expert ,
Sep 19, 2020 Sep 19, 2020

Copy link to clipboard

Copied

With the table, you need to ensure you have the entire table selected. You can add this line to check after the comments for the start of the table methods: 

if (!app.selection[0] instanceof Table) {
    alert("you must select a table!")
    exit();
}

  

 

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
Enthusiast ,
Sep 20, 2020 Sep 20, 2020

Copy link to clipboard

Copied

You don’t have to select the entire table. I have seen some scripts that only need the cursor in the table to execute on the entire table.

But I don’t know how to do it

 

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
Community Expert ,
Sep 20, 2020 Sep 20, 2020

Copy link to clipboard

Copied

The way Sunil wrote the script it requires you to select the entire table. There are other ways to get at a table with the cursor being in the table, but it involves more coding to get right. 

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 ,
Oct 05, 2020 Oct 05, 2020

Copy link to clipboard

Copied

Sorry for my late response.

I understand from your need.

You can try this code:

var myDoc = app.documents[0];
/// This is for paragraphs
if(myDoc.paragraphStyles.item('body').isValid){
    myDoc.stories.everyItem().paragraphs.everyItem().applyParagraphStyle(myDoc.paragraphStyles.item('body'), true);
    }
myDoc.stories.everyItem().characters.everyItem().applyCharacterStyle(myDoc.characterStyles[0]);
//================
// This is for table
var selectionIsInTable = false;
var myTable = null;
try{
    if(app.selection[0].parent.constructor.name == "Cell"){
        selectionIsInTable = true;
        myTable = app.selection[0].parent.parent;
        }
    else if(app.selection[0].constructor.name == "Cell"){
        selectionIsInTable = true;
        myTable = app.selection[0].parent;
        }
    else if(app.selection[0].constructor.name == "Table"){
        selectionIsInTable = true;
        myTable = app.selection[0];
        }
    }
catch(e){
    selectionIsInTable = false;
    }
if(selectionIsInTable && myTable != null && myTable != undefined){
    myTable.rows.everyItem().autoGrow = true;
    myTable.cells.everyItem().autoGrow = true;
    myTable.rows[0].rowType = RowTypes.HEADER_ROW;
    myTable.cells.everyItem().appliedCellStyle = app.documents[0].cellStyles[0];
    if(myDoc.tableStyles.item('myTable').isValid){
        myTable.appliedTableStyle = myDoc.tableStyles.item('myTable');
        }
    myTable.rows.everyItem().height = "8mm";
    }

Best

Sunil 

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
Enthusiast ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

LATEST

Sorry, there is still a problem with this script:

Yes, it works, but it's too slow for text.

 

The table part still has no clear priority (paragraph style, character style, table style and cell style should be cleared)

 

Paragraph style, character style, table style and cell style should be cleaned up.

To put it simply: clear and restore paragraph, character, table and cell styles to none

no.jpgno2.jpg

Hello

Sorry, I haven't seen it for a long time.

This script corresponds to paragraph style. It runs too slowly. And there is no clear priority.

 

Table scripts also have no preference for clearing character styles and paragraph styles

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
Enthusiast ,
Sep 24, 2020 Sep 24, 2020

Copy link to clipboard

Copied

This script takes a long time to run... a bit inefficient

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