1 2 Previous Next 19 Replies Latest reply on Sep 24, 2015 6:22 PM by bjbk

    Calculated AppleScript - escape character challenge

    bjbk

      Hello, All!

      I have a challenging nesting issue perhaps we can come up with a solution. I have been working on this for quite a while to no avail. Sometimes staring at a problem for too long...

      I have an AppleScript in my solution that manipulates a third party website to automate the task of populating the fields on a form. I have a line of code that works just fine in a native Applescript but I would prefer to use a calculated Applescript for future development reasons.

      Here is the piece of native Applescript code:

       

      do JavaScript "function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return \"true\";} else {return \"false\";}} testFieldset();"
      
      

      The challenge is that Applescript has to use a backslash to escape the JavaScript return "true" and "false" code. I tried single quotes, \\"" and even the Quote () function but I'm still getting an error in the script as I just can't seem to wrap my head around the proper escape syntax to accomplish this.

       

      "do JavaScript \"function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return " & Quote ( "\"true\"" ) & ";} else {return \\""false\\"";}} testFieldset();\""¶

       

      Perhaps someone has been through this before.

      Thanks for any help!

        • 1. Re: Calculated AppleScript - escape character challenge
          Malcolm

          Applescript has to use a backslash to escape the JavaScript return "true" and "false" code.  I tried single quotes,
          "" and even the Quote () function but I'm still getting an error in the script as I just can't seem to wrap my head around the proper escape syntax to accomplish this.

           

          From memory, four quotes in a row produced a single quote in the output. Nowadays it may be cleaner to concatenate using Char(34) or does that still need escaping?

           

          Malcolm

          1 of 1 people found this helpful
          • 2. Re: Calculated AppleScript - escape character challenge
            sporobolus

            on 2013-06-03 22:08 bjbk wrote

            The challenge is that Applescript has to use a backslash to escape the JavaScript return "true" and "false" code.  I tried single quotes,
            "" and even the Quote () function but I'm still getting an error in the script as I just can't seem to wrap my head around the proper escape syntax to accomplish this.

             

            "do JavaScript \"function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return " & Quote ( "\"true\"" ) & ";} else {return
            ""false
            "";}} testFieldset();\""¶

             

            Perhaps someone has been through this before.

             

            yes, many times

             

            as a demonstrative example, paste this in as Calculated AppleScript and run the

            script:

             

            "display dialog \" here is a quote --> \\\" <-- and here is a backslash --> 
            \\\\ <--\""
            

             

            basically, you need to still have backslashes left in the string once

            AppleScript gets it, so Quote() isn't sufficient, you have escape the

            backslashes as well

             

            (one odd detail — when i click OK in the expression editor, then click Specify…

            again, the latter four-backslash construct remains, but once i click OK to

            "Perform AppleScript" Options, the four backslashes become three; this doesn't

            make sense to me, so i assume the triple-backslash is a shorthand that

            FileMaker supports, not true escaping)

             

            so your first code line, with the necessary "tell" added, and modified to be a

            calculated AppleScript, would be:

             

            "tell app \"Safari\"¶
               do JavaScript \"function testFieldset() {var fieldsetExists = 
            $('fieldset')[1]; if (fieldsetExists) {return \\\"true\\\";} else {return 
            \\\"false\\\";}} testFieldset();\"¶
            end tell"
            

             

            1 of 1 people found this helpful
            • 3. Re: Calculated AppleScript - escape character challenge
              bjbk

              I'll try the Char ( 34 ) to see if that helps.  Thanks!

              The easiest solution has been to run as a native Applescript.  But I just wanted to overcome this challenge because there may be a time in the future where a field name may change breaking the script.  If I go calculated, the fields will update on their own.

              This has been quite an exercise for me!

              • 4. Re: Calculated AppleScript - escape character challenge
                bjbk

                Thanks for the help!  However, I have tried that and I get this error:

                Screen Shot 2013-06-04 at 10.42.22 AM.png

                Here is the full code [as calculated AppleScript]:

                "tell application \"Safari\"" & ¶ &

                      "tell window 1" & ¶ &

                           "set properties of current tab to {URL:linkTOwebsite}" & ¶ &

                          "delay 1" & ¶ &

                          "tell document 1" & ¶ &

                               "do JavaScript \"function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return \\\"true\\\";} else {return \\\"false\\\";}} testFieldset();\"" & ¶ &

                               "if result = \"true\" then" & ¶ &

                                    "display dialog \"You have a report in progress.  Please select the desired report.\" default button {\"OK\"}" & ¶ &

                               "repeat until (do JavaScript \"function contentInH2() {var contentReturn = $('#content-apps h2').html(); return contentReturn} contentInH2();\") is \"Report\"" & ¶ &

                               "delay 0.5" & ¶ &

                               "end repeat" & ¶ &

                               "else" & ¶ &

                                    "do JavaScript \"document.forms['frmAddData']['txtNum'].value='\" & Number & \"';" & ¶ &

                                    "do JavaScript \"document.forms['frmAddData'].submit()\"" & ¶ &

                                    "end if" & ¶ &

                               "end tell" & ¶ &

                     "end tell" & ¶ &

                "end tell"

                 

                I noticed that your example does not excape the text but puts the carriage return inline.  I'll try that too just to simplify the code and make sure that AppleScript gets the returns it needs.

                Just so we are clear on what the code is doing:

                 

                Go to website.  Check to see if there is a report in progress. (testFieldset function)

                If there is, tell the user to select it. (there is another caclulated Applescript step that deals with this senario, filling in the form on the subsequent page.)

                If not, then add a number to the new field and submit to start a fresh report. (A separate script deals with that.)

                 

                If I run this as a native script, no problem.  I just want to make it a calculated script to future proof the Filemaker field referenced (in this case " & Number & " part that is refereced outside of this code snip changes.

                Hope this makes sense.
                Thanks again for all your help.  Thinking out loud with others really helps!

                 

                Message was edited by: bjbk

                • 5. Re: Calculated AppleScript - escape character challenge

                  Apart from looking ugly, this approach is hard to maintain, test, or change.

                  Better is to put the script (as it can be tested with Script Debugger) into a (global) field as is.

                  You then can call it by

                    Perform AppleScript table::globalField

                   

                  Winfried

                  • 6. Re: Calculated AppleScript - escape character challenge
                    TonyWhite

                    Write the AppleScript to a global using script steps to protect you from

                    field name changes breaking your code.

                     

                    Optionally, use Custom Function to make DOUBLE_QUOTE chars easier to add to

                    and read in the code.

                     

                    Build the JavaScript lines using 2 lines not 1:

                     

                    Set myJavaScriptVar to ³for example...really complicated to read if you have

                    lots of stuff going on²

                    do JavaScript myJavaScriptVar

                     

                    This will allow you to see what you are running in JavaScript and help spot

                    errors

                     

                    When the AppleScript has been written out to the global...run it.

                     

                    If the AppleScript does not work, copy it into Script Debugger .

                     

                    Repeat until it works.

                     

                    Hope that helps.

                     

                    All the best,

                     

                     

                    Tony White

                    Tony White Designs, Inc.

                    Tel: 646-714-2797 (Google Voice)

                    Tel: 718-797-4175

                    tony_white@twdesigns.com

                    http://www.twdesigns.com

                    • 8. Re: Calculated AppleScript - escape character challenge
                      sporobolus

                      on 2013-06-04 11:04 Winfried Huslik wrote

                      Apart from looking ugly, this approach is hard to maintain, test, or change.

                       

                      for someone who has been wranglings strings since SNOBOL, i think it's

                      beautiful! :?>

                       

                       

                      Better is to put the script (as it can be tested with Script Debugger) into a (global) field as is.

                      You then can call it by

                         Perform AppleScript  https://fmdev.filemaker.com/message/117233#117233#117233/table::globalField Re: Calculated AppleScript - escape character challenge

                       

                      the forum software mangled this, i think that was

                       

                      "then you can call it by

                         Perform AppleScript [table::globalField]
                      

                      "

                       

                      that's a reasonable idea but it creates maintenance troubles of its own; it

                      splits the code into multiple places, requires a special layout kept restricted

                      from general users, and if this is run from FMS, changing the script requires

                      either copying it into the global field from a startup script (reintroducing

                      the double-escaping), or taking the file offline

                       

                      once a script becomes fairly complex, i would strongly consider maintaining it

                      externally and just bootstrapping it from FileMaker; e.g.

                       

                      Perform AppleScript ["
                         run script alias ((path to scripts folder as text) & "test.scpt")
                      "]
                      

                       

                      then your maintenance headache will be copying the external script into

                      ~/Library/Scripts for each user, which may or may not be easier

                       

                      but for one liners (which this seemed to be originally), i think it's a matter

                      of preference; i like thinking about strings, so i keep it all within Perform

                      AppleScript

                       

                      another option is to debug in AppleScript Editor, then run a few text

                      replacements that prep the script for pasting into FMP; this can be automated,

                      and i will post a followup with solutions for AppleScript Editor and BBEdit

                      • 9. Re: Calculated AppleScript - escape character challenge
                        sporobolus

                        on 2013-06-04 10:05 bjbk wrote

                        Thanks for the help!  However, I have tried that and I get this error:

                          https://fmdev.filemaker.com/servlet/JiveServlet/showImage/2-117243-3643/ScreenShot2013-06-04at10.42.22AM.png  https://fmdev.filemaker.com/servlet/JiveServlet/downloadImage/2-117243-3643/450-225/ScreenShot2013-06-04at10.42.22AM.png

                        Here is the full code :

                         

                        it looks like there are multiple errors in your attempt; i reduced this to

                        something compilable in AppleScript Editor, then used the search & replace

                        approach from my response to Winfried; i have no way to test it:

                         

                        "tell application \"Safari\"¶
                           tell window 1¶
                             set properties of current tab to {URL:linkTOwebsite}¶
                             delay 1¶
                             tell document 1¶
                               do JavaScript \"function testFieldset() {var fieldsetExists = 
                        $('fieldset')[1]; if (fieldsetExists) {return \\\"true\\\";} else {return 
                        \\\"false\\\";}} testFieldset();\"¶
                               if result = \"true\" then¶
                                 display dialog \"You have a report in progress.  Please select the 
                        desired report.\" default button {\"OK\"}¶
                                 repeat until (do JavaScript \"function contentInH2() {var 
                        contentReturn = $('#content-apps h2').html(); return contentReturn} 
                        contentInH2();\") is \"Report\"¶
                                   delay 0.5¶
                                 end repeat¶
                               else¶
                                 do JavaScript \"document.forms['frmAddData']['txtNum'].value='\\\" & 
                        Number & \\\"';\"¶
                                 do JavaScript \"document.forms['frmAddData'].submit()\"¶
                               end if¶
                             end tell¶
                           end tell¶
                        end tell"
                        
                        

                         

                        • 10. Re: Calculated AppleScript - escape character challenge
                          sporobolus

                          i wrote:

                           

                           

                          another option is to debug in AppleScript Editor, then run a few text

                          replacements that prep the script for pasting into FMP; this can be automated,

                          and i will post a followup with solutions for AppleScript Editor and BBEdit

                           

                          and i thought a problem like this deserved an AppleScript solution

                           

                          attached is an AppleScript tool to convert AppleScript code (as runs in AppleScript Editor)

                          to escaped code suitable for pasting into the calculation editor in FileMaker's Perform AppleScript script step

                           

                          it works by replacing selected text in BBEdit or AppleScript Editor; this text then can be copied and pasted

                           

                          for BBEdit, put the .scpt file in ~/Library/Application Support/BBEdit/Text Filters/ (or /Library)

                          and run via Text > Apply Text Filter menu

                           

                          for AppleScript Editor, put the .scpt file in ~/Library/Scripts/Applications/AppleScript Editor/

                          and run via the system-wide Script Menu

                           

                          you can store the same script both places by using an alias

                           

                          i have tested this lightly, let me know if you find problems

                          • 11. Re: Calculated AppleScript - escape character challenge
                            sporobolus

                            on 2013-06-04 11:04 Winfried Huslik wrote

                            Better is to put the script (as it can be tested with Script Debugger) into a (global) field as is.

                             

                            occurs to me ask what advantage you see for this over "native AppleScript"?

                             

                            "calculated AppleScript" is generally for when you want to dynamically alter

                            the script, which the global approach does not facilitate; the case at hand is

                            not a good example of that, but is a good test case for escaping

                            • 12. Re: Calculated AppleScript - escape character challenge
                              bjbk

                              Thanks for all your efforts!  This is a fun one, eh?

                               

                              Here is the code that works in Applescript editor: [some of the field names have been changed]

                               

                              tell application "Filemaker Pro Advanced" // for running in Applescript editor

                                   set theNum to cellValue of cell "SourceTO::number"

                                   set linkTheForm to "https://foo.bar/apps/"

                              end tell

                               

                              tell application "Safari"

                                   tell window 1           set properties of current tab to {URL:linkTheForm}

                                   delay 1

                                        tell document 1

                                             do JavaScript "function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return \"true\";} else {return \"false\";}} testFieldset();"

                               

                                        if result = "true" then

                                             display dialog "You have a report in progress.  Please select the desired report." default button {"OK"}

                                        repeat until (do JavaScript "function contentInH2() {var contentReturn = $('#content-apps h2').html(); return contentReturn} contentInH2();") is "New Report"

                                        delay 0.5

                                        end repeat

                                        else

                                             do JavaScript "document.forms['frmAddToForm']['txtTheNum'].value='" & theNum & "';"

                                             do JavaScript "document.forms['frmAddToForm'].submit()"

                                        end if

                                        end tell

                                   end tell

                              end tell

                               

                              By the way, I wrote this in the AppleScript editor before porting over to Filemaker.

                               

                              Here is the cleaned up Filemaker calculated script that throws an AppleScript error:

                               

                              "set theNumber to cellValue of cell \"SouceTO::number\" as text¶

                              set linkTheForm to \"https://foo.bar/\"¶

                               

                              tell application \"Safari\"¶

                                        tell window 1¶

                               

                                                  set properties of current tab to {URL:linkTheForm}¶

                                                  delay 1¶

                               

                                                  tell document 1¶

                                                  do JavaScript \"function testFieldset() {var fieldsetExists = $('fieldset')[1]; if (fieldsetExists) {return \\\"true\\\";} else {return \\\"false\\\";}} testFieldset();\"¶

                               

                                                  if result = \"true\" then¶

                                                            display dialog \"You have a report in progress.  Please select the desired report.\" default button {\"OK\"}¶

                                                            repeat until (do JavaScript \"function contentInH2() {var contentReturn = $('#content-apps h2').html(); return contentReturn} contentInH2();\") is \"New Report\"¶

                                                                      delay 0.5¶

                                                            end repeat¶

                               

                                                  else¶

                                                            do JavaScript \"document.forms['frmAddToForm']['txtTheNum'].value='\" & theNumber & \"';¶

                                                            do JavaScript \"document.forms['frmAddToForm'].submit()\"¶

                                                  end if¶

                               

                                        end tell¶

                                        end tell¶

                              end tell"

                               

                              It took a lot of time to get the JavaScript right!  I'll keep working at it.

                              Thanks to everyone for you input on this!  We all have lots to do, so I appreciate your time.

                              Cheers!

                              • 13. Re: Calculated AppleScript - escape character challenge
                                sporobolus

                                on 2013-06-04 22:07 bjbk wrote

                                   do JavaScript \"document.forms['frmAddToForm']['txtTheNum'].value='\" & theNumber & \"';¶

                                 

                                the variable name here should be theNum

                                 

                                when i worked your earlier version, the part where theNum was set was missing

                                and you used "Number", which is a reserved word in AppleScript, so i assumed it

                                was to be quoted

                                • 14. Re: Calculated AppleScript - escape character challenge
                                  bjbk

                                  Sorry about that.  I changed the variables from the original code for proprietary reasons.  All the original code works fine.  It is only the "true" and "false" return in the JavaScript that is the tricky part.  I had a idea to separate the ApplesScript into further script steps to isolate the parts that do not depend on FM data and run that part in a Native AppleScript script step.

                                  Thanks to all and sorry for the confusion, Steve!

                                  Cheers

                                  1 2 Previous Next