9 Replies Latest reply on Sep 18, 2014 5:49 PM by SimonIvarsson

    How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)

    SimonIvarsson

      Title

      How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)

      Post

      Hi Everyone.

      Sorry if my question is easy / stupid, but I'm not that good with scripting.

      What I'm trying to do is a button that creates / opens a folder with the same name as Table:Job ID

      I have tried using Apple Script, and that works as long as the folder name is not a $variable

      Example open a folder:

      tell application "Finder"
      activate
      open "folder:folder:folder:folder:"
      end tell

      Can anyone help?

       

        • 1. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
          FentonJones

          AppleScript (AS) doesn't know anything about FileMaker Variables; they were added to FileMaker many years after FileMaker's AppleScript was (and it's not been updated, as far as I know). You can use a require FileMaker field(s), as a global. Just reset it before you ask Perform AppleScript script step to run. 

          I don't know how much you know about FileMaker & AppleScript, so I'll tell you the easiest way to use it. Have the field(s) on a layout, and go there just before you run the AS. That will allow the Perform AppleScript "Native AppleScript" to work. Else you could use the other option, "Calculated AppleScript"; but then you'd have to redo it, for such things as quotes and returns (in this case not hard, but not done here, so ask if you want to see). This is an example from an old file I have, which will create a specified folder (if needed), then will open it.
          ----------------------------------------

          -- all fields needed (2) are on the layout
          set myFolder to cell "_cMyFolder_AS"  -- folder of this FM database file
          set folder_name to cell "FolderName" of current record  -- field of record
          set bakFolder to myFolder & folder_name

          -- create new folder if needed; open
          tell application "Finder"
              if not (exists folder bakFolder) then
                  make folder at myFolder with properties {name:folder_name}
              end if
                 open bakFolder
                 activate
          end tell

          ----------------------------------------
          I'd added some comment text to say what it does. AS uses "--" [ or (* and *) ], whereas FileMaker uses "//" [ or /* and */ ]

          So, the first thing it does is look at the folder of where all these new folders would be, i.e. my "_cMyFolder_AS", which is a calculation field, of the folder of the database file itself; it is the same for all records. In your case it would be wherever the folder that holds all these new folders. Then it adds the text of the field of the "current record" (since it's a regular text field, you need to tell AS that it needs data from the current record); to make the full path of the "new" folder.

          It then checks if the folder already exists. AS will not create a folder if it already exists (which is good). If it does not exist, then it creates it, and names it. Then it opens it (brings it to the front of Finder's visible folders. Then it uses activate to bring it to the "front", above FileMaker (you don't need to activate "Finder", as it is one application which is always active already; so I wait to bring it all to the front).

          This is the whole FileMaker script. It shows how you could dedicate a folder to AS, go there, then return. That way the  "Native AppleScript" will work (otherwise not).

          • 2. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
            FentonJones

            Oh, I forgot to say. The fact that I used a regular text field (not global) for the "folder name" means that you run this inside a Loop, on the records table, and do the folders for many at once. Two things: 

            AS will not create (overwrite) a folder that already exists. 

            The AS layout MUST be attached to the SAME table (occurrence) as the records with the folder name(s) | So the script steps to Go to Layout [ AS ] and return to "original" layout would NOT be inside the Loop, just before and after.

            • 3. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
              philmodjunk

              You might also consider adding a plug in that offers this capability so that you can use a FileMaker Script instead of AS as a way to create your folders.

              • 4. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
                FentonJones

                OK, I'll make this easier. This is what it looks like as a FileMaker calculation, which can be put within an FileMaker calculation field (Unstored), or can be put directly into the Perform AppleScript, Calculation options. This way you do not have to "go to an AS layout", as a calculation works anywhere of the correct table (occurrence):

                Let ( [ // puts the FM files into AS globals
                main_folder = "\"" & MyPath::_cMyFolder_AS & "\"";
                folder_name = "\"" & MyPath::FolderName & "\"";
                new_folder = "\"" & MyPath::_cMyFolder_AS & MyPath::FolderName & "\""
                 ];

                "tell application \"Finder\"" & ¶ &
                    "if not (exists folder " & new_folder & ") then" & ¶ &
                        "make folder at " & main_folder & " with properties {name:" & folder_name & "}" & ¶ &
                    "end if" & ¶ &
                       "open " & new_folder & ¶ &
                       "activate" & ¶ &
                "end tell"
                )

                • 5. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
                  SimonIvarsson

                  Thank you so much guys!
                  Ill give this a try right away!

                  • 6. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
                    SimonIvarsson

                    Your help is so much appreciated and It kind of works.

                    But I when expanded the script and made a few changes.. then nothing works.. :(

                    Error: Expected end of line but found “"”.

                    Ill try to explain what I want.

                    A folder should be created in a specific folder. 

                    Black text is a fixed path 

                    red is a variable path

                    blue is a variable and the name of the new folder

                    Volume/Container/Customer/JobID 

                    So what I want the operator to do:

                           
                    1. Select customer from a drop down list.
                    2.      
                    3. Type in the JobID
                    4.      
                    5. Run Script

                    This is how my script looks now. Ps, in this script every field is a variable (I guess) but I don't want that.

                    Let ( [ // puts the FM files into AS globals
                    volume = "\"" & Table::Volume & "\"";
                    container = "\"" & Table::Container & "\"";
                    customer = "\"" & Table::Customer & "\"";
                    folder_name = "\"" & Table::Job ID & "\"";
                    new_folder = "\"" & Table::Volume & Table::Container & Table::Customer & Table::Job ID & "\""
                     ];

                    "tell application \"Finder\"" & ¶ &
                        "if not (exists folder " & new_folder & ") then" & ¶ &
                            "make folder at " & volume & container & customer & " with properties {name:" & folder_name & "}" & ¶ &
                        "end if" & ¶ &
                           "open " & new_folder & ¶ &
                           "activate" & ¶ &
                    "end tell"
                    )

                     

                    • 7. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
                      FentonJones

                      This is my version of the above (which works). I changed a few names a little, as either I liked it better, or AS did not like it well. Also, I don't know that you want to add the "Volume name" to every record. It could be hard-coded into the calculation (if it's always the same). Or, better, it could come from another table (one record only) for such things. I main do the below using an Unix command instead; as an AppleScript can run a Unix command via: do shell script "unix command"

                      The advantage of the above is that it can create folders within folders within folders with only one line of text, instead of the several that AS uses. But the AS one is OK, once you read it. It just looks for the "top" folder, then the one below it, then the one below it, etc..

                      P.S. I kept the extra spaces, as, if you create a calculation field for the below (which I always do, so I can SEE what it looks like first), as it looks better, more like a real AS (which is where I first wrote and tested it (I almost always start in AppleScript Editor, as it can tell you better where something is wrong).

                       

                      "set volume_name to \"" & Table::Volume_txt & "\"" & ¶ &
                      "set container_name to \"" & Table::Container_txt & "\"" & ¶ &
                      "set customer to \"" & Table::Customer & "\"" & ¶ &
                      "set job_id to \"" & Table::JobID & "\"" & ¶ &
                      "set container_folder to volume_name & \":\" & container_name" & ¶ &
                      "set customer_folder to container_folder & \":\" & customer" & ¶ &
                      "set new_folder to customer_folder & \":\" & job_id" & "¶¶" &

                      "tell application \"Finder\"" & ¶ &
                      "    if not (exists folder container_folder) then" & ¶ &
                      "        make folder at volume_name with properties {name:container_name}" & ¶ &
                      "    end if" & ¶ &
                      "    if not (exists folder customer_folder) then" & ¶ &
                      "        make folder at container_folder with properties {name:customer}" & ¶ &
                      "    end if"& ¶ &
                      "    if not (exists folder new_folder) then" & ¶ &
                      "        make folder at customer_folder with properties {name:job_id}" & ¶ &
                      "    end if" & ¶ &
                      "    open new_folder" & ¶ &
                      "    activate" & ¶ &
                      "end tell

                      • 8. Re: How to create a Script that creates a folder with $Variable (Folder name = Table:Job ID)
                        FentonJones

                        Here is mostly the same thing thing, also run by AppleScript, but running the actual command line in Unix. As you can see, the command it is a little longer, with a bit more \" and / (Unix uses "/" between folders, not ":" as Apple {old type}, but it can do all 3 folders at once (if each is inside the one before). [ Each folder inside the one above; for simple hierarchy, you can just quote the whole thing at the end. It can also do it if otherwise, but needs more tweaks.]

                        I used the "open" command of Unix ("open" is "open" in either :-), as the path was Unix, with "/"; I just did it right after the "create folder" command. Unix also supports the "do not overwrite an existing folder" way of protection, so you don't have to "test" for it. It will add any folders needed (like the 2nd, if needed, etc.).


                        "set volume_name to \"" & Table::Volume_txt & "\"" & ¶ &
                        "set container_name to \"" & Table::Container_txt & "\"" & ¶ &
                        "set customer to \"" & Table::Customer & "\"" & ¶ &
                        "set job_id to \"" & Table::JobID & "\"" & "¶¶" &

                        "tell application \"Finder\"" & ¶ &
                        "   set new_folder to quoted form of (\"/Volumes/\" & volume_name & \"/\" & container_name & \"/\" & customer & \"/\" & job_id & \"/\")" & ¶ &
                        "   do shell script \"mkdir -p \" & new_folder & \" | open \" & new_folder" & ¶ &
                        "    activate" & ¶ &
                        "end tell"