7 Replies Latest reply on Dec 21, 2016 2:51 PM by breeanne

    Applescript: Create a series of subfolders based on variable names

    breeanne

      Hi,

       

      Goal: Export container fields with images in a defined folder structure with a script. The script needs to create the folders, so: AppleScript..

       

      I have zero experience with AppleScript, but did some reading yesterday and today to try and wrap my brain around it... I reviewed a bunch of examples online and started building the AppleScript to do what I need, and got it to work to create 1 folder and 1 subfolder within the folder, but I don't understand the syntax enough to modify the file path to continue to add the next level of subfolders I need.

       

      I have a table that has photos in a container field, and 4 other fields, each specifying the name for folders (if required, so some records may only have Folder1 and Folder2 populated, etc.). Folder 4 is a subfolder of Folder 3, which is a subfolder of Folder 2, etc. I also created $Folder to be "Exported Images" so, technically there are 5 potential folders.

       

      Here is my AppleScript so far:

       

      //Exported Images Folder

      "set theOutputFolderPath to path to " & "desktop" & " folder" & ¶ &

      "set theNewFolderName to " & Quote( $Folder) & ¶ &

       

      //Folder1

      "set theOutputFolder1Path to (path to " & "desktop" & " folder as text) &" & Quote( $Folder) & ¶ &

      "set theNewFolder1Name to " & Quote( $Folder1) & ¶ &

       

      //Folder2

      // This is one place where I'm stuck... I need a way to add folder1 to the existing outputfolderpath but I can't understand the syntax...

      "set theOutputFolder2Path to (path to " & "desktop" & " folder as text) &" & Quote( $Folder) & ¶ &

      "set theNewFolder2Name to " & Quote( $Folder2) & ¶ &

       

      //Creates Export Folder

      "tell application \"Finder\" " & ¶ &

      //if doesn't exist, make folder

      "if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then make new folder at theOutputFolderPath with properties {name:theNewFolderName} " & ¶ &

       

      //Creates Folder1

      //if doesn't exist, make folder

      "if (exists folder (theOutputFolder1Path & theNewFolder1Name as string)) = false then make new folder at theOutputFolder1Path  with properties {name:theNewFolder1Name} " & ¶ &

      //Creates Folder2

      //if doesn't exist, make folder

      "if (exists folder (theOutputFolder2Path & theNewFolder2Name as string)) = false then make new folder at theOutputFolder2Path  with properties {name:theNewFolder2Name} " & ¶ &

      "end tell"

       

      I've read a little bit about shell scripts, but I don't really understand how the shell AppleScript would be able to source the variables. Would be nice to ditch all the extra quotes... they're quite confusing.

       

      My thought was to run the script as a loop:

      set Folder1 variables, etc based on first record

      run apple script to check if folder exists (*** Another thing, I'm confused why I still get an error message saying the folder already exists. Maybe I don't understand the if statement, but there is no else specified, so it shouldn't do anything if it exists, yes?)

      export

      next record

       

      Any help pointing me in the right direction will be much appreciated! Thanks so much!!

      Bree

        • 1. Re: Applescript: Create a series of subfolders based on variable names
          erolst

          breeanne wrote:

           

          My thought was to run the script as a loop:

          set Folder1 variables, etc based on first record

          I think that's exactly the right approach.

           

          Here's an outline in pseudo-ish-code (it's too late for me to fire up the AppleScript Editor, and AS is a read-only language - so look up the details and exact syntax):

           

          set currentFolderLevel to path to desktop

          -- create a folderList variable from your fields; an FM line would read like:

          "set folderList to {\"" & Substitute ( List ( field1 ; field2 ...) ; Char(10) ; "\",\"" ) & "\"}"

          repeat with folderRef in folderList

            -- the intended new folder is in folderRef

            set nextFolderLevel to currentFolderLevel & folderRef

            if not ( exists folder nextFolderLevel )

              make new folder at currentFolderLevel with properties {name: folderRef}

            end if

            set currentFolderLevel to nextFolderLevel

            -- now it exists (either way), so we continue there

          end repeat

           

          Maybe set up a testbed and use the AS editor to try this with a hardcoded list of folders. Once it works, copy the code into FileMaker and use Let( ) and List( ) to ease the formatting and the creation of the folderList variable:

           

          Let ( [

            folderList = "{\"" & Substitute ( List ( field1 ; field2 ...) ; Char(10) ; "\",\"" ) & "\"}" ;

            List (

              "tell application " & Quote ( "Finder" ) ;

              "set currentFolderLevel to path to desktop" ;

              "set folderList to " & folderList ;

              "etc."

          )

           

          btw, if you need to learn more about AS frequently - or right now - there's a volume from Apress I'd recommend: Hanaan Rosenthal & Hamish Sanderson, Learn AppleScript, 3rd Edition (at least); great introduction, tutorial and reference.

          • 2. Re: Applescript: Create a series of subfolders based on variable names
            databuzz

            FYI if you need an alternative to AppleScript you can use the (free) BaseElements plugin to create the folders and keep everything inside the FileMaker script/calculations:

             

            BaseElements Plugin | Goya Pty Ltd

             

            File and Folder – BaseElements Plugin Help Centre

            • 3. Re: Applescript: Create a series of subfolders based on variable names
              erolst

              This seems to work:

               

              tell application "Finder"

                set folderList to {"level1", "level2", "level3", "level4", "level5"}

                set currentPath to the (path to desktop) as text

                repeat with i from 1 to (length of folderList)

                  set newFolder to item i of folderList

                  set nextPath to (currentPath & newFolder & ":")

                  if not (exists (folder nextPath)) then

                    make new folder at currentPath with properties {name:newFolder}

                  end if

                  set currentPath to nextPath

                end repeat

              end tell

              • 4. Re: Applescript: Create a series of subfolders based on variable names
                breeanne

                Thank you!! I'll see how well I can copy it to FileMaker!

                • 5. Re: Applescript: Create a series of subfolders based on variable names
                  breeanne

                  FYI, I wasn't sure how to use the Let function exactly, but managed to write the AppleScript as a calculated field, and this is what I got to work:

                   

                  "set folderName0 to " & Quote($Folder0) & ¶ &

                  "set folderName1 to " & Quote($Folder1) & ¶ &

                  "set folderName2 to " & Quote($Folder2) & ¶ &

                  "set folderName3 to " & Quote($Folder3) & ¶ &

                  "set folderName4 to " & Quote($Folder4) & ¶ &

                   

                   

                  "tell application \"Finder\"" & ¶ &

                    "set folderList to {folderName0, folderName1, folderName2, folderName3, folderName4}" & ¶ &

                    "set currentPath to the (path to desktop) as text" & ¶ &

                    "repeat with i from 1 to (length of folderList)" & ¶ &

                    "set newFolder to item i of folderList" & ¶ &

                    "set nextPath to (currentPath & newFolder & \":\")" & ¶ &

                    "if not (exists (folder nextPath)) then" & ¶ &

                    "make new folder at currentPath with properties {name:newFolder}" & ¶ &

                    "end if" & ¶ &

                    "set currentPath to nextPath" & ¶ &

                    "end repeat" & ¶ &

                  "end tell"

                   

                  Thanks again!!! HUGE help!

                  • 6. Re: Applescript: Create a series of subfolders based on variable names
                    erolst

                    Glad you got it working.

                     

                    What I meant was to use Let( ) for the variable construction,

                    and List( ) to get rid of the syntactic clutter of & ¶ &

                     

                    Let ( [

                      FMfolderList = List ( $Folder ; $Folder1 ; $Folder2 ; $Folder3 ; $Folder4 ) ;

                      FMfolderList = Substitute ( FMfolderList ; ¶ ; "\",\"" )

                      ] ;

                      List (    

                        "tell application " & Quote ( "Finder" ) ;

                            "set folderList to {\" & FMfolderList & "\"}" ;

                            "set currentPath to the (path to desktop) as text" ;

                            "repeat with i from 1 to (length of folderList)" ;

                                "set newFolder to item i of folderList" ;

                                "set nextPath to (currentPath & newFolder & \":\")" ;

                                "if not (exists (folder nextPath)) then" ;

                                    "make new folder at currentPath with properties {name:newFolder}" ;

                                "end if" ;

                            "set currentPath to nextPath" ;

                            "end repeat" ;

                        "end tell"

                      )

                    )

                    1 of 1 people found this helpful
                    • 7. Re: Applescript: Create a series of subfolders based on variable names
                      breeanne

                      Ohh, okay, I understand. Thanks for clarifying!!