Photoshop Bitmap Change Mode Options

Hello,
I am trying to convert a grayscale document to bitmap, but I am not sure of the syntax to use when using the Bitmap mode options.

For instance, this snippet of code does not work (Sorry, I cannot recall what the tag is for applescript code within these posts:

tell application id “com.adobe.Photoshop”
activate
set thisDoc to current document
change mode of thisDoc to bitmap with options {conversion method:middle threshold, resolution:1200}
end tell

Three back ticks before and after your code will format it as code. FAQ - MacScripter

The code below fails. It should work, but no. No error, just doesn’t convert to Bitmap. This may be one of the MANY broken places in Photoshops scripting implementation that has to be circumvented with javascript code. I’ll see if I can post a ‘do javascript’ solution tomorrow.

tell application "Adobe Photoshop 2024"
	tell current document
		set docName to name
		flatten --Document must be flattened
		change mode to grayscale --Bitmap format conversion is only available when the image is in cetain modes.
		set myOptions to {conversion method:middle threshold, resolution:1200}
		change mode docName to bitmap with options myOptions
	end tell
end tell
1 Like

Very interesting, and thank you Paul for sharing your wisdom and findings.

As for javascript - I can achieve what I am after using the following code:

tell application id "com.adobe.Photoshop"
	activate
	try
		set thisDoc to current document
		do javascript "try {
var bitsaveoptions = new BitmapConversionOptions()
bitsaveoptions.method = BitmapConversionType.HALFTHRESHOLD
bitsaveoptions.resolution = 1200
app.activeDocument.changeMode(ChangeMode.BITMAP,bitsaveoptions) 
}
catch(err)
{}"
	end try
end tell

However, it would be nice to know the AS alternative.

Thanks again,
-Jeff

The only solution is to report this bug here (although I doubt it’ll be ever fixed):

https://community.adobe.com/t5/photoshop-ecosystem/ct-p/ct-photoshop?page=1&sort=latest_replies&filter=all&lang=all&tabid=bugs

Jeffkr,

A lot of my photoshop script library is javascript wrapped up for AppleScript. There’s lots of functionality that’s exposed only in the javascript implementation.

So, while the code below works for your current conversion, if you want to use a halftone screen method then you’d have to code the javascript up for those required options.

convertMode_Bitmap(1200, "HALFTHRESHOLD")


on convertMode_Bitmap(targetResolution, conversionMethod)
	tell application "Adobe Photoshop 2024"
		tell current document
			set javaScriptCode to "try {
var bitsaveoptions = new BitmapConversionOptions()
bitsaveoptions.method = BitmapConversionType." & conversionMethod & "
bitsaveoptions.resolution = " & targetResolution & "
app.activeDocument.changeMode(ChangeMode.BITMAP,bitsaveoptions) 
}
catch(err)
{}"
			do javascript javaScriptCode
		end tell
	end tell
end convertMode_Bitmap

Thank you very much Paul. I suppose it is good that I am not using a halftone screen, since I would have no clue how to code for those additional options.

If possible, and layperson’s terms: I am curious as to what is unique with your javascript that would enable it to yield a different result than the javascript I posted above? Was my javascript code lacking something, which might have caused it to break in certain instances?

Nevertheless, I have updated my code to include your script routine.

Thank you once again,
-Jeff

Nothing at all about my code is different than your code except that it allows you to alter the resolution or method without altering the javascript code. I was just trying to illustrate how you can incorporate javascript and have AppleScript variables alter the outcome.

Sometimes this is very helpful. You can’t select a layer by its layer ID in AppleScript but you can in javascript. So I combine the two and get a functional AppleScript solution.

on document_Select_Add_ArtLayer_By_ID(layerIndex)
	tell application "Adobe Photoshop 2024"
		set js to "selectLayerByID(" & layerIndex & ",true);
function selectLayerByID(index,add){ 
add = undefined ? add = false:add 
var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID(\"Lyr \"), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID(\"null\"), ref );
       if(add) desc.putEnumerated( stringIDToTypeID( \"selectionModifier\" ), stringIDToTypeID( \"selectionModifierType\" ), stringIDToTypeID( \"addToSelection\" ) ); 
      desc.putBoolean( charIDToTypeID( \"MkVs\" ), false ); 
   try{
    executeAction(charIDToTypeID(\"slct\"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
};"
		set selLayers to do javascript js
	end tell
end document_Select_Add_ArtLayer_By_ID

Ah, I see - and I understand what you were trying to illustrate, which is greatly appreciated.

I also appreciate you sharing your snippet of code regarding selecting a specific layer by its id. - But I would like to know the practical application, since you can easily select a layer by its number using AS?
For instance:

tell application id "com.adobe.Photoshop"
	activate
	tell current document
		set layerName to name of layer 2 -- getting the name of the specific layer, in this case layer 2
		set current layer to layer 2 --selecting the 2nd layer down from top 
	end tell
end tell

-Jeff

Also, there’s the ScriptingListener plug-in (as you may know anyway):

It will record everything you do in Photoshop and write the JS code of whatever you do to a file on the Desktop. So if something isn’t directly accessible via AppleScript (or doesn’t work due to a bug), you can obtain the operation’s JS code via the plug-in, then use it in AppleScript.

As far as I remember, though, the plug-in isn’t compatible with Silicon so you’ll need to run Photoshop in the Rosetta mode in order for the plug-in to work.

Also, it can take quite an effort to decipher whatever it writes. But yeah in the end you can do things that cannot be done with AppleScript directly.

Yes, layer index is available to AppleScript.

Layer ID is far more useful because it is unique and durable, not changed by reordering, renaming etc.

I should also note that document_Select_Add_ArtLayer_By_ID(layerIndex) will ADD the layer with ID layerIndex ( apologies for the poor variable name carried over from another handler! ) to the current selection. Only javascript gets to ADD another layer to an existing layer selection. Even when using NAME to address the layer.

1 Like

Thank you Paul for the claification and explanation.

Have a great day, and thank you once again for all of your help.

-Jeff