Matt Matt - 1 year ago 159
AppleScript Question

Applescript how to make file list and move files with certain names

I want to make a script that takes a list of names, and moves certain files that have the similar names to the list, into another folder.

To break it down, I have a folder with a few files "appleClient2016.pdf", "fruitClient2016.pdf", "pearClient2016.pdf"

Now I have a list of "appleSept", "pearSept"
I want to move my list into another folder.

I need a script that recognizes appleSept and appleClient2016.rtf both have apple in the string and will move the pdf into another folder. Also it can only move pdf files.

I am very new to applescript but here is my attempt

set listOfFruits to {"appleSept", "pearSept"}

tell application "Finder"

if folder "moveTest1" contains listOfFruits then
move (every file of folder "moveTest1" whose name is listOfFruits) to folder "moveTest2"
end if

end tell

I know I have a few syntax errors here, but I think you get the idea of what im going for.
Any advice would be greatly appreciated.


Answer Source

Think about two things regarding listOfFruits which is a list of strings.

  1. A folder never contains a list of strings it contains a list of files or folders (contains listOfFruits).
  2. A file has never a name which is a list of strings (whose name is listOfFruits).

Basically it is not needed to check 1. with an if clause.

To check if a name is in a list of strings you have to write is in rather than is.
To understand the syntax is in: The expression a is in {a, b, c} is the same as {a, b, c} contains a

    set listOfFruits to {"appleSept", "pearSept"}

    tell application "Finder"
        move (every file of folder "moveTest1" whose name is in listOfFruits) to folder "moveTest2"
    end tell
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download