trim() is not a function

Participant ,
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

118

Likes

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

Adobe Community Professional , Aug 02, 2022 Aug 02, 2022

trim() is an ECMAScript5 (ES5) feature.

Likes

Translate

Translate
Adobe Community Professional ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

Did it ever though?

Likes

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
Adobe Community Professional ,
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

Likes

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

Copy link to clipboard

Copied

trim() is an ECMAScript5 (ES5) feature.

Likes

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
Adobe Community Professional ,
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.

Likes

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
Adobe Community Professional ,
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

Likes

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

 

Likes

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
Adobe Community Professional ,
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

Likes

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

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

Likes

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
Adobe Community Professional ,
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.

Likes

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