AppleScript to find file containing a variable

Discussion in 'Mac Programming' started by vloryan, Feb 25, 2016.

  1. vloryan macrumors member

    Jan 11, 2014
    Hi guys,

    I have something for Automator and Applescript. I have a part of a filename stored in my Automator variable "filename" and want to search a specific folder "X" for files containing that variable in the filename and copy all those found files to my folder "Y".

    Tried. Googled. Distressed.

    Please help! Thank you very much!
  2. macmesser macrumors 6502a

    Aug 13, 2012
    Long Island, NY USA
    A down and dirty Applescript something like the below outline ought to be enough. Hopefully others will chime in. The logic can be condensed and polished but I like to start with everything broken down as much as possible.

    1)First thing you need to do is create a list variable allFiles which will contain a reference to every file in the source folder. I would just use a choose folder to point to the source folder and tell the finder to add all files the list.

    2)Create another list variable movedFiles to contain a reference to every file that meets your criterion of having a certain string (contained in a variable named searchString) as part of the file name.

    3)Create a handler named fileMove to detect whether or not a file's name meets your criterion and if so, adds the file to the end of movedFiles. The handler will need to count the text items of an input string (in this case a filename) and compare count before and after it changes Applescript's default text item delimiters to searchString. If the count of text items is less after changing the text item delimiters to searchString, then the input fileName contains searchString and should be added to the end of list moveFiles. The handler should restore the text item delimiters to default immediately after this comparison.

    4)Use a repeat loop to cycle through list variable allFiles based on its length and pass each item to handler fileMove.

    5)Use a repeat loop to cycle through list variable movedFiles based on length and tell finder to move each one to destination folder.
  3. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK

    Forgetting about Automator and AppleScript for a moment, you can do this very quickly with a one line command in the Terminal. Lets assume I have two folders on my desktop, on called "Source" and one called "Dest" and I want to copy all files whose name contains "XXX" from Source to Dest. I'd do it like this:

    find ~/Desktop/Source -type f -name "*XXX*" -exec cp "{}" ~/Desktop/Dest \;
    Read up on the find command to learn about other options and tweak the command if necessary.

    Once you've got that nailed, it's fairly easy to take the next step and roll it into your Automator as shown in my attached image. Note that I've got "Pass input" set to "As arguments".
    Screen Shot 2016-03-01 at 15.53.40.png

    Note that $@ is a special variable meaning the argument(s) passed to the "Run Shell Script" action by the previous action. Since it technically *could* be a list, we loop through the items (even if its a list of one item!)

    Hope that's of some help.

Share This Page