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

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

Explorer ,
Mar 13, 2024 Mar 13, 2024

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
825
Translate
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

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

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

 

 

Translate
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
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? 

Translate
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

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.

 

Translate
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

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, …? 

Translate
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

@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

Translate
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

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. 

Translate
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

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.

Translate
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

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

Translate
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

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.

Translate
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

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

Translate
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

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

Translate
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

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...

 

Translate
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

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

Translate
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

It's working for each single text layer, thank you! Sorry, I've forgot password, just sign in and check it out today

Translate
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
LATEST
quote

It's working for each single text layer

Is that not sufficient? 

Translate
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

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

Translate
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