1

I'm trying to search within a list of items (specifically, a list of files from a folder) for certain files. However, I don't know the names of these files; all I know is that they all begin with the same word (for example, "Work 1", "Work 2" and "Work 3"). I tried this script:

tell application "Finder"

    set filelist to name of every item in folder (path to desktop)

    set endlist to {}

    repeat with i from 1 to count filelist
    if {filelist's item i}'s name begins with "Work" then set endlist's end to filelist's item i

    end repeat
end tell

which at first glance would seem about right, but whenevr I run it I get error number -1728 from name. (error "Can’t get name of {\"file1\"}." number -1728 from name of {"file1.txt"}) (this "File1" happens to be the first item in the "filelist" list)

I know that applescript can recognize if an item in the list starts with "work", since this:

 if ((some item whose name begins with "Work") exists) then...

works fine, so I'm not sure what to do next...

P.S. just in case, I've tried many iterations of the "If" statemente in the script, including

  if name of {filelist's item i} begins with "Work" then...

and none have worked...

2

It a simple problem to solve.

You are first asking for the names of every item.

set filelist to name of every item in folder (path to desktop)

Which returns a list of the names. As text items.

Then you want to do your search. But in your code you are asking for the name of the text item.

Thats why you are getting the error. The text item does not have a name.

Change the line:

if {filelist's item i}'s name begins with "Work" then set endlist's end to filelist's item i

to

if filelist's item i begins with "Work" then set endlist's end to filelist's item i

UPDATE: In response to your comment.

You can create a list of the names of the items found on the desktop whose name begins with for example "Work"

Doing it this way may be better.

Here you ask for a list containing the names of any item found whose name (returned as a text item text) begins with ..

tell application "Finder"

    set filelist to name of every item in folder (path to desktop) whose name begins with "Work"
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .