mirror of
https://github.com/sbrow/ps.git
synced 2025-12-29 18:47:38 -05:00
Text can now be formatted
- Added black to the colors - Fixed setStroke to be skipped when needed
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
)
|
||||
|
||||
var Colors map[string]Color = map[string]Color{
|
||||
"Black": &RGB{0, 0, 0},
|
||||
"Gray": &RGB{128, 128, 128},
|
||||
"White": &RGB{255, 255, 255},
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ else
|
||||
path = wScript.Arguments(0)
|
||||
args = wScript.Arguments(1)
|
||||
error = appRef.DoJavaScriptFile(path, Split(args, ","))
|
||||
if Not error = "true" Then
|
||||
if Not error = "true" and Not error = "[ActionDescriptor]" and Not error = "undefined" Then
|
||||
Err.raise 1, "dojs.vbs", error
|
||||
end if
|
||||
end if
|
||||
54
scripts/fmtText.jsx
Normal file
54
scripts/fmtText.jsx
Normal file
@@ -0,0 +1,54 @@
|
||||
var start = parseInt(arguments[1]);
|
||||
var end = parseInt(arguments[2]);
|
||||
var fontName = arguments[3];
|
||||
var fontStyle = arguments[4];
|
||||
var colorArray = [0, 0, 0];
|
||||
if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){
|
||||
var activeLayer = app.activeDocument.activeLayer;
|
||||
var fontSize = activeLayer.textItem.size;
|
||||
if(activeLayer.kind == LayerKind.TEXT){
|
||||
if((activeLayer.textItem.contents != "")&&(start >= 0)&&(end <= activeLayer.textItem.contents.length)){
|
||||
var idsetd = app.charIDToTypeID( "setd" );
|
||||
var action = new ActionDescriptor();
|
||||
var idnull = app.charIDToTypeID( "null" );
|
||||
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" );
|
||||
var actionList = new ActionList();
|
||||
var textRange = new ActionDescriptor();
|
||||
var idFrom = app.charIDToTypeID( "From" );
|
||||
textRange.putInteger( idFrom, start );
|
||||
textRange.putInteger( idT, end );
|
||||
var idTxtS = app.charIDToTypeID( "TxtS" );
|
||||
var formatting = new ActionDescriptor();
|
||||
var idFntN = app.charIDToTypeID( "FntN" );
|
||||
formatting.putString( idFntN, fontName );
|
||||
var idFntS = app.charIDToTypeID( "FntS" );
|
||||
formatting.putString( idFntS, fontStyle );
|
||||
var idSz = app.charIDToTypeID( "Sz " );
|
||||
var idPnt = app.charIDToTypeID( "#Pnt" );
|
||||
formatting.putUnitDouble( idSz, idPnt, fontSize );
|
||||
var idClr = app.charIDToTypeID( "Clr " );
|
||||
var colorAction = new ActionDescriptor();
|
||||
var idRd = app.charIDToTypeID( "Rd " );
|
||||
colorAction.putDouble( idRd, colorArray[0] );
|
||||
var idGrn = app.charIDToTypeID( "Grn " );
|
||||
colorAction.putDouble( idGrn, colorArray[1]);
|
||||
var idBl = app.charIDToTypeID( "Bl " );
|
||||
colorAction.putDouble( idBl, colorArray[2] );
|
||||
var idRGBC = app.charIDToTypeID( "RGBC" );
|
||||
formatting.putObject( idClr, idRGBC, colorAction );
|
||||
textRange.putObject( idTxtS, idTxtS, formatting );
|
||||
actionList.putObject( idTxtt, textRange );
|
||||
textAction.putList( idTxtt, actionList );
|
||||
action.putObject( idT, idTxLr, textAction );
|
||||
app.executeAction( idsetd, action, DialogModes.NO );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,3 +18,67 @@ function bounds(lyr) {
|
||||
lyr.bounds[1] + '],[' + lyr.bounds[2] + ',' +
|
||||
lyr.bounds[3] + ']]').replace(/ px/g, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* The setFormatting function sets the font, font style, point size, and RGB color of specified
|
||||
* characters in a Photoshop text layer.
|
||||
*
|
||||
* @param start (int) the index of the insertion point *before* the character you want.,
|
||||
* @param end (int) the index of the insertion point following the character.
|
||||
* @param fontName is a string for the font name.
|
||||
* @param fontStyle is a string for the font style.
|
||||
* @param fontSize (Number) the point size of the text.
|
||||
* @param colorArray (Array) is the RGB color to be applied to the text.
|
||||
*/
|
||||
function setFormatting(start, end, fontName, fontStyle, fontSize, colorArray) {
|
||||
if(app.activeDocument.activeLayer.kind == LayerKind.TEXT){
|
||||
var activeLayer = app.activeDocument.activeLayer;
|
||||
fontSize = activeLayer.textItem.size;
|
||||
colorArray = [0, 0, 0];
|
||||
if(activeLayer.kind == LayerKind.TEXT){
|
||||
if((activeLayer.textItem.contents != "")&&(start >= 0)&&(end <= activeLayer.textItem.contents.length)){
|
||||
var idsetd = app.charIDToTypeID( "setd" );
|
||||
var action = new ActionDescriptor();
|
||||
var idnull = app.charIDToTypeID( "null" );
|
||||
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" );
|
||||
var actionList = new ActionList();
|
||||
var textRange = new ActionDescriptor();
|
||||
var idFrom = app.charIDToTypeID( "From" );
|
||||
textRange.putInteger( idFrom, start );
|
||||
textRange.putInteger( idT, end );
|
||||
var idTxtS = app.charIDToTypeID( "TxtS" );
|
||||
var formatting = new ActionDescriptor();
|
||||
var idFntN = app.charIDToTypeID( "FntN" );
|
||||
formatting.putString( idFntN, fontName );
|
||||
var idFntS = app.charIDToTypeID( "FntS" );
|
||||
formatting.putString( idFntS, fontStyle );
|
||||
var idSz = app.charIDToTypeID( "Sz " );
|
||||
var idPnt = app.charIDToTypeID( "#Pnt" );
|
||||
formatting.putUnitDouble( idSz, idPnt, fontSize );
|
||||
var idClr = app.charIDToTypeID( "Clr " );
|
||||
var colorAction = new ActionDescriptor();
|
||||
var idRd = app.charIDToTypeID( "Rd " );
|
||||
colorAction.putDouble( idRd, colorArray[0] );
|
||||
var idGrn = app.charIDToTypeID( "Grn " );
|
||||
colorAction.putDouble( idGrn, colorArray[1]);
|
||||
var idBl = app.charIDToTypeID( "Bl " );
|
||||
colorAction.putDouble( idBl, colorArray[2] );
|
||||
var idRGBC = app.charIDToTypeID( "RGBC" );
|
||||
formatting.putObject( idClr, idRGBC, colorAction );
|
||||
textRange.putObject( idTxtS, idTxtS, formatting );
|
||||
actionList.putObject( idTxtt, textRange );
|
||||
textAction.putList( idTxtt, actionList );
|
||||
action.putObject( idT, idTxLr, textAction );
|
||||
app.executeAction( idsetd, action, DialogModes.NO );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
#include lib.js
|
||||
var saveFile = File(arguments[0]);
|
||||
if(saveFile.exists)
|
||||
saveFile.remove();
|
||||
@@ -7,4 +8,5 @@ saveFile.open("e", "TEXT", "????");
|
||||
for (var i = 0; i < arguments.length; i++) {
|
||||
saveFile.writeln(arguments[i])
|
||||
}
|
||||
setFormatting(0,6, "Arial", "Bold");
|
||||
saveFile.close();
|
||||
43
structs.go
43
structs.go
@@ -1,7 +1,9 @@
|
||||
// TODO: Count skipped steps.
|
||||
package ps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -267,7 +269,7 @@ func (a *ArtLayer) SetActive() ([]byte, error) {
|
||||
func (a *ArtLayer) SetColor(c Color) {
|
||||
if a.Color.RGB() == c.RGB() {
|
||||
if Mode == 2 || (Mode == 0 && a.current) {
|
||||
log.Println("Skipping color: already set.")
|
||||
// log.Println("Skipping color: already set.")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -299,10 +301,15 @@ func (a *ArtLayer) SetColor(c Color) {
|
||||
}
|
||||
|
||||
func (a *ArtLayer) SetStroke(stk Stroke, fill Color) {
|
||||
if stk.Size == 0 {
|
||||
a.Stroke = &stk
|
||||
a.SetColor(fill)
|
||||
return
|
||||
}
|
||||
if stk.Size == a.Stroke.Size && stk.Color.RGB() == a.Stroke.Color.RGB() {
|
||||
if a.Color.RGB() == fill.RGB() {
|
||||
if Mode == 2 || (Mode == 0 && a.current) {
|
||||
log.Println("Skipping stroke: already set.")
|
||||
// log.Println("Skipping stroke: already set.")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -314,8 +321,6 @@ func (a *ArtLayer) SetStroke(stk Stroke, fill Color) {
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
log.Printf(" layer %s stroke was %.2fpt %v and color to %v\n", a.name, a.Stroke.Size,
|
||||
a.Stroke.Color.RGB(), a.Color.RGB())
|
||||
a.Stroke = &stk
|
||||
a.Color = fill
|
||||
stkCol := stk.Color.RGB()
|
||||
@@ -340,6 +345,17 @@ func (a *ArtLayer) Path() string {
|
||||
return fmt.Sprintf("%s%s", a.parent.Path(), a.name)
|
||||
}
|
||||
|
||||
func (a *ArtLayer) Format(start, end int, font, style string) {
|
||||
if !a.Visible() {
|
||||
return
|
||||
}
|
||||
_, err := DoJs("fmtText.jsx", fmt.Sprint(start), fmt.Sprint(end),
|
||||
font, style)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Layer returns an ArtLayer from the active document given a specified
|
||||
// path string.
|
||||
func Layer(path string) (ArtLayer, error) {
|
||||
@@ -484,6 +500,7 @@ func (l *LayerSet) ArtLayers() []*ArtLayer {
|
||||
|
||||
// ArtLayer returns the first top level ArtLayer matching
|
||||
// the given name.
|
||||
// TODO: Does funky things when passed invalid layername.
|
||||
func (l *LayerSet) ArtLayer(name string) *ArtLayer {
|
||||
for _, lyr := range l.artLayers {
|
||||
if lyr.name == name {
|
||||
@@ -491,16 +508,24 @@ func (l *LayerSet) ArtLayer(name string) *ArtLayer {
|
||||
err := lyr.Refresh()
|
||||
if err != nil {
|
||||
l.Refresh()
|
||||
err := lyr.Refresh()
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return lyr
|
||||
}
|
||||
}
|
||||
l.Refresh()
|
||||
for _, lyr := range l.artLayers {
|
||||
fmt.Println(lyr)
|
||||
}
|
||||
// l.Refresh()
|
||||
// for _, lyr := range l.artLayers {
|
||||
// fmt.Println(lyr)
|
||||
// }
|
||||
lyr := l.ArtLayer(name)
|
||||
fmt.Println(lyr)
|
||||
if lyr == nil {
|
||||
log.Panic(errors.New("Layer not found!"))
|
||||
}
|
||||
return lyr
|
||||
}
|
||||
|
||||
@@ -577,7 +602,7 @@ func (l *LayerSet) Refresh() {
|
||||
byt, err := DoJs("getLayerSet.jsx", JSLayer(l.Path()))
|
||||
err = json.Unmarshal(byt, &tmp)
|
||||
if err != nil {
|
||||
log.Println(string(byt))
|
||||
log.Println("Error in LayerSet.Refresh()", string(byt))
|
||||
log.Panic(err)
|
||||
}
|
||||
tmp.SetParent(l.Parent())
|
||||
|
||||
Reference in New Issue
Block a user