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

Running the script a second time, ae crashes

Explorer ,
Oct 23, 2022 Oct 23, 2022

Copy link to clipboard

Copied

I tried writing a script to quickly replace placeholders and it worked, but the second time I ran it, AE crashed

    var myComp = app.project.activeItem;
    var ml = myComp.selectedLayers[0];
    if (ml.source.frameRate === 0) {
        var ml_Source_FrameRate = 1;
    }else{
        var ml_Source_FrameRate = ml.source.frameRate;
    };
 
    function searchLayer(string) {
        var itemNum = app.project.numItems;
        for (searchLayerIndex = 1; searchLayerIndex <= itemNum; searchLayerIndex++) {
            if (app.project.item(searchLayerIndex).name === string) {
                if (app.project.item(searchLayerIndex).file.fsName ===  ml.source.file.fsName) {
                    return searchLayerIndex;
                }
            };
        }
        return false;
    }

    searchLayer(ml.source.name);
    var ml_source_name = ml.source.name;
    app.project.item(searchLayerIndex).replaceWithPlaceholder("__placeholder__", ml.source.width, ml.source.height, ml_Source_FrameRate, ml.source.duration);
    ml.source.name = ml_source_name;
}

How can I modify it so that it can run the second time without ae crashing?
TOPICS
Scripting

Views

158

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

Explorer , Oct 24, 2022 Oct 24, 2022

Sorry I found the cause of the error in my unpublished code, now it's fixed...

Votes

Translate

Translate
LEGEND ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

You may need to add a check for the layer type. After the first replacement procedure AE may simply try to replace a placeholder with another. Also the last curly brace doesn't seem to belong anywhere, so maybe it's throwing up on that because it expects a function.

 

Mylenium

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

Sorry, I'm missing some operations.
After I ran it the first time it was successful, I tried ctrl+z to restore it, and when I ran it again, ae crashed
I have tried not to use ctrl+z, after the material is replaced with a placeholder, re-replace the placeholder with a new material, and then run on this material, ae crashes
Please ignore the last "}", this is my wrong operation

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

Sorry I found the cause of the error in my unpublished code, now it's fixed...

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 ,
Oct 24, 2022 Oct 24, 2022

Copy link to clipboard

Copied

LATEST

I'm using the wrong test to determine if the asset is an external asset

if (myComp.selectedLayers[0].source.file.fsName = null) {}
->
if (myComp.selectedLayers[0].source.file.fsName instanceof String) {}

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