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

trim() is not a function

Engaged ,
Aug 02, 2022 Aug 02, 2022

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
3.8K
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

Mentor , Aug 02, 2022 Aug 02, 2022

trim() is an ECMAScript5 (ES5) feature.

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

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

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

Did it ever though?

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

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

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

trim() is an ECMAScript5 (ES5) feature.

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

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

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

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

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

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

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

 

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

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

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

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

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