list folder acting strange

Ok, what i’m trying to do is that I have a list with all the files in a folder, then I add a file to the folder and do another list folder and compare the result to get the file that has been added.

I thought that list folder only returned the filename but I suspect that’s not the case. See the code below.
First of all i just tried to do as below to compare files in the list. This didn’t work at all, and that’s the first thing that got me to suspect list folder doesn’t return the filename.

if {an_item} is not in folder_items then

So i changed it to compare as strings. And that works.

property mac_path : ((path to desktop from user domain) as string) & "Test:"
set folder_items to list folder alias mac_path without invisibles

display dialog "Add a file to the folder"
set folder_items_2 to list folder alias mac_path without invisibles
repeat with an_item in folder_items_2
	if {an_item as string} is not in (folder_items as string) then
		display dialog an_item & " has been added to the folder"
		set folder_items to folder_items & {an_item}
	end if
end repeat

So this checks if the there is a new file added in the folder and then it adds it to the list with files. But if you now do this procedure twice in a row it doesn’t work. See the code below:

property mac_path : ((path to desktop from user domain) as string) & "Test:"
set folder_items to list folder alias mac_path without invisibles

display dialog "Add a file to the folder"
set folder_items_2 to list folder alias mac_path without invisibles
repeat with an_item in folder_items_2
	if {an_item as string} is not in (folder_items as string) then
		display dialog an_item & " has been added to the folder"
		set folder_items to folder_items & {an_item}
	end if
end repeat

display dialog "Add another file to the folder"
set folder_items_2 to list folder alias mac_path without invisibles
repeat with an_item in folder_items_2
	if {an_item as string} is not in (folder_items as string) then
		display dialog an_item & " has been added to the folder"
		set folder_items to folder_items & {an_item}
	end if
end repeat

Isn’t list folder just returning a filename. Then i should be able to manually add and manipulate these lists and just be able to do a compare without the “as string” in there.

Hi,

the problem is this line

 set folder_items to folder_items & {an_item}

you don’t add the value of an_item to the list but a reference to an element of the list folder_items_2.
btw: It’s recommended to add an item to a list with the syntax set end of theList to


property mac_path : ((path to desktop as text) & "Test:")
set folder_items to list folder alias mac_path without invisibles

display dialog "Add a file to the folder"
set folder_items_2 to list folder alias mac_path without invisibles
repeat with an_item in folder_items_2
	if an_item is not in folder_items then
		display dialog an_item & " has been added to the folder"
		set end of folder_items to contents of an_item
	end if
end repeat

Ok, I see…

But that’s just because I do it from within a “repeat with” loop?

In every other case when dealing with lists it automatically works with the content and not a reference, right?

For example like this?

set end of active_uploads to first item in upload_queue

Here there’s no need to do content of?

Right, explicit dereferencing is only necessary in repeat loops with repeat with a in b syntax and in cases of comparison or assigning values.
If you want to just get the value, AppleScript dereferences the list item implicitly