3 Replies Latest reply on Jun 9, 2015 1:42 PM by FentonJones

    Script example for MAC or Windows that makes a directory using Command line or AppleScript

    MarcMcCall

      Title

      Script example for MAC or Windows that makes a directory using Command line or AppleScript

      Post

      Here is an example of a script that works on MAC or windows, that creates a directory on the desktop and creates a backup of the DB.  The script includes comand line or AppleScript steps to perform this.

      Have Fun!!!!!

      • Allow User Abort [ Off ]
      • Set Error Capture [ On ]
      • #--------------------------------------Freezes the window and navigates to the Preferences Screen....
      • Freeze Window
      • #
      • #--------------------------------------Sets the name of the folder to be created on the user's Desktop...
      • Set Variable [ $dirName; Value:"Products_Backups" ]
      • If [ Abs ( Get ( SystemPlatform ) ) - 1 //windows ]
      • #--------------------------------------If the current operating system is a Windows machine...
      • #--------------------------------------A folder will be created using the $dirName variable if the folder is not already created.
      • Set Variable [ $desktopUse; Value:Substitute ( "/" & Get ( DesktopPath ); [ "//" ; "" ]; [ "/" ; "\\" ]) ]
      • Send Event [ open document/application; "cmd.exe /C MKDIR " & Quote ( $desktopUse & $dirName & "\\" ) ]
      • Else
      • #--------------------------------------If the current operating system is not a Windows machine, meaning that if it is a Mac...
      • #--------------------------------------A folder will be created using the $dirName variable if the folder is not already created.
      • Perform AppleScript [ Native AppleScript: set theOutputFolderPath to path to desktop folder set theNewFolderName to "Products_Backups" tell application "Finder" if (exists folder (theOutputFolderPath & theNewFolderName as string)) = false then make new folder at desktop with properties {name:theNewFolderName} end if end tell ]
      • End If
      • #--------------------------------------The file name is set in variables based on the current Date and Time, and a copy of the Databse is saved to the appropriate folder on the user's Desktop.
      • Set Variable [ $filename; Value:GetAsText ("Products" & "_" & GetAsText( Substitute ( Get (CurrentDate) ; "/" ; "_")) & "_" & Substitute ( Substitute ( GetAsText (Get(CurrentTime)) ; ":" ; "" ) ; " " ; "") & ".fp7") ]
      • Set Variable [ $output; Value:"file" & Case ( Abs ( Get ( SystemPlatform ) ) - 1 ; "win:" ; "mac:" ) & Get ( DesktopPath ) & $dirName & "/" & $filename ]
      • Save a Copy as [ “$output” ] [ copy of current file ]
      • Set Variable [ $error; Value:Get ( LastError ) ]
      • #
      • Refresh Window [ Flush cached join results; Flush cached SQL data ]
      • If [ $error ]
      • #--------------------------------------If there was an error in the saving of the backup, an error dialog will display...
      • If [ $error = 3 ]
      • Show Custom Dialog [ Title: "Backup Error:"; Message: "There was an error making the Backup of your Database. You may only make a backup with a local copy of this database."; Buttons: “OK” ]
      • Else
      • Show Custom Dialog [ Title: "Backup Error:"; Message: "There was an error making the Backup of your Database. Database was not backed up. Error: " & $error; Buttons: “OK” ]
      • End If
      • Else
      • #--------------------------------------If the Database saved successfully, a dialog will display alerting the user to the fact...
      • Show Custom Dialog [ Title: "Backup"; Message: "Your Database has been backed up successfully."; Buttons: “OK” ]
      • End If 

        • 1. Re: Script example for MAC or Windows that makes a directory using Command line or AppleScript
          sdejong

          Is it possible to send me a sample script.  I am relatively new at scripting and I am making a mistake at the Send Event step.

          Thank you

          Simon deJong

          simon@ssimicro.com

           

          • 2. Re: Script example for MAC or Windows that makes a directory using Command line or AppleScript
            Bucko

            Great script. I did have one problem though - the applescript part does not use the variable $dirName. It uses fixed text. How do I get the value of a field or variable to be used for the folder name?

            Also, do you have any example scripts in an FMP file for ease of import? Some of the syntax is hard to translate to newer versions from the screenshot.

            Thanks

            • 3. Re: Script example for MAC or Windows that makes a directory using Command line or AppleScript
              FentonJones

              The original post was from a long time ago; so I don't know if you'll get an answer about getting a file. I can help with the AppleScript however. Yes, your correct to notice that he did not use a Variable for the AS; he just added a name via text.

              Basically, AppleScript cannot see a Variable, via the 'Native AppleScript' option. It could see a global field however, so you could use that; but it would have to be on the layout at the time it looked for it. If you use the other option, 'Calculated AppleScript', then you could use a Variable. However then you'd need to write a FileMaker syntax calculation which resulted in a valid AppleScript (with \" for 'text' and ¶ for each AS line).

              In this case however this is not very hard, as it only has a few lines. I find the simplest method for creating a folder is to use the fact that both AppleScript (and Unix) will NOT overwrite a folder (to save us from doing something really stupid). So this is a possible calculation to create a folder:

              Let (
              folder_name = People::Person;

              "tell application \"Finder\"¶
              try¶
              make new folder at desktop with properties {name: \"" & folder_name & "\"}¶
              end try¶
              end tell"
              )

              I'm just specifying a field; it could be a global. It does not need to be on a layout to make the AS work (as FileMaker will return the value of it beforehand). This could also be a Variable (with $name), which could be preset in a previous FileMaker Script Step (so you could see it beforehand).*

              I used 'try' as AppleScript will return a visual error, telling you if the folder already exists; which in this case you don't really care, as the folder is what you wanted. Before doing much serious you'd want to test the above first, to maker sure it creates a folder, but if you gave it a path and a name it should.

              * Another alternative (my favorite) is to use a Set Variable ($as) set to ALL of the calculation required. That way you can (in FileMaker Pro Advanced) view the entire AS which is going to run, with the FileMaker value already set; you would then see what AS will see. Then in the Perform Applescript's step, in the Calculated AppleScript, you can just write: $as