Copy link to clipboard
Copied
Hello everyone!
I'm currently working on typesetting dialogue for a comic. When I type, I often have extra spaces at the beginning and end of each line when I press enter, because we have to manually line break. For each extra space, we are penalized and lose points. Is there a script that can automatically remove the extra spaces at the beginning and end of the selected text layer?
@c.pfaffenbichler wrote:
Thanks, I had misunderstood.
Does the Script @Stephen_A_Marsh posted work for you?
No it doesn't, as I didn't have the document to test on...
This simple one-liner should adjust the active text layer. The code could be extended to process all text layers in the active document or all text layers in all open documents:
app.activeDocument.activeLayer.textItem.contents = app.activeDocument.activeLayer.textItem.contents.replace(/^\s+|\s+$/gm, "");
Copy link to clipboard
Copied
You should be able to use a regular expression with a global flag and multi-line flag, replacing the white space with nothing:
^\s+|\s+$
Perhaps like this (script code courtesy of @Lumigraphics)
/*
Utility PS Scripts created by David M. Converse ©2018-21
This script is a demo of replacing text
Last modified 6/2/2021
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target photoshop
layerUpdate();
function layerUpdate(){
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
var originalRulerUnits = preferences.rulerUnits;
try{
var docRef = activeDocument;
preferences.rulerUnits = Units.POINTS;
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
if(LayerRef.kind == LayerKind.TEXT){
var TextRef = LayerRef.textItem;
var layerText = TextRef.contents;
var newText = layerText.replace(/^\s+|\s+$/gm, "");
TextRef.contents= newText;
}
}
preferences.rulerUnits = originalRulerUnits;
app.displayDialogs = originalDialogMode;
}
catch(e){
}
}
}
Copy link to clipboard
Copied
When I type, I often have extra spaces at the beginning and end of each line when I press enter, because we have to manually line break.
Why would that cause spaces to manifest?
Is that a peculiarity of the used font/language?
Could you provide a file as is and the corresponding one as it should be?
Copy link to clipboard
Copied
The extra spaces are not caused by the language itself.
In any language, there should be a space between each word. However, when typesetting dialogue, we are required to remove spaces at the beginning and end of sentences when we break lines. This means that after breaking a line, we need to remove the inherent space between words to prevent them from shifting the dialogue slightly and making it uneven.
I have attached a sample file below. The dialogues highlighted in red are those with extra spaces at the beginning and end of sentences, some with up to two extra spaces. I am looking for a way to quickly remove these spaces.
Copy link to clipboard
Copied
Thanks, I had misunderstood.
Does the Script @Stephen_A_Marsh posted work for you?
Is the Type Layers’ content always homogenous, so without variability in font, size, tracking, …?
Copy link to clipboard
Copied
@c.pfaffenbichler wrote:
Thanks, I had misunderstood.
Does the Script @Stephen_A_Marsh posted work for you?
No it doesn't, as I didn't have the document to test on...
This simple one-liner should adjust the active text layer. The code could be extended to process all text layers in the active document or all text layers in all open documents:
app.activeDocument.activeLayer.textItem.contents = app.activeDocument.activeLayer.textItem.contents.replace(/^\s+|\s+$/gm, "");
Copy link to clipboard
Copied
Neat.
The one thing that could be a problem would be inhomogenous type properties, like if single words within a Type Layer were set to bold or similar.
The sample file does not seem to contain any such occurrence, so this may be overcomplicating the issue needlessly.
Copy link to clipboard
Copied
Certainly, it's always something to consider, which is why you asked the question earlier... it's a drawback with DOM code, like layer styles, working with fonts via AM code is way more complex.
Copy link to clipboard
Copied
I thought Edit > Find and Replace Text might work out, but that doesn’t seem to process RegExp.
Copy link to clipboard
Copied
You need to be able to search for return and space characters, which Find/Replace doesn't seem to do. Manually, you can search for space and use the Change button on each leading/trailing space you find.
The other way to handle this is copy/past into a text editor like BBEdit (Mac) or Notepad++(Win), both of which have free versions, and use find/replace there. Then paste back in and style that.
Copy link to clipboard
Copied
The regex beginning ^ or end $ of string metacharacter ignores the linefeed \n which simplifies things.
Copy link to clipboard
Copied
Yeah I couldn't get the built-in Find to recognize either regex or returns.
Copy link to clipboard
Copied
Yes, I have just added a feature request if anybody cares to vote:
Copy link to clipboard
Copied
@Kathyyyy – How did my latest single-line script go?
Copy link to clipboard
Copied
It's working for each single text layer, thank you! Sorry, I've forgot password, just sign in and check it out today
Copy link to clipboard
Copied
It's working for each single text layer
Is that not sufficient?
Copy link to clipboard
Copied
@Kathyyyy , please remember to mark the correct answer as such.