Find file

Discussion in 'Mac Programming' started by Sitckybeak, Mar 29, 2016.

  1. Sitckybeak macrumors newbie


    Jun 10, 2012
    Sydney, Australia
    I have a filemaker database from which I would like to be able to run an applescript that will find files of a particular name.
    Filemaker can pass the filename to the script and I then need a script to search for the file of that name. I would then like the file to appear in finder so I can open it if I wish.
    Can anyone help?
  2. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Sure. I guess personally I'd step out to the shell and do the find there. Let's say I want to search my documents folder for a file named "prop.png", then the command in Terminal would be:

    find ~/Documents -type f -name "prop.png"
    That will search all folders recursively, which can be a pain in AppleScript. If you want to limit how many folders deep you want to search then you can do:

    find ~/Documents -type f -name "prop.png" -maxdepth 5
    ...adjust the '5' according to how many folders deep you want to search.

    To roll that into an AppleScript you could use something like:

    --I've hard coded the file name.
    set theFiles to every paragraph of (do shell script "find ~/Documents -type f -name \"prop.png\" -maxdepth 1")
    --display an error if no files were found
    if (count of theFiles) is 0 then
        display alert "Error" message "No files found"
    end if
    --I'm assuming that only the one file will be found. If there are several, we just deal with the first match
    set theFile to (POSIX file (item 1 of theFiles))
    tell application "Finder"
        reveal theFile
    end tell
    I've not tested this thoroughly and it's not production ready code - it's just intended to be good enough to get you going.

    Hope that's of some help!

Share This Page