Last active
January 2, 2023 20:03
-
-
Save akamilkhan/57dfe803aa43ed675eb691563f112520 to your computer and use it in GitHub Desktop.
Set bold and set italic function for Photoshop JS Scripting. This can be used to control text properties in ranges, which is otherwise not supported by conventional PS JS scripting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setbold(IO, start, end, fontSize) { | |
//Sanity checking: is the active layer a text layer? | |
var activeLayer = app.activeDocument.activeLayer; | |
if (activeLayer.kind == LayerKind.TEXT) { | |
//More checking: does the text layer have content, and are start and end set to reasonable values? | |
if ((activeLayer.textItem.contents != "") && (start >= 0) && (end <= activeLayer.textItem.contents.length)) { | |
//The indentation that the Script Listerner gives you is weird, but it’s useful. | |
var idsetd = app.charIDToTypeID("setd"); | |
var action = new ActionDescriptor(); | |
var idnull = app.charIDToTypeID("null"); | |
//The action reference specifies the active text layer. | |
var reference = new ActionReference(); | |
var idTxLr = app.charIDToTypeID("TxLr"); | |
var idOrdn = app.charIDToTypeID("Ordn"); | |
var idTrgt = app.charIDToTypeID("Trgt"); | |
reference.putEnumerated(idTxLr, idOrdn, idTrgt); | |
action.putReference(idnull, reference); | |
var idT = app.charIDToTypeID("T "); | |
var textAction = new ActionDescriptor(); | |
var idTxtt = app.charIDToTypeID("Txtt"); | |
//actionList contains the sequence of formatting actions. | |
var actionList = new ActionList(); | |
//textRange sets the range of characters to format. | |
var textRange = new ActionDescriptor(); | |
var idFrom = app.charIDToTypeID("From"); | |
textRange.putInteger(idFrom, start); | |
textRange.putInteger(idT, end); | |
var idTxtS = app.charIDToTypeID("TxtS"); | |
//The "formatting" ActionDescriptor holds the formatting. It should be clear that you can | |
//add other attributes here--just get the relevant lines (usually 2) from the Script Listener | |
//output and graft them into this section. | |
var formatting = new ActionDescriptor(); | |
var idsyntheticBold = stringIDToTypeID("syntheticBold"); | |
formatting.putBoolean(idsyntheticBold, IO); | |
var idSz = app.charIDToTypeID("Sz "); | |
var idPnt = app.charIDToTypeID("#Pnt"); | |
formatting.putUnitDouble(idSz, idPnt, fontSize); | |
textRange.putObject(idTxtS, idTxtS, formatting); | |
actionList.putObject(idTxtt, textRange); | |
textAction.putList(idTxtt, actionList); | |
action.putObject(idT, idTxLr, textAction); | |
app.executeAction(idsetd, action, DialogModes.NO); | |
} | |
} | |
} | |
function setitalic(IO, start, end, fontSize) { | |
//Sanity checking: is the active layer a text layer? | |
var activeLayer = app.activeDocument.activeLayer; | |
if (activeLayer.kind == LayerKind.TEXT) { | |
//More checking: does the text layer have content, and are start and end set to reasonable values? | |
if ((activeLayer.textItem.contents != "") && (start >= 0) && (end <= activeLayer.textItem.contents.length)) { | |
//The indentation that the Script Listerner gives you is weird, but it’s useful. | |
var idsetd = app.charIDToTypeID("setd"); | |
var action = new ActionDescriptor(); | |
var idnull = app.charIDToTypeID("null"); | |
//The action reference specifies the active text layer. | |
var reference = new ActionReference(); | |
var idTxLr = app.charIDToTypeID("TxLr"); | |
var idOrdn = app.charIDToTypeID("Ordn"); | |
var idTrgt = app.charIDToTypeID("Trgt"); | |
reference.putEnumerated(idTxLr, idOrdn, idTrgt); | |
action.putReference(idnull, reference); | |
var idT = app.charIDToTypeID("T "); | |
var textAction = new ActionDescriptor(); | |
var idTxtt = app.charIDToTypeID("Txtt"); | |
//actionList contains the sequence of formatting actions. | |
var actionList = new ActionList(); | |
//textRange sets the range of characters to format. | |
var textRange = new ActionDescriptor(); | |
var idFrom = app.charIDToTypeID("From"); | |
textRange.putInteger(idFrom, start); | |
textRange.putInteger(idT, end); | |
var idTxtS = app.charIDToTypeID("TxtS"); | |
//The "formatting" ActionDescriptor holds the formatting. It should be clear that you can | |
//add other attributes here--just get the relevant lines (usually 2) from the Script Listener | |
//output and graft them into this section. | |
var formatting = new ActionDescriptor(); | |
var idsyntheticItalic = stringIDToTypeID("syntheticItalic"); | |
formatting.putBoolean(idsyntheticItalic, IO); | |
var idSz = app.charIDToTypeID("Sz "); | |
var idPnt = app.charIDToTypeID("#Pnt"); | |
formatting.putUnitDouble(idSz, idPnt, fontSize); | |
textRange.putObject(idTxtS, idTxtS, formatting); | |
actionList.putObject(idTxtt, textRange); | |
textAction.putList(idTxtt, actionList); | |
action.putObject(idT, idTxLr, textAction); | |
app.executeAction(idsetd, action, DialogModes.NO); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#target photoshop | |
function changeColor(start, end, Red,Green,Blue) { | |
//Sanity checking: is the active layer a text layer? | |
var activeLayer = app.activeDocument.activeLayer; | |
if (activeLayer.kind == LayerKind.TEXT) { | |
//More checking: does the text layer have content, and are start and end set to reasonable values? | |
if ((activeLayer.textItem.contents != "") && (start >= 0) && (end <= activeLayer.textItem.contents.length)) { | |
//The indentation that the Script Listerner gives you is weird, but it's useful. | |
var idsetd = charIDToTypeID("setd"); | |
var action = new ActionDescriptor(); | |
var idnull = charIDToTypeID("null"); | |
//The action reference specifies the active text layer. | |
var reference = new ActionReference(); | |
var idTxLr = charIDToTypeID("TxLr"); | |
var idOrdn = charIDToTypeID("Ordn"); | |
var idTrgt = charIDToTypeID("Trgt"); | |
reference.putEnumerated(idTxLr, idOrdn, idTrgt); | |
action.putReference(idnull, reference); | |
var idT = charIDToTypeID("T "); | |
var textAction = new ActionDescriptor(); | |
var idTxtt = charIDToTypeID("Txtt"); | |
//actionList contains the sequence of formatting actions. | |
var actionList = new ActionList(); | |
//textRange sets the range of characters to format. | |
var textRange = new ActionDescriptor(); | |
var idFrom = charIDToTypeID("From"); | |
textRange.putInteger(idFrom, start); | |
textRange.putInteger(idT, end); | |
var idTxtS = charIDToTypeID("TxtS"); | |
//The "formatting" ActionDescriptor holds the formatting. It should be clear that you can | |
//add other attributes here--just get the relevant lines (usually 2) from the Script Listener | |
//output and graft them into this section. | |
var formatting = new ActionDescriptor(); | |
var idClr = charIDToTypeID("Clr "); | |
var colorAction = new ActionDescriptor(); | |
var idRd = charIDToTypeID("Rd "); | |
colorAction.putDouble(idRd, Red); | |
var idGrn = charIDToTypeID("Grn "); | |
colorAction.putDouble(idGrn, Green); | |
var idBl = charIDToTypeID("Bl "); | |
colorAction.putDouble(idBl, Blue); | |
var idRGBC = charIDToTypeID("RGBC"); | |
formatting.putObject(idClr, idRGBC, colorAction); | |
var idSz = app.charIDToTypeID("Sz "); | |
var idPnt = app.charIDToTypeID("#Pnt"); | |
$.writeln (activeLayer.textItem.contents); | |
formatting.putUnitDouble(idSz, idPnt, activeLayer.textItem.size); | |
textRange.putObject(idTxtS, idTxtS, formatting); | |
actionList.putObject(idTxtt, textRange); | |
textAction.putList(idTxtt, actionList); | |
action.putObject(idT, idTxLr, textAction); | |
executeAction(idsetd, action, DialogModes.NO); | |
} | |
} | |
} | |
var textlayer = app.activeDocument.artLayers.getByName("text"); | |
app.activeDocument.activeLayer = textlayer; | |
changeColor(5,10,255,255,0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment