Rewriting the title

Java ≠ JavaScript they are different languages

No the string substitution is a special syntax for the GarageSale Template language (a whole other language from AppleScript and JavaScript). You can only use the square brackets in GS templates.

Here is a little JavaScript that prints the brand, if there is one, to the console, or “NO BRAND” if there isn’t one set. The template language you are using is doing a lot of extra BS to check whether there is a Brand attribute at all. Don’t do that.

function run(){
  var thisBrand = "";
	for (const listing of selectedListings) {
	  thisBrand = listing.attributes["Brand"];
		if (!thisBrand) {
		  consoleLog("NO BRAND");
		} else {
		  consoleLog(thisBrand);		  
		}
	}
}

(This is not very well-factored JS; I just threw it together to show you how to get the attributes in JS.)

Hiya!! oh great thanks for this… Ill see if i can take that, and work out how to make the title… brand size colour etc.

Thanks!

Ah okay okay… sorry i used to use a different software and the [[ could be used everywhere…

Yeah that’s Shopify/Liquid template syntax.

@pkjack contacted me through the support with some request regarding adding certain attributes to the title so I tried to create one based on @Vaguery script. It might be helpful for others, too, that’s why I am posting it here. Feel free to modify and improve it to fit your needs.

function run() {

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

	for (const listing of selectedListings) {

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

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

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

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

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

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


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

	}
}

More about GarageSale’s javascript feature: Running Scripts

Regards, Kristian

Hiya!! you beat me to it… was just about to paste it here… going to run some tests & will update…

As a note for anyone looking at this… with the help from @Vaguery i managed to get to this stage this stage -

function run() {
consoleLog("selectedListings: " + selectedListings.length);
for (const listing of selectedListings) {
// consoleLog(listing.title);
listing.title = “Ladies” + " " + listing.attributes[“Brand”] + " " + “Size” + " " + listing.attributes[“Size”] + " " + listing.attributes[“Colour”] + " " + listing.attributes[“Neckline”] + " " + listing.attributes[“Sleeve Length”] + " " + listing.attributes[“Type”] + " " + “Top”
// consoleLog(listing.title);
}
}

The problem i had was if a attribute was missing it would read as undefined - this is where @Vaguery 2nd solution come into play with removing anything that was blank… but i could quite figure out how to implement into each other…

then @kristian put it all together…

:slight_smile:

@kristian perhaps we might find a channel (or whatever discourse has) for sharing tips and tricks and scripts?

1 Like

Perhaps another forum on here? for custom scripts?

I added a GarageSale Scripts category for all your script sharing needs.

1 Like

Hey Kristian. So. works great! the only thing is if lets say " pattern " doesnt have one… it adds an extra space into the title…

i know if i remove the space in - " " - that would do it… but then if there is a pattern, it needs a space…

Is there anyway around that?
All the best

If there’s no “Pattern” attribute no extra space should be added at all.
Can you please double-check?

I couldn’t reproduce this problem.

Regards, Kristian

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.