Skip to main content
luisf19226020
Participant
December 31, 2019
Question

Vbs syntax for Ps

  • December 31, 2019
  • 1 reply
  • 448 views
  • What is the syntax to check, after executing Vbscript to Select Color Range, to determine if any pixel is selected?
This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
December 31, 2019

You will most likely need to use Action Manager Code to get that information.  There is no DOM method to get that that info.  I do not know or use Vbs for its only supported on windows.  The Action Manager  JavaScript code I have seen for that looks like this.

 

 

////// check for selection //////
function hasSelection(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var docDesc = executeActionGet(ref);
var hasSelection = docDesc.hasKey(stringIDToTypeID("selection"));
return hasSelection;
};

alert("hasSelection = " + hasSelection() );

 

 

You may be able to use DOM code to get the current selection bounds, if VBS has a way of catching errors.   If there is no selection I would think trying to get the selection bounds would fail. So if it fails there is no selection. If it works you know there is a selection and know its bounds.

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits;
// Set Photoshop to use pixels 
app.preferences.rulerUnits = Units.PIXELS;
try { 
	var SB = activeDocument.selection.bounds;
	var SWidth = (SB[2].value) - (SB[0].value);
	var SHeight = (SB[3].value) - (SB[1].value);	
	alert("'" + activeDocument.activeLayer.name + "' Selection Bounds\nTop Left "
 	+  SB[0].value + "X," +  SB[1].value + "Y   Bottom Right "  +  SB[2].value + "X," +  SB[3].value
	+ "Y\nWidth " +  SWidth + "px   Height " + SHeight +"px" 
	+ "\nSlection center relative to canvas " + (SB[0].value + SWidth/2) + "X," +  (SB[1].value + SHeight/2) +"Y" );
}
catch(e) {alert("There is no active selection");}
// Return the app preferences
app.preferences.rulerUnits = startRulerUnits;

 

 

 

 

JJMack
luisf19226020
Participant
January 1, 2020

Thank you for the reply. I found a work around in Vbs. I created a function that will copy a selection in the clipboard. Copy will generate an error if there is no selection found. So the function will return True if there is a selection and return False if there is no selection. The clipboard is cleared in the function before exiting. It does work. If anybody has a better solution in Vbs I would appreciate it very much.

 

Luis

 

Here is the code:

Function HasSelection() As Boolean
       Rem ========Test for selection ===========
       On Error GoTo HasSelectionErr
       HasSelection = True
       Dim objApp
       Set objApp = CreateObject("Photoshop.Application")
       Rem Use dialog mode 3 for show no dialogs
       Dim dialogMode
       dialogMode = 3
       Dim id3622
       id3622 = objApp.charIDToTypeID("copy")
       Call objApp.ExecuteAction(id3622, , dialogMode)
       Clipboard.Clear
       Exit Function
HasSelectionErr:
       HasSelection = False
End Function

JJMack
Community Expert
Community Expert
January 1, 2020

Yes anything that requires an active selection will fail.  I chose Selection bounds for if there is an active selection knowing it bounds might be useful.

JJMack