3 Replies Latest reply on Jun 7, 2010 5:50 PM by FentonJones

    Using AppleScript Calculated and IF Statements

    ericdano

      Title

      Using AppleScript Calculated and IF Statements

      Post

      This is driving me crazy. I want to be able to add contacts to my address book, and am trying to code in some logic to deal with if they have up to 5 email or phone numbers using IF statements.....but FileMaker keeps throwing errors. Here is the code as a Custom Function:

       

      "

      tell application \"Address Book\"¶

              set thePerson to make new person with properties {first name:\"" & firstName & "\", last name:\"" & lastname & "\"}¶

              make new related name at end of related names of thePerson with properties {label:\"Parents\", value:\"" & parents & "\"}¶

              make new email at end of emails of thePerson with properties  {label:\"" & emailaddress1label & "\", value:\"" & emailaddress1 & "\"}¶

              if (\""  & emailaddress2 & "\") is not null then¶

                make new email at end of emails of thePerson with properties  {label:\"" & emailaddress2label & "\", value:\"" & emailaddress2 & "\"}¶

              end if¶

              if (\""  & emailaddress3 & "\") is not null then¶

                make new email at end of emails of thePerson with properties  {label:\"" & emailaddress3label & "\", value:\"" & emailaddress3 & "\"}¶

              end if ¶ 

              if (\""  & emailaddress4 & "\") is not null then¶

                make new email at end of emails of thePerson with properties  {label:\"" & emailaddress4label & "\", value:\"" & emailaddress4 & "\"}¶

              end if¶

              if (\""  & emailaddress5 & "\") is not null then¶

              make new email at end of emails of thePerson with properties  {label:\"" & emailaddress5label & "\", value:\"" & emailaddress5 & "\"}¶

              end if¶

       

              make new phone at end of phones of thePerson with properties {label:\"" & phone1label & "\", value:\"" & phone1 & "\"}¶

              if (\""  & phone2 & "\") is not null then¶

              make new phone at end of phones of thePerson with properties {label:\"" & phone2label & "\", value:\"" & phone2 & "\"}¶

              end if¶

              if (\""  & phone3 & "\") is not null then¶

              make new phone at end of phones of thePerson with properties {label:\"" & phone3label & "\", value:\"" & phone3 & "\"}¶

              end if¶

              if (\""  & phone4 & "\") is not null then¶

              make new phone at end of phones of thePerson with properties {label:\"" & phone4label & "\", value:\"" & phone4 & "\"}¶

              end if¶

              if (\""  & phone5 & "\") is not null then¶

              make new phone at end of phones of thePerson with properties {label:\"" & phone5label & "\", value:\"" & phone5 & "\"}¶

              end if¶

              make new address at end of addresses of thePerson with properties {label:\"Home\", street:\"" & address1 & "\", city:\"" & city1 & "\", state:\"" & state1 & "\", zip:\"" & zip1 & "\"}¶

              if (\""  & address2 & "\") is not null then¶

                make new address at end of addresses of thePerson with properties {label:\"Other\", street:\"" & address2 & "\", city:\"" & city2 & "\", state:\"" & state2 & "\", zip:\"" & zip2 & "\"}¶

              end if¶

              save¶

      end tell¶

      -- open the new entry in the address book¶

      tell application \"Finder\"¶

      set theURL to \"addressbook://\" & id of thePerson¶

      open location ( theURL )¶

      end tell¶

      "

       

      Ideas? Thanks!

        • 1. Re: Using AppleScript Calculated and IF Statements
          MarkBanks

          If it's of assistance, there is sample code for adding contacts to the OS X address book at:

           

          http://www.banks.id.au/filemaker/createcontactinaddressbook/

          • 2. Re: Using AppleScript Calculated and IF Statements
            ericdano

            Well, I have referred to that link. What I want to do is get fields from Filemaker and put them into Address Book.

             

            I put in all those IF statements because it seems Applescript will throw an error if you try adding a field that is null.

             

            So, I basically need to check to see if it is null before having AppleScript do anything with it.

            • 3. Re: Using AppleScript Calculated and IF Statements
              FentonJones

              I think I'd do it this way.

               

              1. set AppleScript variables to the FileMaker fields (current record)

              set ALL of them whether FileMaker has anything in the field or not.

              AppleScript is happier that way. It won't test a variable until it's been set, to something.

               

              2. Test each to see if there's anything. I'd use,

               

              if length of emailaddress2 = 0

              set emailaddress2 to missing value -- AppleScript constant

              end if

               

              It only needs to reset the variable to missing value if it was empty (not in FileMaker field)

               

              3. I'd put all this testing in a subscript down at the bottom out of the way, and call it. All it's doing is resetting the empty variables to missing value.

               

              4. Then, in the Address Book block, test for missing value in the if's instead of null (which is another AppleScript constant, but I usually see it used with numbers more).