10 Replies Latest reply on Mar 14, 2012 9:45 AM by stinchy

    Calculated Applescript Query

    stinchy

      This is my first post and hopefully someone can help . . .

       

      I need to write a calculated applescript that will move the entire contents of a folder on my desktop ( static folder named "Transfers" ) to another folder on my network ( this folder is already created by a previous applescript based on a field name so can be related by a variable name e.g. $foldername).

       

      I have looked around for examples and nothing really seems to work.

       

      My first shot was as follows :-

       

      "do shell script mv /Users/myname/Desktop/Transfers/*.* /$foldername/"

       

      Would be extremely grateful if someone has any ideas on what this calculation should be . . . . .

        • 1. Re: Calculated Applescript Query
          GaryTate

          Here you go...

           

          set SourceFolder to "folder 1"

          set targetFolder to "folder 2"

           

          tell application "Finder"

              move every item of folder SourceFolder of desktop to folder targetFolder of desktop

          end tell

          • 2. Re: Calculated Applescript Query
            comment

            stinchy wrote:

             

            My first shot was as follows :-

             

            "do shell script mv /Users/myname/Desktop/Transfers/*.* /$foldername/"

             

             

            The idea with calculated AppleScript is for the calculation to return the literal text of the script to run. In your example, the calculation would return =

             

            do shell script mv /Users/myname/Desktop/Transfers/*.* /$foldername/

             

            and that won't work because $foldername is undefined. Assuming $foldername is a Filemaker $variable defined in the running script, try =

             

            "do shell script mv /Users/myname/Desktop/Transfers/*.* /" & $foldername/ & "/"

             

             

            EDIT:

            I believe you need to add some quotes to make that a valid call to shell.

             

            Message was edited by: Michael Horak

            • 3. Re: Calculated Applescript Query
              sporobolus

              on 2012-13-03 10:30 stinchy wrote

              I need to write a calculated applescript that will move the entire contents of a folder on my desktop ( static folder named "Transfers" ) to another folder on my network ( this folder is already created by a previous applescript based on a field name so can be related by a variable name e.g. $foldername).

               

              I have looked around for examples and nothing really seems to work.

               

              My first shot was as follows :-

               

              "do shell script mv /Users/myname/Desktop/Transfers/. /$foldername/"

               

               

              there are two basic ways, via the shell and via Finder; the script above is an

              attempt at using the shell, but the "do shell script" command requires that you

              pass it a string containing the actual shell command, which means enclosing

              the shell command in double-quotes; since a calculated AppleScript means you

              are already using double-quotes to enclose the whole AppleScript command, you

              must escape the internal quotes with backslashes; there's also the possibility

              that the paths you pass to mv include spaces, so it's a good practice to

              enclose those paths in single-quotes

               

              beyond that, if you want the contents of your $foldername variable to be used

              in the command, you have to insert it into the calculation so that it appears

              at the right place withing the resulting string; as above, you'd be passing the

              name of the variable, not the string it contains, to the shell command, and the

              shell won't know what "$foldername" is; it's unclear from your post what is in

              the $foldername variable; the way you used it above, it would have to be a

              POSIX path stripped of initial and trailing slashes; i would recommend you set

              $foldername to contain leading and trailing slashes so that it is in itself a

              full path, e.g.

               

                 $foldername = "/Volumes/network vol/some folder path/"
              

               

              also note that "." is a DOSism and won't transfer all files on a *nix system,

              just * is better (though will still skip invisibles)

               

              so here is a calc that should work given the above (all one line in case the

              site folds it)

               

                 "do shell script \"mv '/Users/myname/Desktop/Transfers/*' '" & $foldername & 
              "'\""
              

               

              (mv will, by the way, wipe out any same-named files at the destination)

               

              by contrast, here's a pure AppleScript version of the calculated AppleScript;

              in this case i assume that $foldername contains an HFS path, e.g

               

                 $foldername = "network vol:some folder path:"
              

               

              note that this approach also requires escaping quotes within the calculated

              string; watch for linewrap:

               

                 "tell application \"Finder\"
                   set source_folder to folder ((path to desktop folder as string) & 
              \"Transfers\")
                   set dest_folder to folder \"" & $foldername & "\"
                   move entire contents of source_folder to dest_folder
                 end tell"
              

               

              • 4. Re: Calculated Applescript Query
                stinchy

                Firstly thanks everyone for your (very prompt) suggestions.

                 

                I have tried them all and the closest so far is from Steve.

                 

                Steve's suggestion didn't work with the * in the script. However once I removed the * then it worked - although it actually moved the entire folder and it's contents from the desktop into the desired destination folder. The script looks like this :–

                 

                "do shell script \"mv '/Users/clive/Desktop/Transfers/' '" & $foldername & "'\""

                 

                However what I really need is to leave the Transfers folder on the desktop and only move the contents to the destination folder.

                 

                Have I typed something wrong or is there something simple missing ?

                 

                Once again I would really like to thank everyone who has replied – this has saved me no end of time.

                • 5. Re: Calculated Applescript Query
                  BruceRobertson

                  But it wasn't *

                   

                  It was *.*

                  • 6. Re: Calculated Applescript Query
                    comment

                    I thought your question was about Filemaker - not about AppleScript or shell scripting as such. I'd suggest you perfect your script in AppleScript editor before you try implementing it in Filemaker.

                     

                    See also:

                    https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/mv.1.html

                    • 7. Re: Calculated Applescript Query
                      stinchy

                      Michael,

                       

                      You are right that originally my attempt was *.*, however Steve's suggestion was to use * on its own.

                       

                      I have tried both and get an error.

                       

                      I think another suggestion of brushing up on my AppleScript is a valid one and I shall be certain to learn more soon.

                      • 8. Re: Calculated Applescript Query
                        ariley

                        Actually, try perfecting your shell script first in terminal.

                         

                        Make sure the mv command does exactly what you want.

                         

                        agnes b. riley . filemaker and web development

                        www.zerobluetech.com

                        T 201-299-6223 (NJ) . 212-842-8830 (NY) . 917-660-7221 (C)

                        FileMaker Certified in 10 and 11

                        • 9. Re: Calculated Applescript Query
                          sporobolus

                          on 2012-14-03 2:33 stinchy wrote

                          Steve's suggestion didn't work with the * in the script. However once I removed the * then it worked - although it actually moved the entire folder and it's contents from the desktop into the desired destination folder. The script looks like this :–

                           

                          "do shell script \"mv '/Users/clive/Desktop/Transfers/' '"&  $foldername&  "'\""

                           

                          However what I really need is to leave the Transfers folder on the desktop and only move the contents to the destination folder.

                           

                          ah, the problem is that the * is within the single quotes, which prevents the

                          shell from expanding the wildcard; try this:

                           

                             "do shell script \"mv '/Users/myname/Desktop/Transfers/'* '" & $foldername & 
                          "'\""
                          

                           

                          • 10. Re: Calculated Applescript Query
                            stinchy

                            Steve,

                             

                            You are my hero !!!!

                             

                            Works a treat.

                             

                            Really must thank you for your advice. Applescript is new to me and something that I think has some great uses in conjunction with filemaker ( thinking of possibly automating some kind of scan and save options in future !! ).

                             

                            Anyone have any book suggestions ?

                             

                            Thanks to everyone else who gave suggestions too - much appreciated.

                             

                            Not sure I have the experience to assist anyone in future - but in time you never know . . . . . . . . . .