Folder action query

Hi, I've been using the applescript "Camelot" suggested successfully for a while sorting thousands of nature photographs into a sorted folder structure until I hit a snag.

This script:


-- check the file name

else if name of each_file contains "Patersonia" then

-- move the file to a specific folder path

move each_file to folder "Macintosh HD:Users:stevendew:Documents:aussiecreatures:TO_UPLOAD:ZZZ_PLANTS_SORTED:Iridaceae:Nivenioideae:Patersonia"


...picks up another plant with the species name "tersonia" and places it into the wrong folder.


I'm guessing theres a way to say something like:


if name of each_file contains "Patersonia" but not "tersonia" then etc


but of course the file name still contains "tersonia"....I'm confused.


Hoping someone can help.

REgards

Steve Dew


Posted on Mar 2, 2023 2:11 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 2, 2023 11:37 AM

Hey, glad to know my script is still (mostly) working for you :)


I am a little confused by the statement:


> ...picks up another plant with the species name "tersonia" and places it into the wrong folder.


'tersonia' should not match 'Paterson', so the move shouldn't happen.


Regardless, there are bound to be other cases, and this is the big gotcha with simple path matching.


There are a couple of ways out of this.


The easiest, as you allude to, is a double-check. That's fine for simple cases, but can get unwieldy if you run into lots of exceptions.

For now, though, if you want to take that approach, you can use something like these solutions:


Assuming you have three files "Patersonia.jpg", "Mega Patersonia.jpg" and "Sonia.jpg" that need to be sorted differently


One-liner:


			else if (name of each_file contains "Patersonia") and (name of each_file does not contain "Mega Patersonia") then
				-- move the file to a specific folder path
				move each_file to folder "Macintosh HD:Users:stevendew:Documents:aussiecreatures:TO_UPLOAD:ZZZ_PLANTS_SORTED:Iridaceae:Nivenioideae:Patersonia"


In this case, the first check ("Patersonia") matches, but the second check fails, allowing you to filter them separately.


You can also cascade them using nested if statement:


else if (name of each_file contains "sonia") then
    -- this will match all three (matching is case-insensitive by default), so filter it:
    if (name of each_file contains "Mega Patersonia") then...
        -- move the file to the Mega folder
    else if (name of each_file contains "Patersonia") then
 .      -- move the file to the regular folder
    else
        -- if we get here, it doesn't match either of the other options, so
        -- move the file to the default 'sonia' folder
    end if
else if (name of each_file contains "next keyword") then...


This can get unwieldy, though, if you have many similar names.


The third option is a modification to the order in which you perform the checks.


By structuring the if/else statements to filter the most specific first, these items wouldn't then be processed by the subsequent filters. e.g. given the same list above:


else if (name of each_file contains "Mega Patersonia") then
	-- move the file to the Mega folder
else if (name of each_file contains "Patersonia") then
	-- move the file to the regular folder
else if (name of each_file contains "sonia") then
	-- move the file to the default 'sonia' folder
else if (name of each_file contains "something else") then...


In this case, when the 'Sonia' item comes through the script, it doesn't match either of the first two checks and is caught by the third.

Similarly, when 'patersonia.jpg' comes in, it skips the first check (it doesn't match 'Mega Patersonia'), but catches the second. The third check is skipped since it's already found a match, and the 'else' makes sure the 'Sonia' check only runs if the preceding checks fail.


So careful strucuturing of the if/else statements can catch many of these, but in all these examples you have to have an idea of what the possible corner cases are.


Does that help?

4 replies
Question marked as Top-ranking reply

Mar 2, 2023 11:37 AM in response to aussiecreature

Hey, glad to know my script is still (mostly) working for you :)


I am a little confused by the statement:


> ...picks up another plant with the species name "tersonia" and places it into the wrong folder.


'tersonia' should not match 'Paterson', so the move shouldn't happen.


Regardless, there are bound to be other cases, and this is the big gotcha with simple path matching.


There are a couple of ways out of this.


The easiest, as you allude to, is a double-check. That's fine for simple cases, but can get unwieldy if you run into lots of exceptions.

For now, though, if you want to take that approach, you can use something like these solutions:


Assuming you have three files "Patersonia.jpg", "Mega Patersonia.jpg" and "Sonia.jpg" that need to be sorted differently


One-liner:


			else if (name of each_file contains "Patersonia") and (name of each_file does not contain "Mega Patersonia") then
				-- move the file to a specific folder path
				move each_file to folder "Macintosh HD:Users:stevendew:Documents:aussiecreatures:TO_UPLOAD:ZZZ_PLANTS_SORTED:Iridaceae:Nivenioideae:Patersonia"


In this case, the first check ("Patersonia") matches, but the second check fails, allowing you to filter them separately.


You can also cascade them using nested if statement:


else if (name of each_file contains "sonia") then
    -- this will match all three (matching is case-insensitive by default), so filter it:
    if (name of each_file contains "Mega Patersonia") then...
        -- move the file to the Mega folder
    else if (name of each_file contains "Patersonia") then
 .      -- move the file to the regular folder
    else
        -- if we get here, it doesn't match either of the other options, so
        -- move the file to the default 'sonia' folder
    end if
else if (name of each_file contains "next keyword") then...


This can get unwieldy, though, if you have many similar names.


The third option is a modification to the order in which you perform the checks.


By structuring the if/else statements to filter the most specific first, these items wouldn't then be processed by the subsequent filters. e.g. given the same list above:


else if (name of each_file contains "Mega Patersonia") then
	-- move the file to the Mega folder
else if (name of each_file contains "Patersonia") then
	-- move the file to the regular folder
else if (name of each_file contains "sonia") then
	-- move the file to the default 'sonia' folder
else if (name of each_file contains "something else") then...


In this case, when the 'Sonia' item comes through the script, it doesn't match either of the first two checks and is caught by the third.

Similarly, when 'patersonia.jpg' comes in, it skips the first check (it doesn't match 'Mega Patersonia'), but catches the second. The third check is skipped since it's already found a match, and the 'else' makes sure the 'Sonia' check only runs if the preceding checks fail.


So careful strucuturing of the if/else statements can catch many of these, but in all these examples you have to have an idea of what the possible corner cases are.


Does that help?

Mar 3, 2023 4:56 AM in response to Camelot

So yes thanks, it worked....I took the easy option which was option 1 but will keep in mind the other suggestions if (when) things get more complicated.

I didn't describe my initial problem properly, it was the part of the script looking for "Tersonia" which was picking up "Patersonia" so i used this line:


else if (name of each_file contains "Tersonia") and (name of each_file does not contain "Patersonia") then


successfully.

Thanks again!


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Folder action query

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.