Run search query with each line

I’m trying to build an action that would allow me to use each line of draft to generate a new search, with each search result opening in a new tab in my browser. (Use case: Type a list of groceries, run the action, and have the browser open a series of tabs, each of which contains the Amazon/Whole Foods search results for an item listed on a line of the draft.)

I can do that very inelegantly by using a series of URL steps, each of which adds a different line to the search query. For example:
https://www.amazon.com/s?k=[[line|1]]&i=wholefoods&ref=nb_sb_noss_1
https://www.amazon.com/s?k=[[line|2]]&i=wholefoods&ref=nb_sb_noss_1
https://www.amazon.com/s?k=[[line|3]]&i=wholefoods&ref=nb_sb_noss_1

But that approach isn’t tied to the number of items on my list. if I had just two items and ran an action with three URL steps (using the URLs above), I’d get one tab for each of the two items, and then a third tab with a blank search result. On the other hand, if I had four items, I’d only get tabs for the first three – which means that, to make my action useful, I’d need it to contain a large number of URL steps, and most of the time I’d then end up with a ton of blank search pages.

I suspect that there’s a very simple way to produce a more elegant solution, but I’m very inexperienced with any sort of coding, and have been unable to figure out how to modify any of the other examples I’ve seen in the forums to accomplish this. For example, the discussion of looping an action for each line here seems like it would be helpful, but I’ve failed in my attempts to modify the functions that it calls so that it would generate an appropriate URL for each line.

Any help would be much appreciated!

See if this action does what you need.

It is a few lines of JavaScript that will ignore blank lines, but process each content line in a draft into the URL you set out.

let astrItems = draft.content.split("\n");
astrItems.forEach(function(strItem)
{
	if (strItem.length > 0) app.openURL(`https://www.amazon.com/s?k=${strItem}&i=wholefoods&ref=nb_sb_noss_1`, false);
});

Yes, this does the trick. If a line has more than one word on it, though, it seems to ignore the line – for example, if my draft has:

Corn
Broccoli
Children’s Tylenol
Candy

It will return three tabs – one each for corn, broccoli, and candy. But “Children’s Tylenol” seems to be ignored. By pulling an encoding line from the script here, I think I was able to solve that problem by revising it as follows:

let astrItems = draft.content.split("\n");
astrItems.forEach(function(strItem)
{
if (strItem.length > 0)
strItem = HTML.escape(strItem)
app.openURL(https://www.amazon.com/s?k=${strItem}&i=wholefoods&ref=nb_sb_noss_1, false);
});

If there are any characters that need to be encoded for a URL, such as spaces.

I’ve updated the action to do it like this.

let astrItems = draft.content.split("\n");
astrItems.forEach(function(strItem)
{
	if (strItem.length > 0) app.openURL(`https://www.amazon.com/s?k=${encodeURIComponent(strItem)}&i=wholefoods&ref=nb_sb_noss_1`, false);
});

It isn't quite the same as what you have opted for.
1 Like

Thanks very much, @sylumer. This was perfect, and I can see lots of ways to repurpose it for other searches as well.