• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Script automatically remove extra spaces at the beginning and end of a selected text layer.

Explorer ,
Mar 13, 2024 Mar 13, 2024

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?

screenshot_1709622560.pngscreenshot_1709704968.png

TOPICS
Actions and scripting

Views

682

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 14, 2024 Mar 14, 2024

@c.pfaffenbichler wrote:

Thanks, I had misunderstood. 

 

Does the Script @Stephen 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, "");

 

2024-03-14_21-20-34.png

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 13, 2024 Mar 13, 2024

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){
            }
        }
    }

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

quote

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 14, 2024 Mar 14, 2024

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

Thanks, I had misunderstood. 

 

Does the Script @Stephen Marsh posted work for you? 

Is the Type Layers’ content always homogenous, so without variability in font, size, tracking, …? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied


@c.pfaffenbichler wrote:

Thanks, I had misunderstood. 

 

Does the Script @Stephen 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, "");

 

2024-03-14_21-20-34.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

I thought Edit > Find and Replace Text might work out, but that doesn’t seem to process RegExp. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2024 Mar 14, 2024

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

The regex beginning ^ or end $ of string metacharacter ignores the linefeed \n which simplifies things.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

Yeah I couldn't get the built-in Find to recognize either regex or returns.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

Yes, I have just added a feature request if anybody cares to vote:

 

https://community.adobe.com/t5/photoshop-ecosystem-ideas/add-a-regular-expression-option-to-find-and...

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2024 Mar 18, 2024

Copy link to clipboard

Copied

@Kathyyyy â€“ How did my latest single-line script go?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 25, 2024 Mar 25, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 26, 2024 Mar 26, 2024

Copy link to clipboard

Copied

LATEST
quote

It's working for each single text layer

Is that not sufficient? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 19, 2024 Mar 19, 2024

Copy link to clipboard

Copied

@Kathyyyy , please remember to mark the correct answer as such. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines