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

trim() is not a function

Engaged ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

I have an old Photoshop script from circa 2012 - which I remember working, that uses the trim() function. Strings, not pixels - let's make that clear.

 

    var text = " Hello Spoons! ";
    var result = text.trim();
    alert(result)

 

However, something like the code above no longer works. text.trim is not a function.

 

I can use reg ex to trim the string, but my question is this:

Has Photoshop scripting changed/updated it's  version of ECMA Script (ver 3.x???) to no longer include trim?

 

 

 

TOPICS
Actions and scripting

Views

1.4K

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

Guide , Aug 02, 2022 Aug 02, 2022

trim() is an ECMAScript5 (ES5) feature.

Votes

Translate

Translate
Adobe
LEGEND ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

The Object Model Viewer doesn't show trim() as a supported function.

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
Engaged ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Did it ever though?

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 ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

trim is a method of document, so trying to apply it to some text seems pointless. 

Screenshot 2022-08-02 at 16.09.55.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
Guide ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

trim() is an ECMAScript5 (ES5) feature.

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 ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

https://www.w3schools.com/jsref/jsref_trim_string.asp

Properly its string.trim() but its apparently not implemented in ExtendScript.

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 ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Hi @Ghoul Fool,

As mentioned by other members, this method is not available in ExtendScript. You can use the following link to see the methods/properties available to use in ExtendScript

https://www.indesignjs.de/extendscriptAPI/photoshop-latest/#String.html

As @jazz-y mentioned trim was introduced in ECMA5 and Extendscript uses ECMA3 so that explains this.

Now as a replacement you could write you own version of a simple trim method like the following

function trim(str){
	return str.replace(/\s*(\S*)\s*/, "$1")
}

-Manan

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
Engaged ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Thanks for the confirmation - I'd go with 

   ^\s+|\s+$

as I don't want to remove ALL spaces - just white space at the start and end.

Incidentally, as c.pfaffenbichler mentioned, isn't using "trim" as a function going to cause problems even though they are evoked differently: canvas.trim() versus trim(str)?

 

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 ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

No that shouldn't be a problem at all. We have same named functions in different objects all the time and they seem to work well.

-Manan

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
Enthusiast ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Use this polyfill. Put it somewhere near top of script before calling trim on a string.

if (!String.prototype.trim) {
    String.prototype.trim = function () {
        return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
    };
}

 

William Campbell

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 ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

LATEST

I have an include file with my script pack where I define a couple of utility functions. It can be a prototype or just an object, either works fine.

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