Rewriting the title

Kristan sorry let me reword that…

If pattern attribute is there but is blank… it leaves a space… is there anyway around that? I use a template lsiting to duplicate so have all the options…

Or so i jsut need to remove it if not being used?

Can you please give this updated script a try?:

function run() {

	var thisBrand = "";
	var thisSize = "";
	var thisColour = "";
	var thisPattern = "";
	var thisFeatures = "";
	var thisType = "";

	for (const listing of selectedListings) {

		if (!listing.attributes["Brand"] || listing.attributes["Brand"]=="") {
			consoleLog("NO BRAND");
		} else {
			thisBrand = listing.attributes["Brand"] + " ";
		}

		if (!listing.attributes["Size"] || listing.attributes["Size"]=="") {
			consoleLog("NO SIZE");
		} else {
			thisSize = "Size" + " " + listing.attributes["Size"] + " ";
		}

		if (!listing.attributes["Colour"] || listing.attributes["Colour"]=="") {
			consoleLog("NO COLOUR");
		} else {
			thisColour = listing.attributes["Colour"] + " ";
		}

		if (!listing.attributes["Pattern"] || listing.attributes["Pattern"]=="") {
			consoleLog("NO PATTERN");
		} else {
			thisPattern = listing.attributes["Pattern"] + " ";
		}

		if (!listing.attributes["Features"] || listing.attributes["Features"]=="") {
			consoleLog("NO FEATURES");
		} else {
			thisFeatures = listing.attributes["Features"] + " ";
		}

		if (!listing.attributes["Type"] || listing.attributes["Type"]=="") {
			consoleLog("NO TYPE");
		} else {
			thisType = listing.attributes["Type"] + " ";
		}


		listing.title = "Text" + " " + thisBrand + thisSize + thisColour + thisPattern + thisFeatures + thisType + "Text";

	}
}

HIya yeah i tried removing the gap in the " " - but then if i does have a pattern, there isnt a gap…

No worries, ill just have a mess about.

Thanks

Did you actually give my updated script a try?
If there’s an empty value for “Pattern”, no space will be added.

Regards, Kristian

Yeah i did try… let me try again. been doing so much copy & pasting my head feels like its melting…

Should this work on when slecting more than 1 item right?

I seem to have this problem say i select 10 items… it adds a random " pattern " into the title even though the item doesnt have a pattern attribute at all…?

Im going to record a video to show…

You should be OK if you change the order you build the new title string. Instead of making the parts as

chunk = "ChunkName" + " " + ...

build the new title in a variable something like newTitle incrementally, and append each chunk to newTitle only when the value is not empty.

Then call something like listing.title = newTitle at the very end.

Please see here re: something odd going on - Loom | Free Screen & Video Recording Software ( ive added comments on the video )

it seems if i bulk run the script it takes the attribute from the others.

I See. Maybe this is a general flaw of my script. Not sure if I can figure this out.

I made more tests but even with 10 selected listings it worked fine for me.
Not sure what’s going on here.

OK, could reproduce it now: If there’s no attribute (e.g. Pattern), the value from the previous listings gets added. Strange :thinking:

I think, this is the fix:

function run() {

	for (const listing of selectedListings) {
		
		var thisBrand = "";
		var thisSize = "";
		var thisColour = "";
		var thisPattern = "";
		var thisFeatures = "";
		var thisType = "";

		if (!listing.attributes["Brand"] || listing.attributes["Brand"]=="") {
			consoleLog("NO BRAND");
		} else {
			thisBrand = listing.attributes["Brand"] + " ";
		}

		if (!listing.attributes["Size"] || listing.attributes["Size"]=="") {
			consoleLog("NO SIZE");
		} else {
			thisSize = "Size" + " " + listing.attributes["Size"] + " ";
		}

		if (!listing.attributes["Colour"] || listing.attributes["Colour"]=="") {
			consoleLog("NO COLOUR");
		} else {
			thisColour = listing.attributes["Colour"] + " ";
		}

		if (!listing.attributes["Pattern"] || listing.attributes["Pattern"]=="") {
			consoleLog("NO PATTERN");
		} else {
			thisPattern = listing.attributes["Pattern"] + " ";
		}

		if (!listing.attributes["Features"] || listing.attributes["Features"]=="") {
			consoleLog("NO FEATURES");
		} else {
			thisFeatures = listing.attributes["Features"] + " ";
		}

		if (!listing.attributes["Type"] || listing.attributes["Type"]=="") {
			consoleLog("NO TYPE");
		} else {
			thisType = listing.attributes["Type"] + " ";
		}

		listing.title = "Text" + " " + thisBrand + thisSize + thisColour + thisPattern + thisFeatures + thisType + "Text";

	}
}
1 Like

yeah thats right strange one isnt it

thats done it i think… let me run a few more to check

Nice one :slight_smile:

Yeah bingo! thats done it - thanks for that… looks like youve jsut moved the for (const listing of selectedListings)?

Is that right?

All the best

The initial variables had to be moved inside the for loop. Otherwise they won’t get “reseted”.

1 Like

ah yeah. i can see that now!!

Nice one, thanks for that

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.