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

Problem with adding TabbedPalette using the create document event

Guru ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Dear forum,

I created a TabbedPalette and now want it to add automatically on start up. My first attempt was to add it to the ‘Startup Scripts’ folder, but this, obviously, didn’t work so I reread the documentation more carefully and found out that this should be done using the create document event.

You can add a palette to an existing browser window at any time (as long as the identifier is unique), and

you can use the create document event to add your palette to new browser windows on creation.

(Bridge CS6 scripting reference, page 87)

Here’s a snippet illustrating the problem:

onCreateDocument = function(event) {

    if (event.object instanceof Document && event.type == "create") {

        $.writeln("Document is created");

        CreateTabbedPalette();

        return {handled: true};

    }

}

app.eventHandlers.push({handler: onCreateDocument});

function CreateTabbedPalette() {

    $.writeln("Entering the 'CreateTabbedPalette' function");

    $.writeln("App documents length = " + app.documents.length);

    $.writeln("Exiting the 'CreateTabbedPalette' function");

}

03-05-2017 15-37-09.png

It seems that the create document event is triggered before the document is actually created so app.documents.length = 0; as a result, later when I try to add a TabbedPalette to the current document -- app.documents[0] – an error occurs.

03-05-2017 15-59-10.png

03-05-2017 15-58-44.png

Am I doing something wrong, or adding a TabbedPalette via menu item is the only option?

Regards,
Kas

TOPICS
Scripting

Views

839

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
Guru ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Now I tried to use the loaded event instead of created: I guess this may solve my problem.

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
Guru ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

I tried to use the loaded event — no joy.

Though the palette is added on start up, at the line which adds the TabbedPalette, an error occurs:

03-05-2017 16-44-30.png

So I attempted to enclose the line into a try-catch bloc:

03-05-2017 17-02-03.png

Here's what I get in the console:

Document is created

App documents length = 1

Document: 1280

Bad argument list, line: 58

true

So the active document is valid and I can get its properties: e.g. width)

The palette has been created and is visible in Bridge. However, the error (bad argument) occurs when the script is triggered by the event (when run from ESTK, no error happens) and the variable 'c' is undefined which leads to further errors when the script references it.

03-05-2017 16-59-30.png

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
Guide ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Would something like this work for you?

try{

    var ct = new TabbedPalette( app.document, "Test tabbedPanel", "Id", "script", "left", "top");

     CreateTabbedPalette( );

     }catch(e){}

onCreateDocument = function(event) { 

    if (event.object instanceof Document && event.type == "create") { 

        $.writeln("Document is created"); 

        if(ct == undefined){

            var ct = new TabbedPalette( app.document, "Test tabbedPanel", "Id", "script", "left", "top");

        CreateTabbedPalette( ); 

        }else{

            $.sleep(500);

             var ct = new TabbedPalette( app.document, "Test tabbedPanel", "Id", "script", "left", "top");

            CreateTabbedPalette(); 

            }

        return {handled: true}; 

    } 

 

app.eventHandlers.push({handler: onCreateDocument}); 

 

function CreateTabbedPalette() { 

    $.writeln("Entering the 'CreateTabbedPalette' function"); 

    $.writeln("App documents length = " + app.documents.length); 

    $.writeln("Exiting the 'CreateTabbedPalette' function"); 

}

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
Guru ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

Thanks for your suggestion, but this doesn't work. It still produces the 'Bad argument list' error on creation of the TabbedPalette -- even after 'sleeping'.

I also tried to wait till the document becomes available like so:

onCreateDocument = function(event) { 

    if (event.object instanceof Document && event.type == "create") { 

        $.writeln("Entering the 'onCreateDocument' function");

        while (app.documents.length == 0) {

            $.writeln("Let's wait for a sec");

            $.sleep(1000);

        }

        CreateTabbedPalette(); 

        return {handled: true}; 

    } 

 

app.eventHandlers.push({handler: onCreateDocument}); 

 

function CreateTabbedPalette() { 

    $.writeln("Entering the 'CreateTabbedPalette' function"); 

    $.writeln("App documents length = " + app.documents.length); 

    $.writeln("Exiting the 'CreateTabbedPalette' function"); 

}

But this hangs Bridge.

So far I decided in favour of the 'menu element with check box' approach:

var set = null;

if (MenuElement.find("Adobe Bridge Script")) {

    $.writeln("Error:Menu element already exists!\nRestart Bridge to run this script again.");

}

else {

    CreateMenuElement();

}

function CreateMenuElement() {

    var menuElement = new MenuElement("command", "Adobe Bridge Script", "at the end of Tools",  "kasAdobeBridgeScript");

    menuElement.canBeChecked = true;

    menuElement.onSelect = function() {

        var tabbedPalette = GetPalette("AdobeBridgeScript");

        if (tabbedPalette == null) {

            CreateTabbedPalette();

            menuElement.checked = true;

        }

        else if (tabbedPalette != null && !tabbedPalette.visible) {

            tabbedPalette.visible = true;

            menuElement.checked = true;

        }

        else if (tabbedPalette != null && tabbedPalette.visible) {

            tabbedPalette.visible = false;

            menuElement.checked = false;

        }

    }

}

function CreateTabbedPalette() {

    // Create Tabbed Palette here

}

function GetPalette(id) { // Return either palette if it already exists, or null

    var palette,

    palettes = app.document.palettes,

    result = null;

   

    for (var i = 0; i < palettes.length; i++) {

        palette = palettes;

        if (palette.id == id) result = palette;

    }

    return result;

}

04-05-2017 13-13-47.png

04-05-2017 13-14-23.png

— Kas

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
Guide ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

So long as you have a solution, thats all that counts.

By the way the code worked for me using Bridge cc 2017 and Windows 10.

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
Guru ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

I also have Bridge CC 2017 and Windows 10.

When I tested your code as it is, It wrote App documents length = 0 in console and created an empty palette.

I tried to modify it to fill with controls, but this resulted in the error.

Later, if time permits, I will test using the 'create' event for creating the tabbed palette in CS3 hoping it hasn't been broken back in CS3.

So far, though not exactly what I originally wanted, I've found a working solution: the user clicks the menu and the palette appears/disappears.

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
Engaged ,
Aug 13, 2019 Aug 13, 2019

Copy link to clipboard

Copied

I have a solution for this. It may be seen as an inelegant work-around for the issue, but it works for me.

Once you have your script file working the way you want and it is located in the Bridge Startup Scripts folder, go into the Bridge Preferences and disable that script file from running at Startup. We will call this file "MainScript.jsx".  Or you can actually save it anywhere else and skip the disabling step. Just remember where it is.

Create another script file to be saved into the Bridge Startup Scripts folder. This one we will call "Script-Launcher.jsx". In Script-Launcher.jsx put the following code (which you will have to edit for your own personal needs).

Edit the first 3 variables for your own system DelaySeconds you will have to play with to get it where you want it. Too soon might launch the Palette script while the application is still loading and cause errors. Too late and the Palette won't be available right away when Bridge starts. Error on the side of too late.

launchFileName="MainScript.jsx";  //Filename for Palette Script File

scriptDir="...\\Adobe\\Bridge CC 2018\\Startup Scripts\\"; //Folder where Bridge Startup Scripts are located

DelaySeconds=15; //Delay before running Palette Script. This may have to be adjusted.

launchFile=scriptDir+launchFileName;

delay=DelaySeconds*1000;

app.scheduleTask("LaunchFile()",delay,false);

function LaunchFile() {

    if (File(launchFile).exists) {

        $.writeln("Launching "+launchFile);

        $.evalFile(launchFile);

    }

    else $.writeln("Did not launch "+launchFile);

}

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
Guru ,
Aug 13, 2019 Aug 13, 2019

Copy link to clipboard

Copied

Thank you!

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
LEGEND ,
Aug 20, 2019 Aug 20, 2019

Copy link to clipboard

Copied

LATEST

My Text Preview script does this, and is configurable. Apache-licensed open source. Look at the event handlers, I too use the loaded event.

Dropbox - Utility Script Pack.zip - Simplify your life

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