13 Replies Latest reply on Apr 2, 2013 8:22 PM by Lemmtech

    Processing Files, how to rename and move files?

    mbust

      Hello,

       

      I have a db that gets files via email using 360Works email plug in. I need to move and rename the files from the temporary folder where FM has them to another folder for later processing. To do that I wrote an AppleScript that takes the file, renames the files and moves moves them. The AppleScript works perfectly well when it runs by itself from the AppleScript editor, but I want to run it from a FileMaker script as a calculated script so that I can take the path of the temporary folder from a FM variable. I can easily save the AppleScript to the FM Script, but when I run it, I get a 2741 Applescript error. I don't know how to debug the AppleScript from within FM.

       

      So can anyone please show me how to rename and move files with a FM script?

       

      Thanks.

        • 1. Re: Processing Files, how to rename and move files?
          BruceHerbach

          When you create an apple script by calcultation you have to remember everthing is text

          So make sure all of the text is en quotes and the variables have an & before and after

           

          HTH

          Bruce

          • 2. Re: Processing Files, how to rename and move files?
            mbust

            Yes, that much I could tell.  Also because FM won't be happy if it is not a function, something it recognizes as legitimate text or variables. But having that, I still have the error message.  I had thought that pehaps the AppleScript strings encased in quotation marks would cause problems, so I've doubled checked that any quotation mark has  a back-lash (\) preceding it. And I still can't get it to work.  Any help will be much appreciated.

            • 3. Re: Processing Files, how to rename and move files?
              BruceRobertson

              "Also because FM won't be happy if it is not a function"

              What does that mean?

               

              It would be best if you post your file, or simpified copy, or copy of your script.

              There are an infinite number of ways you could be doing this wrong.

              Rather than asking us to guess and waiting for results, make it easy and show us what you are doing.

              • 4. Re: Processing Files, how to rename and move files?
                mbust

                I mean that it will generate an error if it is not something that corresponds to a function, a variable, or text it recognizes as a valid string.

                 

                The code is here (the file that it receives, like I said comes from 360Works Email function that pulls IMAP mail, with the attachment property set to true):

                "on SearchAndReplace(SearchString, ReplaceString, SearchIn)

                    set text item delimiters to SearchString

                    set Temp to text items of SearchIn

                    set text item delimiters to ReplaceString

                    set z to Temp as string

                    set text item delimiters to \"\"

                    return z

                end SearchAndReplace

                 

                tell application \"Finder\"

                   

                    set DestinationStorageFolder to alias \"MacintoshHD:Users:mbust:Desktop:~PT:Customer Templates:Excel Email Attachments Landing:\"

                    set FileItem to " & $FileName & "

                    set String1 to \"PTImportTemplate.xls\"

                    set FolderName to my SearchAndReplace(String1, \"\", FileItem)

                    set FolderName to my SearchAndReplace(\"/\", \":\", FolderName)

                    set FileItem to my SearchAndReplace(\"/\", \":\", FileItem)

                    set FileItem to alias FileItem

                   

                    set FolderName to alias FolderName

                    set NewFileName to \"(Sent by-user@email.com;FileID" & $FileProcID & ")\" & \"PTImportTemplate.xls\" as string

                    set name of FileItem to NewFileName

                    set FileCount to count of files in FolderName

                    set NewFile to file 1 of FolderName

                   

                    move NewFile to DestinationStorageFolder

                   

                end tell

                "

                 

                Thank you for any help on this.

                • 5. Re: Processing Files, how to rename and move files?
                  RubenVanDenBoogaard

                  You could also use the (free) 360Works Scriptmaster plugin to list/move/delete files (among other things)

                   

                  Gives you more control over the differtent steps and works x-plat.

                   

                  Hope that helps,

                   

                  Best regards,

                   

                  Ruben van den Boogaard

                  Infomatics Software

                  ruben@infomatics.nl

                  1 of 1 people found this helpful
                  • 6. Re: Processing Files, how to rename and move files?
                    BruceRobertson

                    When writing applescripts of this type, I prefer to create the script template as one variable; then create a second variable which contains the script where all the substitutions have been peformed; and finally, perform the calculated applescript on this variable. This lets you look at the resulting script in script debugger to see if it is correct. You can also set a field to the variable so you can examine the script. Something like this:

                     

                    Set Error Capture [ On ]

                    Set Variable [ $filename; Value: "invoices" ]

                    Set Variable [ $as; Value:List(
                    "on SearchAndReplace(SearchString, ReplaceString, SearchIn)";

                    "  set text item delimiters to SearchString";

                    "  set Temp to text items of SearchIn";

                    "  set text item delimiters to ReplaceString";

                    "  set z to T emp as string";

                    "  set text item delimiters to \"\"";

                    "  return z";
                    "end SearchAndReplace";

                     

                    "tell application \"Finder\"";

                    " set DestinationStorageFolder to alias \"MacintoshHD:Users:mbust:Desktop:~PT:Customer Templates:Excel Email Attachments Landing:\"";

                    "  set FileItem to \"" & $FileName & "\"";

                    "  set String1 to \"PTImportTemplate.xls\"";

                    "  set FolderName to my SearchAndReplace(String1, \"\", FileItem)";

                    "  set FolderName to my SearchAndReplace(\"/\", \":\", FolderName)";

                    "  set FileItem to my SearchAndReplace(\"/\", \":\", FileItem)";

                    "  set FileItem to alias FileItem";

                    "  set FolderName to alias FolderName";

                    "  set NewFileName to \"(Sent by-user@email.com;FileID" & $FileProcID & ")\" & \"PTImportTemplate.xls\" as string";

                    "  set name of FileItem to NewFileName";

                    "  set FileCount to count of files in FolderName";

                    "  set NewFile to file 1 of FolderName";

                    "  move NewFile to DestinationStorageFolder";

                    "end tell"

                    )

                    ]

                     

                    Perform AppleScript [ Calculated AppleScript: $as ]

                    Set Variable [ $error; Value:Get( LastError) ]
                    Exit Script [ Result: $error ]

                    • 7. Re: Processing Files, how to rename and move files?
                      gcatnine

                      instead of using a Apple script, just use a plugin.

                      here there is a freeware plugin http://www.goya.com.au/baseelements/plugin

                      you can use functions like copy file and move file

                      much more simple

                      1 of 1 people found this helpful
                      • 8. Re: Processing Files, how to rename and move files?
                        Malcolm

                        An inspired use for List().

                         

                        malcolm

                        • 9. Re: Processing Files, how to rename and move files?
                          BruceRobertson

                          Inspired use for List(); thanks Malcolm. Yes, it can be helpful, it does let you debug the syntax of each line, allows you to easily comment out and use alternate lines, etc. And of course, I should have said it lets you look at the script in variable watcher.

                          • 10. Re: Processing Files, how to rename and move files?
                            mbust

                            That is an amazing list!  Great way to debug scripts.  Thank you. This did the trick!

                            • 11. Re: Processing Files, how to rename and move files?
                              fmp

                              Another easy method is to use a global field to hold the Applescript, and Perform Applescript Calculated on that field. No escaping of quotes, etc., is required. For example:

                               

                              -- tell application "Filemaker Pro"

                              tell window 1

                              tell layout "Main"

                              set fp to get data cell "fullpath" of current record

                              end tell

                              end tell

                              -- end tell

                               

                              tell application "Finder"

                              open file fp

                              end tell

                               

                              You can copy it out into the Script Editor to run it (after uncommenting the tell/end tell Filemaker lines) to test, and copy it back into the field when ready (and comment out the tell/end tell Filemaker lines).

                              • 12. Re: Processing Files, how to rename and move files?
                                mbust

                                Here's an AppleScript I quickly threw together that creates a FileMaker-formatted list for use with Bruce's suggestion, in case others want to turn AppleScripts to lists. Using this script avoids some potential mistakes of missing some escape "\" characters, semicolons, etc.  The script requires TextWrangler, the (free) text editor, but can easily be modifed to work with other programs:

                                --Start Script

                                (*

                                INSTRUCTIONS: To convert an AppleScript to a FileMaker Variable list, copy the AppleScript to be turned into a list (not this one)  into an empty

                                TextWrangler window and run this script from an AppleScript editor.  The result will be copied to the clipboard formatted to be copied

                                directly to a FM variable. The TextWrangler window will be automatically deleted.

                                To execute, just run a calculated script calling on that variable.

                                *)

                                 

                                 

                                tell application "TextWrangler"

                                    tell window 1

                                        --replace quotation marks

                                        replace "\"" using "\\\"" options {search mode:literal, starting at top:true}

                                        --Add semicolons

                                        replace "

                                " using "\";

                                \"" options {search mode:literal, starting at top:true}

                                        --remove some empty semicolons, though is mostly cosmetic, as leaving them will not affect the script.

                                        replace "\"                    \";" using "" options {search mode:literal, starting at top:true}

                                        replace "\"        \";" using "" options {search mode:literal, starting at top:true}

                                    end tell

                                   

                                    set a to text of window 1

                                --To keep the TextWrangler window from closing, comment out the next line

                                    close window 1 saving no

                                    set a to characters 1 thru -4 of a as string

                                    set x to "list(

                                    \"" & a & "

                                    ) " as string

                                   

                                end tell

                                tell application "Finder" to set the clipboard to x

                                 

                                --End Script

                                • 13. Re: Processing Files, how to rename and move files?
                                  Lemmtech

                                  I use the FileFire plugin from Dacons for this sort of thing works great and it's cross platform.