// DeselectLargest.jsx // // Copyright 2009 Dmitriy Mayorov // All Rights Reserved // /** @fileoverview Deselect the largest file among two or more selected. @class Compare sizes of selected files and deselect the largest one.

Usage

  1. In Bridge, select thumbnails
  2. Right click in Windows, Ctrl-click in Mac OS to invoke context menu
  3. Choose "Deselect Largest"
  4. You can then delete the files that remain selected, keeping only the largest.

Description

Sharper files contain more information. All other things being equal, corresponding JPEG or CR2 files are bigger. The snippet uses this observation to identify the sharpest file among selected.

Script location

@constructor Constructor */ function DeselectLargest() { // Any class variables should be commented as such /** The context in which this snippet can run. @type String */ this.requiredContext = "\tNeed to be running in context of Bridge\n"; /** The unique identifier for the new menu item command @type String */ this.menuID = "idDeselectLargest"; } /** Functional part of this snippet. @return True if the snippet ran as expected, false otherwise @type boolean */ DeselectLargest.prototype.run = function() { var retval = true; var uid = 1; // Static counter that is incremented at each run. Used in prefix to avoid filename conflicts. if (!this.canRun()) { retval = false; return retval; } var cntCommand = new MenuElement("command", "Deselect Largest", "at the end of Thumbnail", this.menuID); // What to do when the menu item is selected cntCommand.onSelect = function(m) { var prefix = 'deselect-largest-' + uid; uid++; // Get the selected Thumbnail objects, only accept these file types var thumbs = app.document.getSelection("crw, cr2, jpg"); if (thumbs.length > 1) { var largest_index = 0; var largest_size = 0; // This might take a while, do it in the background var op = new ProgressOperator("progress", thumbs); op.start = function() { op.procStat = "notStarted"; op.opStat = "incomplete"; app.operationChanged(this); op.procStat = "inProgress"; app.operationChanged(this); op.percComp = 1; for (var i = 0; i < thumbs.length; i++) { if (thumbs[i].spec instanceof File) { // Ignore folders op.percComp = 100 * i / thumbs.length; app.operationChanged(this); if (largest_size < thumbs[i].spec.length) { largest_index = i; largest_size = thumbs[i].spec.length; } } } op.percComp = 100; op.opStat = "succeeded"; op.procStat = "completed"; app.operationChanged(this); app.document.deselect(thumbs[largest_index]); return; } op.stop = function() { return; } op.getConflictInfo = function() { return "unknown"; } op.getOperationStatus = function() { return op.opStat; } op.getPercentageComplete = function() { return op.percComp; } op.getProcessingStatus = function() { return op.procStat; } op.getProgressMessage = function() { return "Identifying and deselecting the largest file"; } op.getType = function() { return "progress"; } op.getUIPolicy = function() { return true; } op.resume = function() { return false; } op.resolveConflict = function() { } app.enqueueOperation(op); } else { retval = false; } }; // When to display the menu item cntCommand.onDisplay = function() { try { cntCommand.text = "Deselect Largest"; if (app.document.selections.length > 1 && !app.document.selections[0].container) { cntCommand.enabled = true; } else { cntCommand.enabled = false; } } catch (error) { $.writeln(error); } }; return retval; } /** Determines whether snippet can be run given current context. The snippet fails if these preconditions are not met: @return True if this snippet can run, false otherwise @type Boolean */ DeselectLargest.prototype.canRun = function() { if (BridgeTalk.appName == "bridge") { return true; } $.writeln("ERROR:: Cannot run DeselectLargest"); $.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(SelectLargest_unitTest ) == "undefined") { new DeselectLargest().run(); }