Skip to main content
dulajun
Inspiring
September 26, 2017
Question

I want copyTo method to be executed as background operation

  • September 26, 2017
  • 2 replies
  • 726 views

I have read these lines:

Long-running handler operations create and return Operator objects to perform time-intensive

file-system operations that block the main thread. Adobe Bridge view code or your display code

passes the object to app.enqueueOperation() to initiate the action when appropriate.

But I couldn't get it. Please could you help me in a snippet of code explaining how to execute copyTo command in the background.

This topic has been closed for replies.

2 replies

dulajun
dulajunAuthor
Inspiring
September 27, 2017

This code also doesn't work

#target bridge

var sourceFile= File.openDialog ("Select big file");

var sourceThumbnail = new Thumbnail (sourceFile);

var destinationFolder = Folder.selectDialog ("Select a folder to copy the file to");

var destinationThumbnail = new Thumbnail (destinationFolder);

destinationThumbnail.model.copyFrom([sourceThumbnail], 0, "suppressUi", "", ["New Name.mp4"]);

SuperMerlin
Inspiring
September 30, 2017

No I couldn't get copyFrom  to work either.

I have modified Bobs code to get it to work.

(It uses selected files and a new folder is created off the desktop)

Hope it helps.

if ( BridgeTalk.appName == "bridge" ) {

var BgFileCopy = {};

try {

BgFileCopy.scriptInfo = new ScriptManager.ScriptInfo();

BgFileCopy.scriptInfo.set( "name", "Background File Copy" );

BgFileCopy.scriptInfo.set( "author", "Bob Stucky" );

BgFileCopy.scriptInfo.set( "company", "Adobe Systems, Inc." );

BgFileCopy.scriptInfo.set( "copyright", "Copyright (c) 2004-2005 Adobe Systems Incorporated. All rights reserved" );

BgFileCopy.scriptInfo.set( "version", "0.1.1" );

BgFileCopy.scriptInfo.set( "date", "08-03-2005" );

BgFileCopy.scriptInfo.set( "website", "http://www.adobe.com" );

} catch ( e ) {

}

verifyFolder = function( folder ) {

    if ( !folder.exists ) folder.create();

    }

FIFOBuffer = function() {

        this.ancestor = Array();

    }

FIFOBuffer.prototype = new Array();

FIFOBuffer.prototype.pop = function() {

        return this.shift();

    }

FIFOBuffer.prototype.peek = function() {

        return this[ 0 ];

    }

FIFOBuffer.prototype.toString = function() {

        return "[Object FIFOBuffer]" + this.toString();

    }

// background file copy for scripting

BgFileCopy._bgProcessRunning = false;

BgFileCopy._bgTaskId = "";

BgFileCopy._array = new Array();

BgFileCopy._cycle = 500;

BgFileCopy._nextId = 0;

BgFileCopy._ticks = 0;

///

/// method: BgFileCopy.bgProcess()

/// brief: Execute a background copy cycle

/// This is a primitive for internal use only. This method is the master process

///

BgFileCopy._bgProcess = function( ) {

try {

BgFileCopy._ticks++;

var a = new Array();

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

var copier = BgFileCopy._array[ i ];

if ( !copier.isEmpty ) {

copier._executeBackground();

a.push( copier );

} else if ( !copier.closeOnEmpty ) {

a.push( copier );

}

}

BgFileCopy._array = a;

if ( a.length == 0 ) {

app.cancelTask( BgFileCopy._bgTaskId );

BgFileCopy._bgProcessRunning = false;

}

} catch ( e ) {

alert( e );

}

// app.document.status = "BG Process: " + BgFileCopy.ticks++;

}

BgFileCopy._addBgProcess = function( object ) {

try {

BgFileCopy._array.push( object );

if ( !BgFileCopy._bgProcessRunning ) {

BgFileCopy._bgTaskId = app.scheduleTask( "BgFileCopy._bgProcess()", BgFileCopy._cycle, true );

BgFileCopy._bgProcessRunning = true;

}

} catch ( e ) {

alert( e );

}

}

BgFileCopy.factory = function( onComplete, onTick, onError ) {

var t = new BgFileCopy.FileCopier( BgFileCopy._nextId++, onComplete, onTick, onError );

BgFileCopy._addBgProcess( t );

return t;

}

BgFileCopy.toString = function(){

return "[Object BgFileCopy]";

}

///

/// class: BgFileCopy.CopySet

/// brief: Utility class that links two files, on a copyFrom, one a copyTo file.

/// During execution, a copier will copy files as a set, "from" one file "to" the other.

///

BgFileCopy.CopySet = function( from, to ) {

this.from = from;

this.to = to;

}

///

/// toString() everyone should have one

///

BgFileCopy.CopySet.prototype.toString = function() {

return "[Object BgFileCopy.CopySet]";

}

/// class: BgFileCopy.CopyError

/// brief: wrapper object to carry error information back to the calling process

///

BgFileCopy.CopyError = function( fileCopier, err, from, to ) {

this.fileCopier = fileCopier;

this.error = err;

this.from = from;

this.to = to;

}

///

/// class: BgFileCopy.FileCopier

/// brief: the workhorse class that executes the copy

/// Do not use the constructor, use the BgFileCopy.factory method to obtain one of these

/// The FileCopier object will not start executing until you call its execute method

///

BgFileCopy.FileCopier = function( id, onComplete, onTick, onError ) {

this.id = id;

this.onComplete = onComplete || BgFileCopy.onComplete;

this.onTick = onTick || BgFileCopy.onTick;

this.onError = onError || BgFileCopy.onError;

this.executing = false;

this.acceptNewFiles = true;

this.rewindOnError = false;

this.closeOnEmpty = false;

this.isEmpty = false;

this.error = "";

this.errored = false;

this.buffer = new FIFOBuffer();

this.copied = new Array();

this.fileCount = 0;

this.current = 0;

}

BgFileCopy.FileCopier.prototype.toString = function() {

return "[Object BgFileCopy.FileCopier]";

}

///

/// method: BgFileCopy.FileCopier.add

/// brief: adds a CopySet of files to the copy buffer;

/// throws: an exception if the copier is flagged to not accept new sets of files

///

BgFileCopy.FileCopier.prototype.add = function( from, to ) {

if ( this.acceptNewFiles ) {

this.buffer.push( new BgFileCopy.CopySet( from, to ) );

this.isEmpty = false;

this.fileCount++;

return true;

}

throw "BgFileCopy.FileCopier not accepting new files to copy";

}

///

/// method: BgFileCopier.FileCopier.setRewindOnError /// brief: sets a flag rewind on an error condition

///

BgFileCopy.FileCopier.prototype.setRewindOnError = function( boo ) {

this.rewindOnError = boo;

}

///

/// method: BgFileCopier.FileCopier.rewind

/// brief: removes all copied files. Use if there is an error condition and you want all the

/// files copied befor the error to be deleted

///

BgFileCopy.FileCopier.prototype.rewind = function() {

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

this.copied[ i ].remove();

}

}

BgFileCopy.FileCopier.prototype.setCloseOnEmpty = function() {

this.closeOnEmpty = true;

this.acceptNewFiles = false;

}

///

/// method: BgFileCopier.FileCopier._copy

/// brief: The primitive method called by BgFileCopier's master process to execute the copy

///

BgFileCopy.FileCopier.prototype._executeBackground = function() {

var set = this.buffer.pop();

if ( set != undefined ) {

if ( this.onTick ) {

this.onTick( set, ++this.current, this.fileCount );

}

if ( set.from.copy( set.to ) ) {

this.copied.push( set.to );

return;

} else {

this.error = set.from.error;

this.errored = true;

if ( this.onError != undefined ) {

this.onError( new BgFileCopy.CopyError( this, this.error, set.from, set.to ) );

}

if ( this.rewindOnError ) {

this.rewind();

}

return;

}

} else {

if ( this.closeOnEmpty ) {

if ( this.onComplete ) {

this.onComplete();

}

this.isEmpty = true;

}

}

}

///

/// method: BgFileCopier.onComplete /// brief: default onComplete handler

///

BgFileCopy.onComplete = function() {

app.document.status = "Copy Complete";

}

///

/// method: BgFileCopier.onTick /// brief: default onTick handler

///

BgFileCopy.onTick = function( set, current, fileCount ) {

app.document.status = "Copying: " + decodeURI( set.from.name ) + " To: " + decodeURI( set.to.name );

}

///

/// method: BgFileCopier.onErr /// brief: default onError handler

///

BgFileCopy.onError = function( copyError ) {

app.document.status = copyError.error;

}

// test code, creates a menu in Bridge, sets the onSelect handler.

// creates a folder /my documents/copyTemp - this is the only place it copies to...

// open a page with files in Bridge, select the menu

// it will copy all of the files in that folder, or if you selected a couple

// it will copy the selected files.

BgFileCopy.execute = function() {

try {

$.level = 1;

//var files = getBridgeFiles();

var files = app.document.selections;

var copier = BgFileCopy.factory();

var toFolder = new Folder( Folder.desktop +"/copyTemp" );

if(!toFolder.exits) toFolder.create();

//toFolder = verifyFolder( toFolder, true );

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

var from = files[ i ].spec;

var to = new File( toFolder + "/" + from.name );

copier.add( from, to );

}

copier.setCloseOnEmpty();

} catch ( e ) {

alert( e + "\n" + e.line);

}

}

var backGroundCopy = new MenuElement( "command", "BgFileCopy Test", "at the end of Tools", "tools/fct" );

backGroundCopy.onSelect = function () {

    BgFileCopy.execute();

}

};

dulajun
dulajunAuthor
Inspiring
September 27, 2017

I found this discussion, but also I couldn't understand

Re: Failure to draw palette