The every keyword

G’day all,

I want to use the every keyword but am failing to get the desired result:

This works without using the every keyword:


	set gTheScripts to {}
	repeat with i from 1 to the count of theScripts
		if item i of theScripts ends with ".scpt" then
			copy item i of theScripts to the end of gTheScripts
		end if
	end repeat

But these doesn’t, and plenty of variations also don’t. Some don’t compile some just don’t give the right answer:


set gTheScripts to every item of theScripts ends with ".scpt"
set gTheScripts to every item of theScripts whose contents ends with ".scpt"

Any help would be appreciated.

Does this work?

set gTheScripts to (choose folder)
tell application "Finder" to set theScripts to (files of gTheScripts whose name extension is "scpt") as alias list

– Rob

Unfortunatelly, it doesn’t exist such function:

set x to {"lol","back"}
every item of x whose contents = "lol"

In this sentence every item of x maps to {“lol”,“back”}, so a quick translation would be:

{"lol","back"} whose contents = "lol"

Which is absurd :cry:
In this one, the same:

set x to {"lol", "back"}
items of x = "lol"
--> {"lol","back"} = "lol"
--> false, of course

You must follow Rob’s code, which is a guy who answered while I was composing this answer :evil:
Happily, his code will fail when Finder finds a single “.scpt” file. You can use this pretty & original script instead:

set gTheScripts to (choose folder)
tell application "Finder"
	try
		set theScripts to (files of gTheScripts whose name extension is "scpt") as alias list
	on error
		set theScripts to {(files of gTheScripts whose name extension is "scpt") as alias}
	end try
end tell

:twisted: :wink:

Good catch JJ! I always forget that part until it bites me. :slight_smile:

Yes that does work but I usually try to avoid using finder. Finder seems to be quite slow and the standard additions seem to be quite a lot faster.

I also would like to know if there is a general solution to the problem of creating a new list from an old list based on a rule like equal to, or contains, or ends with etc. using the every keyword, or more accutely without having to use a repeat loop.

It seems like it it is easy if it is a list of records because you can use the whose clause on one of the properties of the record, but if it is a list of individual items then you can’t do it.

Kevin

Can you give us an example of the list that you are trying to work with in this case? It’s hard to tell what you are working with when you include only variables in your sample code.

– Rob

The following pseudo code may help indicate what I would like to know


set x to {"blahblah", "mymac", "thisText", "nomoreblah"}
set blahList to every item of list x that ends with "blah"

and then blahList becomes:
{“blahblah”, “nomoreblah”}

The faster solution, the better.
I also try to avoid the Finder, but then you must go to the shell:

set someFolder to alias "path:to:somefolder:"
paragraphs of (do shell script "find " & quoted form of POSIX path of someFolder & " -name '*.scpt' -print -maxdepth 1")

If you wish a recursive search of all nesteed directories, delete “-maxdepth 1”.

But here you have a posix-path list. If you need an alias list, you need a repeat loop anyway…

I’m pretty sure that it’s not possible.

Thanks jj and Rob,

I had a suspicion that it wouldn’t work, but I wanted to find out from the experts first. The power of some of the AppleScript commands is often astonishing, but then you realize that there are some real holes in the language.

The do shell script solution using the unix find command is an excellent solution for anything that relates to the file system.

Kevin