5 Replies Latest reply on Oct 11, 2009 9:35 PM by FentonJones

    external AScript targeting a table for new record

    anode505

      Title

      external AScript targeting a table for new record

      Post

      I'm just starting to play with AS integration with FM.

      DB= AS Test

      first table = ASTest

      Second table = ASTest2

      (both tables identical, done via copy)

       

      set nme to "Bob"

      set num to "23"

      tell application "FileMaker Pro Advanced"

        activate

       

        -- This works on current table

       

        create new record at the end

        go to the last record

        set the contents of field "Name" of current record to nme

        set the contents of field "Number" of current record to num

       

      -- This doesn't work.   

      -- This creates an object not found by FM (AS error)

      -- It does create the record, just won't enter data

       

        tell table "ASTest2" of database "AS Test"

          create new record at the end

          go to the last record

          set the contents of field "Name" of current record to nme  -- <--- Fails here

          set the contents of field "Number" of current record to num

        end tell

       

      end tell 

        • 1. Re: external AScript targeting a table for new record
          anode505
            

          OK found it!

           

          set the contents of field "Name" to nme   (removing the of current record part) of the bad section 

          • 2. Re: external AScript targeting a table for new record
            FentonJones
              

            A construction that seems to always work (for me) is to go the layout (much like you would in FileMaker). I then set the new record into an AppleScript variable, to use instead of "current record". You will not see the new record created. It is faster not to see it, if you're creating a lot of records this way. If you want to see it, then use your method of going to the record instead. But it is the go to layout which ensures it will be created in the correct table.

             

             

            tellapplication "FileMaker Pro Advanced"

            activate

            telldatabase "AS_test"

            go tolayout "AStest2"

            set newRec to create new record

            setcell "nme" ofnewRecto "Bob"

            setcell "num" ofnewRecto "23"

            end tell

            end tell 

             

             

            BTW, if you are running this inside a FileMaker Perform AppleScript step, you do not need the tell FileMaker, nor the activate; just the below (unless you want to see the record):

             

            go to layout "AStest2"

            set newRec to create new record

            set cell "nme" of newRec to "Bob"

             

            set cell "num" of newRec to "23" 

            • 3. Re: external AScript targeting a table for new record
              anode505
                

              OK, I'm stumped. When this runs, it changes every record to curent values. I added the go to first record and go to last record  at the end just to force a commit.

               

              tell application "IndigoServer"

              -- get variables

              set tble to value of variable "FM_Table"
              set mesg to value of variable "FM_Message"
              set tp to value of variable "FM_Type"
              set dev to value of variable "FM_Device"
              end tell

              tell application "FileMaker Pro Advanced"
              --activate
              tell table tble of database "Indigo"
              activate
              create new record at the end
              go to the last record
              set the contents of field "Time" to (time string of (current date)) as string
              set the contents of field "Date" to date string of (current date) as string
              set the contents of field "Message" to mesg
              set the contents of field "Type" to tp
              set the contents of field "Device" to dev
              go to the first record
              go to the last record
              end tell
              end tell









              • 4. Re: external AScript targeting a table for new record
                anode505
                  

                Fenton, thanks!

                 

                I'm usinging your version.  Works like a charm.

                 

                But I still want to know why all records get changed with mine.  (its personal now :) ) 

                • 5. Re: external AScript targeting a table for new record
                  FentonJones
                    

                  Good for you. AppleScript sees FileMaker a little differently than you at first think. Actually I believe it is more how FileMaker sees itself, but we don't think about it much. Within FileMaker there is a big difference between methods/structures which see all the records (relationships see all the records, though they may only return a few), and found sets, which see only their own records. 

                   

                  In AppleScript this would be the difference between the terms database (all) and document (found set). Generally a database has tables and fields, whereas a document has layouts, cells, and a "current record".

                   

                  If I say: field "StartDate" of current record

                  I get that record's value.

                  If I say: field "StartDate" of record 1

                  I get the 1st record's value.

                  But if I say: field "StartDate"

                  I get the values of that field from all the records of the found set (as an AppleScript list).

                   

                  If I say:

                  tell database 1

                  field "StartDate"

                  end tell

                  I get ALL the values of that field from ALL records of the table.

                   

                  If I say: cell "StartDate"

                  I get the 1st record's value. I do not get all the values in all the records.

                  If I say: cell "StartDate" of current record

                  I get the currently active record's value. 

                   

                  So, there is some overlap. But usually you want to use cell not field if you're working in one record at a time. 

                   

                  There is also some overlap between database on the one hand, and document or window on the other. Also, there is a default in AppleScript, if you have a FileMaker file/window open, which is that file, the layout of that window, the fields on that layout. So if you're running an AppleScript from within FileMaker, you may not need to specify so much explicitly. You do however if: 1. A field (cell) you want is not on the layout, or, 2. You want another layout or table.

                   

                  For that reason, in order to keep my AppleScripts simpler, with fewer hard-coded names in its code, I often create a special hidden AppleScript layout, with the fields I need. Then switch to the layout, run the AppleScript, and switch back; no one sees.

                   

                  FileMaker made a mistake I think in not including the documentation for AppleScript in FileMaker 10 (9 either I don't think). But it is still available. There is an object tree which shows some of the above. Though figuring out exactly what objects and commands to use can be a struggle sometimes. There are those who are real experts, such as Bruce Robertson,* who know all the tricks. But I know enough to get by :-]

                   

                  http://fmdl.filemaker.com/MISC/fmp10/fp/apple_events_reference_wwe.zip

                   

                  *He recently posted the fact that AppleScript's getting data of a field is likely the fastest method to accumulate the IDs from a found set, faster than any Custom Function. Though still not as fast as Copy All Records I don't think.