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…
@kristian perhaps we might find a channel (or whatever discourse has) for sharing tips and tricks and scripts?
Perhaps another forum on here? for custom scripts?
I added a GarageSale Scripts category for all your script sharing needs.
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.
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
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";
}
}