Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Ignore a folder if it doesn't exist

This is the script i have been using as a part of a filing system.


If a folder doesn't exist I just need it to ignore the missing folder


How could I add an if exists within this script please?


Many Thanks in advance


Matt


tell application "Finder"

set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2Do"


set allTheDestinationFolders to

first folder of desktop whose name starts with "2_do_BHS", ¬

first folder of desktop whose name starts with "2_do_Bu", ¬

first folder of desktop whose name starts with "2_do_Da", ¬

first folder of desktop whose name starts with "2_do_Di", ¬

first folder of desktop whose name starts with "2_do_Do", ¬

first folder of desktop whose name starts with "2_do_In", ¬

first folder of desktop whose name starts with "2_do_Fr", ¬

first folder of desktop whose name starts with "2_do_No", ¬

first folder of desktop whose name starts with "2_do_Ma", ¬

first folder of desktop whose name starts with "2_do_Pr", ¬

first folder of desktop whose name starts with "2_do_To", ¬

first folder of desktop whose name starts with "2_do_Wa", ¬

first folder of desktop whose name starts with "2_do_Se"}


set allTheFilesToMove to

files of theHotFolder whose name starts with ¬

"BH" or name starts with ¬

"SM" or name starts with ¬

"AL", ¬

files of theHotFolder whose name starts with ¬

"BU", ¬

files of theHotFolder whose name starts with ¬

"ES", ¬

files of theHotFolder whose name starts with ¬

"DV", ¬

files of theHotFolder whose name starts with ¬

"DJ" or name starts with ¬

"RA", ¬

files of theHotFolder whose name starts with ¬

"GT" or name starts with ¬

"CC", ¬

files of theHotFolder whose name starts with ¬

"FR", ¬

files of theHotFolder whose name starts with ¬

"TM17" or name starts with ¬

"NN", ¬

files of theHotFolder whose name starts with ¬

"MA" or name starts with ¬

"MF" or name starts with ¬

"FI" or name starts with ¬

"MC" or name starts with ¬

"MH" or name starts with ¬

"MB", ¬

files of theHotFolder whose name starts with ¬

"PR", ¬

files of theHotFolder whose name starts with ¬

"TM15" or name starts with ¬

"TM11", ¬

files of theHotFolder whose name starts with ¬

"WA", ¬

files of theHotFolder whose name starts with ¬

"SE"}

iMac, Mac OS X (10.6.8)

Posted on Dec 6, 2012 2:58 AM

Reply
15 replies

Dec 6, 2012 8:07 AM in response to MattJayC

Try this:



tell application "Finder"

set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2Do"

set theStrings to

"2_do_BHS", ¬

"2_do_Bu", ¬

"2_do_Da", ¬

"2_do_Di", ¬

"2_do_Do", ¬

"2_do_In", ¬

"2_do_Fr", ¬

"2_do_No", ¬

"2_do_Ma", ¬

"2_do_Pr", ¬

"2_do_To", ¬

"2_do_Wa", ¬

"2_do_Se"}

set allTheDestinationFolders to {}

repeat with thisString in theStrings

set theFolders to (folders of desktop whose name starts with thisString)

if theFolders is not {} then

copy (first item of theFolders) to the end of allTheDestinationFolders

end if

end repeat

set allTheFilesToMove to

files of theHotFolder whose name starts with ¬

"BH" or name starts with ¬

"SM" or name starts with ¬

"AL", ¬

files of theHotFolder whose name starts with ¬

"BU", ¬

files of theHotFolder whose name starts with ¬

"ES", ¬

files of theHotFolder whose name starts with ¬

"DV", ¬

files of theHotFolder whose name starts with ¬

"DJ" or name starts with ¬

"RA", ¬

files of theHotFolder whose name starts with ¬

"GT" or name starts with ¬

"CC", ¬

files of theHotFolder whose name starts with ¬

"FR", ¬

files of theHotFolder whose name starts with ¬

"TM17" or name starts with ¬

"NN", ¬

files of theHotFolder whose name starts with ¬

"MA" or name starts with ¬

"MF" or name starts with ¬

"FI" or name starts with ¬

"MC" or name starts with ¬

"MH" or name starts with ¬

"MB", ¬

files of theHotFolder whose name starts with ¬

"PR", ¬

files of theHotFolder whose name starts with ¬

"TM15" or name starts with ¬

"TM11", ¬

files of theHotFolder whose name starts with ¬

"WA", ¬

files of theHotFolder whose name starts with ¬

"SE"}

end tell

Dec 6, 2012 9:22 AM in response to MattJayC

Hi,


Since these two variables (allTheDestinationFolders and allTheFilesToMove) must contain the same number of elements (numbers of folder and numbers of list of files), the script should not add the list of files if the folder doesn't exists, otherwise some files may go in the wrong folder when moving or saving later in the script.



Here is the script:

------------------------

set namesStartFolder to {"BHS", "Bu", "Da", "Di", "Do", "In", "Fr", "No", "Ma", "Pr", "To", "Wa", "Se"}
set namesStartFile to {{"BH", "SM", "AL"}, {"BU"}, {"ES"}, {"DV"}, {"DJ", "RA"}, {"GT", "CC"}, {"FR"}, {"TM17", "NN"}, {"MA", "MF", "FI", "MC", "MH", "MB"}, {"PR"}, {"TM15", "TM11"}, {"WA"}, {"SE"}}

set {allTheDestinationFolders, allTheFilesToMove} to {{}, {}}
tell application "Finder"
      set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2Do"
      set tc to (count namesStartFolder)
      repeat with i from 1 to tc
            set dirs to (folders of desktop whose name starts with "2_do_" & item i of namesStartFolder)
            if dirs is not {} then -- folder exists
                  set end of allTheDestinationFolders to item 1 of dirs -- the first folder 
                  set end of allTheFilesToMove to my filterFiles(theHotFolder, list i of namesStartFile)
            end if
      end repeat
      --> rest of your script ******



on filterFiles(f, L)
      set t to "files of folder \"" & f & "\" whose name starts with \"" & item 1 of L & "\""
      set tc to count L
      if tc > 1 then repeat with i from 2 to tc -- multi filter --> or name ...
            set t to t & " or name starts with \"" & item i of L & "\""
      end repeat
      run script "tell application \"Finder\" to " & t -- return filtered files
end filterFiles


Message was edited : correction --> count namesStartFolder instead of count namesStartList

Dec 6, 2012 10:23 AM in response to red_menace

Hi,


red_menace wrote:


I'm glad one of us figured out what the rest of the script was going to do - now, can you tell me where I left my keys? 😉


This is because I know the script since I helped to do it, but I could be wrong, if he uses this part of the script to create another script.

I'm fifty percent chance that it is correct, otherwise MattJayC will use one of the other scripts available.


I hope I can post a script according to what I think, if I thought wrong, I wasted my time and some say I'm a fool 😁, but that doesn't bother me.

I doesn't prevent someone to post better scripts than mine.

Dec 6, 2012 10:41 AM in response to Jacques Rioux

No problem - after your solution I was just thinking "hey, that's pretty good - why didn't I see that?".


For things like this, I like to keep things organized into lists or records, grouping the different things together so that I don't get lost (which, like my keys, happens a lot these days).


# the file matrix is a record of {destination folder name, {list of file prefixes to move}}

setfileMatrixto

{folderName:"2_do_BHS", prefixes:{"BH", "SM", "AL"}}, ¬

{folderName:"2_do_Bu", prefixes:{"BU"}}, ¬

{folderName:"2_do_Da", prefixes:{"ES"}}, ¬

{folderName:"2_do_Di", prefixes:{"DV"}}, ¬

{folderName:"2_do_Do", prefixes:{"DJ", "RA"}}, ¬

{folderName:"2_do_In", prefixes:{"GT", "CC"}}, ¬

{folderName:"2_do_Fr", prefixes:{"FR"}}, ¬

{folderName:"2_do_No", prefixes:{"TM17", "NN"}}, ¬

{folderName:"2_do_Ma", prefixes:{"MA", "MF", "FI", "MC", "MH", "MB"}}, ¬

{folderName:"2_do_Pr", prefixes:{"PR"}}, ¬

{folderName:"2_do_To", prefixes:{"TM15", "TM11"}}, ¬

{folderName:"2_do_Wa", prefixes:{"WA"}}, ¬

{folderName:"2_do_Se", prefixes:{"SE"}}}


tellapplication"Finder"


settheHotFoldertofolder"Hal 9000:Users:matthew:Pictures:HotFolder2Do"


repeatwithmatrixIteminfileMatrix-- look for folder

setdestinationFolderto (foldersofdesktopwhosenamestarts withfolderNameofmatrixItem)

ifdestinationFolderisnot {} then-- found one

setdestinationFoldertofirstitemofdestinationFolder-- only one destination

repeatwithaPrefixinprefixesofmatrixItem-- look for files

settheFilesto (filesoftheHotFolderwhosenamestarts withaPrefix)

iftheFilesisnot {} thenmovetheFilestodestinationFolder

endrepeat

endif


endrepeat

endtell

Dec 6, 2012 1:08 PM in response to twtwtw

red_menace wrote:


now, can you tell me where I left my keys? 😉


God how I wish there were an app for that...


property rightWhereILeftThem : {"my pockets", "the drawer", "the couch"}
try
    repeat
        display dialog "Are they in " & some item of rightWhereILeftThem & "?" buttons {"Cancel", "NO"} default button 2
    end repeat
on error number -128
    display alert "WHO MOVED MY KEYS !" buttons {"*#~@#!!"} default button 1
end try

Dec 7, 2012 7:04 AM in response to red_menace

Sorry back again


here is the whole script.


I run into an error where there is a file of the same name already exists, within this script it adds _a, _b etc. to the file name before moving currently it moves it too early, just not to sure how to tie these together?


Can you help, there are a few notes where I think the error is occurring.


tell application "Finder"

set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2do"

set foldericon to folder "Hal 9000:Users:matthew:Pictures:Icons:Rejected Folder"


set fileMatrix to

{folderName:"2_do_BHS", prefixes:{"BH", "SM", "AL"}}, ¬

{folderName:"2_do_Bu", prefixes:{"BU"}}, ¬

{folderName:"2_do_Da", prefixes:{"ES"}}, ¬

{folderName:"2_do_Di", prefixes:{"DV"}}, ¬

{folderName:"2_do_Do", prefixes:{"DJ", "RA"}}, ¬

{folderName:"2_do_In", prefixes:{"GT", "CC"}}, ¬

{folderName:"2_do_Fr", prefixes:{"FR"}}, ¬

{folderName:"2_do_No", prefixes:{"NN"}}, ¬

{folderName:"2_do_Ma", prefixes:{"MA", "MF", "FI", "MC", "MH", "MB"}}, ¬

{folderName:"2_do_Pr", prefixes:{"PR"}}, ¬

{folderName:"2_do_To", prefixes:{"TM15", "TM11", "TM17"}}, ¬

{folderName:"2_do_Wa", prefixes:{"WA"}}, ¬

{folderName:"2_do_Se", prefixes:{"SE"}}}


tell application "Finder"

set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2do"

repeat with matrixItem in fileMatrix-- look for folder

set destinationFolder to (folders of desktop whose name starts with folderName of matrixItem)

if destinationFolder is not {} then -- found one

set destinationFolder to first item of destinationFolder-- only one destination

repeat with aPrefix in prefixes of matrixItem -- look for files

set theFiles to (files of theHotFolder whose name starts with aPrefix)

if theFiles is not {} then movetheFilestodestinationFolder



--the line above moves them too early, and any duplicates get stuck where the relabeller would correct this below.

end repeat

end if

end repeat

end tell


--relabeller


repeat with k from 1 to (count allTheDestinationFolders)

set theFilesToMove to itemk of allTheFilesToMove

set theDestinationFolder to itemk of allTheDestinationFolders

set theFolderName to name of theDestinationFolder


set tAlpha to "abcdefghijklmnopqrstuvwxyz"

repeat with thisFile in theFilesToMove

set tName to name of thisFile

if existsitemtName in theDestinationFolder then

set tContainer to container of thisFile

set tExtension to name extension of thisFile

if tExtension is not "" then

set tName to text 1 thru -((length of tExtension) + 2) of tName

set tExtension to "." & tExtension

end if

if class of thisFile is not alias then set thisFile to thisFile as alias

if character -2 of tName = "_" and (character -1 of tName) is in tAlpha then -- check underscore

set startChar to (id of (character -1 of tName)) - 96

if startChar < 1 then set startChar to 1

set tName to text 1 thru -3 of tName

else

set startChar to 1

end if

set uScrore to "_"

repeat -- As you must rename the file before moving it, the name must be unique in his folder and in the destination folder.

if startChar > 26 then

set uScrore to uScrore & "_"

set startChar to 1

end if

set tempName to tName & uScrore & (itemstartChar of tAlpha) & tExtension

if not (existsitemtempName in theDestinationFolder) and not (existsitemtempName in tContainer) then

set name of thisFile to tempName

exit repeat

end if

set startChar to startChar + 1

end repeat

end if


movethisFiletotheDestinationFolder

end repeat

end repeat

if (counttheLastFolder) is 0 then deletetheLastFolder

end tell

Dec 7, 2012 8:59 PM in response to MattJayC

This is a good example of keeping things organized by using a handler - in this case checking for duplicate names and adding a suffix if needed. The handler can also be tweaked without things getting lost in the main script.


The change I've made is to add an error statement when trying to move a file, using a handler to add a suffix if there is an error. This is done as the script processes the folder contents, rather than building a master list.

# the file matrix is a record of {destination folder name, {list of file prefixes to move}} set fileMatrix to {¬      {folderName:"2_do_BHS", prefixes:{"BH", "SM", "AL"}}, ¬      {folderName:"2_do_Bu", prefixes:{"BU"}}, ¬      {folderName:"2_do_Da", prefixes:{"ES"}}, ¬      {folderName:"2_do_Di", prefixes:{"DV"}}, ¬      {folderName:"2_do_Do", prefixes:{"DJ", "RA"}}, ¬      {folderName:"2_do_In", prefixes:{"GT", "CC"}}, ¬      {folderName:"2_do_Fr", prefixes:{"FR"}}, ¬      {folderName:"2_do_No", prefixes:{"TM17", "NN"}}, ¬      {folderName:"2_do_Ma", prefixes:{"MA", "MF", "FI", "MC", "MH", "MB"}}, ¬      {folderName:"2_do_Pr", prefixes:{"PR"}}, ¬      {folderName:"2_do_To", prefixes:{"TM15", "TM11"}}, ¬      {folderName:"2_do_Wa", prefixes:{"WA"}}, ¬      {folderName:"2_do_Se", prefixes:{"SE"}}} tell application "Finder"      set theHotFolder to folder "Hal 9000:Users:matthew:Pictures:HotFolder2Do"            repeat with matrixItem in fileMatrix -- look for folder           set destinationFolder to (folders of desktop whose name starts with folderName of matrixItem)           if destinationFolder is not {} then -- found one                set destinationFolder to first item of destinationFolder -- only one destination                                repeat with aPrefix in prefixes of matrixItem -- look for files                     set theFiles to (files of theHotFolder whose name starts with aPrefix) as alias list                                          if theFiles is not {} then repeat with aFile in theFiles -- move files                          try                               move aFile to destinationFolder                          on error errmess number errnum -- oops (should probably check for a specific error number)                               log "Error " & errnum & " moving file: " & errmess                               set newName to my getUniqueName(aFile, destinationFolder)                               set name of aFile to "this is a unique name" -- or whatever                               move aFile to destinationFolder                               set name of aFile to newName                          end try                     end repeat                end repeat           end if      end repeat end tell to getUniqueName(someFile, someFolder)      (*      check if someFile exists in someFolder, creating a new unique file name (if needed) by adding a suffix           parameters -          someFile [mixed]: a source file path                               someFolder [mixed]: a folder to check           returns [list]:          a unique file name and extension      *)      set {counter, suffixes, divider} to {0, "abcdefghijklmnopqrstuvwxyz", "_"}      set someFile to someFile as text -- System Events will use both Finder and POSIX text            tell application "System Events" to tell disk item someFile to set {theName, theExtension} to {name, name extension}      if theExtension is not "" then set theExtension to "." & theExtension      set theName to text 1 thru -((length of theExtension) + 1) of theName -- just the name part            set newName to theName & theExtension      tell application "System Events" to tell (get name of files of folder (someFolder as text))           repeat while it contains newName                set counter to counter + 1 -- hopefully there aren't more than 26 duplicates (numbers are easier)                set newName to theName & divider & (item counter of suffixes) & theExtension           end repeat      end tell            return newName end getUniqueName

Ignore a folder if it doesn't exist

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