Bug with Search and Replace when checking list with duplicate items? [SOLVED]

I have a list of names and one of them is a duplicate, like this:

Tiago
Paulo
Miguel
Tiago

When I use a For Each with a Search and Replace, I'm getting this weird behavior where the names that are duplicated, seems to be processed twice:

SCR-20240612-jsra

Once I use a unique name, with the exact same actions, each name is surrounded by the quotes and then the comma at the end as expected:

SCR-20240612-jsts

Is this a bug? If not, why is it processing it like this, if I'm processing each line individually?

1 Like

No – it's doing what you are specifying – nothing weird at work.

The scope of your search and replace is too large:

You may be stepping through the list line by line, but at each line you are again specifying yet another search and replace through the entire list.

In the Search and Replace action, change the scope from Local_list to Local_listItem

1 Like

Could you please still explain anyway why the extra quotes appear in the output from @alltiagocom's macro? I can smell that there is logic to it all but I haven't worked it out yet...

I note with interest that if the second "Tiago" is renamed "Tiago2", the output is:

"tiago",
"paulo",
"miguel",
"tiago",2

This is what I would assume is the logical approach, but this is something I encountered more than once. When you change the variable from Local_list to Local_listItem, it doesn't process. So this seems to be a bug itself.

Logically, I would assume that I need to search the variable "Local__listItem", because that's what I'm processing at the moment, but it only works with "Local__list".
As you can see, using the exact same actions with both examples, I get different results, even though it processes them while using "Local__list"

Exactly. If it wasn't working at all by using Local__list instead of Local__listItem then none of the options would work, which is not the case.
And if it was indeed processing things twice by using the Local__list, then it would add the quotes to the beginning and end of the second example. That's the first issue.

The second issue is that using Local__listItem as we would expect it to be logically, doesn't work at all, only when using the variable that contains the full list, Local__list
This is something I noticed a while ago and it made no sense to me.

If I'm checking a specific line, I should be using the variable defined at the top of the For Each, not the variable with the full list.

@ComplexPoint
As you can see here, adding the "expected/logical" variable, produces no results

image

1 Like

Can you post us a copy to experiment with ?

(It takes time to set things up from 'seeing' here, and risks extraneous variations :slight_smile: )

( experiment is the only source of knowledge – nullius in verba )


In the meanwhile, two things jump to the eye:

  1. Your entry in the Regular Expression field looks odd (would a plain string search suffice ?)
  2. Does the target (Source) make sense ?

Another approach might be to start with a blank target accumulator variable, and
for each line in the source, append a modified copy of local_listItem to the target.

Remove duplicates from a list copy.kmmacros (23.5 KB)

Thanks – the context is to reformat as an AppleScript list ?

You are aiming, more broadly, for something like:

Remove duplicates from a list copy.kmmacros (20 KB)

1 Like

Or this kind of thing ?

AppleScript list from plain text lines.kmmacros (21 KB)

1 Like

Yes my main goal is that. I appreciate you sharing it.
I was trying it with AppleScript first and for some reason I can't make it work.
I will create a thread just for that, because I want to understand what the issue is, even if I use your macro. I always like to learn a bit more of AS.

Now:
1 - Can you explain what your script is doing?
2 - We still should figure out why my macro is adding those extra quotes and comma
3 - We should also check if in fact using the Search and Replace while using Local__list is a bug or not, instead of using Local_listItem, because it seems like it.

In AppleScript I might write, for example:

use framework "Foundation"

on run
    nub({"Tiago", "Paulo", "Miguel", "Tiago"})
end run


-- nub :: [a] -> [a]
on nub(xs)
    ((current application's NSArray's arrayWithArray:xs)'s ¬
        valueForKeyPath:"@distinctUnionOfObjects.self") as list
end nub

The JavaScript for automation action uses a Set object.

(Applying new Set to a list, even one containing repetitions, defines a new Set of unique values).

The idiom [...someSet] converts that set back to a list.

1 Like

This would be my ultimate goal, yes (or maybe without the {}, because I don't know how the AppleScript is processing the line I have:

set formattedText to {"Tiago", "Paulo", "Pedro", "Tiago"}

So when using a variable, should I use it like this:

set formattedText to {myVar}

assuming that my variable doesn't include the {}

or like this

set formattedText to myVar

...assuming that my variable DOES include the {}
?

Or would that be the same thing?

Basically what I'm trying to achieve in the end, can be achieved using your JavaScript (initially I saw it as shell, my bad)
I just started with AppleScript and I'm trying to figure out why it isn't working, so I can learn something else in the process.

I think it's better to start a new thread for this AppleScript issue.
I will mention you on that thread, if you don't mind?

I think we should focus on the issue on the title, instead.

I think I figured it out, based on your suggestion of using another variable, like this:
image

So when we use the Search and Replace action and set it to "Source", the Local__listItem doesn't work in the "Search Variable" field, but Local__list works, but with that issue with the commas and quotes

image

When set to send it to another variable as you suggested, it works with Local__listItem as expected, but with Local__list outputs this:
image

which seems weird, because it's adding commas and quotes around Tiago, both versions, but nothing else...

So it's kinda weird how this is working (or not working)

Do you think that's a good choice, in 2024 ?

It's already a sunsetted language, there has been no development work on it for years now, it lacks a lot of the basics, and with every macOS release, it seems to be injured fractionally more.

I've personally had a lot of fun with AppleScript, but I wouldn't really recommend it to anyone as a starting language now.

You wouldn't prefer to learn some JS, which is useful on iOS and the web, as well ?

I can learn that as well, but if AS is still around, even if not as much as JS, wouldn't it be wise to learn a bit more? Do you think AS will be completely abandoned anytime soon?
I'm no developer and I don't plan on being. I learn as I go, mostly because of KM, so learning lots of different languages when I don't use them on a daily basis (I'm a musician, by the way), will probably be not as productive.
I started learning JS maybe 1-2 years ago and because I don't use it on a daily basis, I pretty much forgot 99.9% of it

For example, since you already know AS, it's not a big issue, because even if you learn a new language such as JS, most of the AS knowledge will still be there no matter what. Like riding a bicycle. In my case it would be a bit more complicated and I wonder if AS will be completely abandoned or just used less than other languages and if it makes sense to completely stop learning AS?

Would you confirm that JS can do everything that AS does (and more)? Or is there anything that still needs AS?

Again, if I'm learning a new language, especially because I'm still new to AS, and if JS is capable of what AS does, and more, then yes, I might as well just put AS to the side and start learning JS.

As I mentioned, I'm not a developer and won't be, so I rather focus on learning something I see myself using on a regular basis so I won't forget how things work, you know what I mean?

I appreciate any feedback you can share.

The way that Apple puts it is that JavaScript and AppleScript are peer languages in terms of access to osascript scripting interfaces.

Mac Automation Scripting Guide: About Mac Scripting

More generally, JS comes equipped with a lot of things which reduce the friction in practical day to day scripting.

When it comes to XY Problems

XY problem - Wikipedia

my personal instinct always to move on as swiftly as possible to the Y.

1 Like