Skip navigation

Ignore a folder if it doesn't exist

632 Views 15 Replies Latest reply: Dec 10, 2012 4:13 AM by MattJayC RSS
1 2 Previous Next
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
Dec 6, 2012 2:58 AM

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)
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Dec 6, 2012 8:06 AM (in response to MattJayC)

    One way would be to set the folder location in a try statement, and make an alternate setting if there is an error:

     

    tell application "Finder"

      try

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

      on error errmess -- folder doesn't exist

        log errmess -- just log the error

        -- set alternate hot folder, etc

      end try 

    end tell

  • Pierre L. Level 4 Level 4 (3,860 points)
    Currently Being Moderated
    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

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    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

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Dec 6, 2012 9:26 AM (in response to Jacques Rioux)

    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?

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Dec 6, 2012 9:29 AM (in response to red_menace)

    red_menace wrote:

     

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

     

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

  • Frank Caggiano Level 7 Level 7 (22,755 points)
    Currently Being Moderated
    Dec 6, 2012 9:45 AM (in response to twtwtw)

    Just glue your keys to your iPhone and then use Find my Phone/keys

  • léonie Level 8 Level 8 (46,355 points)
    Currently Being Moderated
    Dec 6, 2012 9:47 AM (in response to Frank Caggiano)

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    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.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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}}

    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)

            if theFiles is not {} then move theFiles to destinationFolder

          end repeat

        end if

      end repeat

    end tell


  • adayzdone Level 2 Level 2 (150 points)
    Currently Being Moderated
    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
    
    Mac OS X (10.6.8)
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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
    



1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.