Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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...

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Thanks!!! I'll be honest, at first I though exactly this wast the problem (each list item not having a name), but I got confused when I checked online and found that "if some item whose name begins with..." line. This is just what I needed –  Carlos Mar 2 '13 at 1:30
    
Update the answer to show you you where not far of on that either.. –  markhunte Mar 2 '13 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.