Skip to main content
Inspiring
October 10, 2012
Question

Scripting with bridge

  • October 10, 2012
  • 1 reply
  • 1437 views

Hello, i am pretty new to this so i am still figuring out how this scripting works

In general what i want to do is generate a jpg image from an symbols.ai library file and because bridge shows this library in some way, i want to use a script against bridge.

So now i have found a script from the sdk - SnpSaveAsJPEG and i am trying to understand what is happening because i have no experience with javascript.

maybe some advice on some good literature is also good?

can someone please help me with the following questions:

at the bottom we have "new SnpSaveAsJPEG().run();" that makes the program run

why is it executing the (at the top) "function SnpSaveAsJPEG()" even if you change that functions name?

why do we set requiredContext?

is the "$.level" always needed and why do we set it?

next we have the method "SnpSaveAsJPEG.prototype.run = function()"

where does "prototype" come from?

Why is it executing this function as there is nowhere a reference to this?

thanks

This topic has been closed for replies.

1 reply

Inspiring
October 10, 2012

Can you post a pic of what Bridge is showing you…? What app versions are you using? Yes those snippets are a little strange in how they run…

abjellyAuthor
Inspiring
October 10, 2012

Hello Muppet Mark.

We are using CS 5.5

Here is the sample code, as found in the sdk(but some little test modifications by me), copyright adobe

The script is working correct, but i am trying to understand how it does what it does.

#target bridge

#targetengine "MainEngine"

function SnpSaveAsJPEG()

{

    //The context in which this snippet can run.

     //type String

    this.requiredContext = "\tExecute against Bridge.\nBridge must be running and \n"

        + "at least one thumbnail selected that is not a folder.";

    $.level = 1; // Debugging level

}

/**

Functional part of this snippet. 

Gets a File object for each selected thumbnail, and uses it to

create a BitmapData object. Uses the BitmapData object to export

to the JPEG format.

     @return True if the snippet ran as expected, false if no files are selected in Bridge

     @type Boolean

*/

SnpSaveAsJPEG.prototype.run = function()

{

    var retval = true;

    if(!this.canRun()) {

        retval = false;   

        return retval;

    }

    // Get the selected Thumbnail objects - only accept these file types

    var thumbs = app.document.getSelection("ai");

    // Go through each of the selected thumbnails

    if(thumbs.length != 0)

    {

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

        {

            // get associated File object, ignore folders

            if(thumbs.spec instanceof File)

            {

                var thumb = thumbs;

                // create a BitmapData object

                var bm = new BitmapData(thumbs.spec);

                if(bm instanceof BitmapData)

                {

                    // create the path and file name

                    var exportFilePath = thumbs.parent.spec + "/SnpSaveAsJPEG_" + thumbs.name + ".jpg";

                    // create the new file and export the data

                    bm.exportTo(new File(exportFilePath));

                }

            }

        }

    }

    else

    {

        retval = false;

    }

    return retval;

}

/**

Determines whether snippet can be run given current context.  The snippet will

fail if these preconditions are not met:

<ul>

<li> Must in Bridge

<li> At least one file must be selected in Bridge

</ul>

@return True if this snippet can run, false otherwise

@type boolean

*/

SnpSaveAsJPEG.prototype.canRun = function()

{   

    // must run in Bridge

    // must be at least one selection.

    if((BridgeTalk.appName == "bridge") && (app.document.selectionLength > 0))

    {

        return true;

    }

    // Fail if these preconditions are not met. 

    $.writeln("ERROR:: Cannot run SnpSaveAsJPEG");

    $.writeln(this.requiredContext);

    return false;

}

/**

"main program": construct an anonymous instance and run it

  as long as we are not unit-testing this snippet.

*/

//if(typeof(SnpSaveAsJPEG_unitTest)  == "undefined") {

//    new SnpSaveAsJPEG().run();

//}

new SnpSaveAsJPEG().run();

Inspiring
October 10, 2012

Bridge is capable of creating images from files… That's basically what it does when generating preview caches *.jpeg The script sample just uses this and lets you export the bitmap data to file…