• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can someone lend me a handler?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can someone lend me a handler?


  • Subject: Re: Can someone lend me a handler?
  • From: "Marc K. Myers" <email@hidden>
  • Date: Fri, 9 Jan 2004 04:03:44 -0500

Subject: Can someone lend me a handler?
Date: Thu, 8 Jan 2004 12:06:06 -0600
From: "Wallace, William" <email@hidden>
To: <email@hidden>

I've seen recursion discussed on this list before, so I did some poking around in the archives but I can't seem to find exactly what I need with respect to this topic. I've seen lots of stuff about burrowing through nested folders and creating a list of files that meet some criteria, but what I need is a plain vanilla Applescript handler that you pass a folder and a string to, the handler searches that folder recursively for a file name that matches the passed string. As soon as it finds a match, it breaks and returns a reference to the file whose name matches the string. I know it seems like I should be able to sort this out based on the code I've seen, but I can't seem to get my hands around this one. I'm also interested in squeezing the fastest possible performance out of this handler.

To do it under OS 9 you'd want something like this:

global theAnswer

on run
set theAnswer to ""
set theFldr to choose folder
set theText to text returned of (display dialog "File name?" default answer "")
findFile((theFldr as text), theText)
if theAnswer is "" then
display dialog "File not found"
else
display dialog theAnswer
end if
end run

on findFile(theFldr, theText)
tell application "Finder"
set itemList to items of folder theFldr
repeat with anItem in itemList
set theRtrn to ""
set theItem to the (contents of anItem) as text
if character -1 of theItem is ":" then
my findFile(theItem, theText)
if theAnswer is not "" then exit repeat
else
if name of item theItem is theText then
set theAnswer to theItem
exit repeat
end if
end if
end repeat
if theAnswer is not "" then return theRtrn
end tell
end findFile

To do it under OS X is much simpler:

set theFldr to text 1 thru -2 of POSIX path of (choose folder)
set theText to text returned of (display dialog "File Name?" default answer "")
set theResult to POSIX file (do shell script "find \"" & theFldr & "\" -type f -iname \"" & theText & "\"")

Marc [01/09/04 4:02:56 AM]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: UI Scripting, Safari and text field
  • Next by Date: Re: Can someone lend me a handler?
  • Previous by thread: Re: Can someone lend me a handler?
  • Next by thread: Lend a handler addendum
  • Index(es):
    • Date
    • Thread