7 Replies Latest reply on Apr 27, 2015 2:23 AM by PieroF

    Testing URL validity



      Testing URL validity



      I’d like to know if there is a way to test the validity of a URL path without using Open URL.


      I know that if I use Open URL I get a return code (Get(Last Err)) I can test; but if the file does exist this is immediately opened.

      I want to write a script browsing all records in a selection and returning the subset of them that have invalid URL; but without opening the valid ones…


      Is it possible ?





        • 1. Re: Testing URL validity

          I saw that you had an earlier post where you asked about an AppleScript. So I'll tell you an AppleScript method, which uses a Unix command which can tell whether a URL exists. It can do all kinds of things (many of which I don't even understand), but this one seems the fastest. 

          It just checks for the "Header" (which is not very long). This is an AppleScript line; this is this post:

          do shell script "curl -I http://forums.filemaker.com/posts/aac9f6f962"

          "I" is a capital, like in Italy, not in 'doll"

          The AppleScript command will get an error if the nothing is there. So you need to use a "try". It seems easiest to use the FileMaker method (since all you want is to know whether it's there or not; if you want more info, such as what the AppleScript error is, then you'd use tests then instead).

          Set Error Capture [On]
          Perform AppleScript [ do shell script "curl -I 'http://forums.filemaker.com/posts/aac9f6f962'" ]
          If [ Get ( LastError ) = 0 ]
          # Set a field to 1
          # Set a field to 0
          End If

          The AS could be written as "Native", as similar to:

          set url_path to cell "URL" of current record
          do shell script "curl -I " & url_path

          Or, as Calculation:

          "set url_path to cell \"URL\" of current record¶
          do shell script \"curl -I \" & url_path"

          P.S. The field "URL" needs to be on the layout when the above runs ( I think ).

          • 2. Re: Testing URL validity

            Thanks Fenton!

            Using Applescript seems a very good idea, even though I tried to keep FM script as simple and native as possible... anyway this seems to be the only way. So thanks again for the detailed answer. I'll let you know the result



            • 3. Re: Testing URL validity

              OK. Based on your hints I wrote my FM script "URL Exists":

              Set Error Capture [On]
              Perform AppleScript ["do shell script \"curl -I '" & Get(ScriptParameter) & "'\""]
              Exit Script [Result: Get(LastError) 
              ≠ 0 // 0=OK - 1=Error]

              So I can call it:
              Perform Script ["URL Exists"; Parameter: <my URL field>]
              and test its results!

              Even better, I found out it works both for files and folders, and this is important for me.
              Thanks again Fenton for your big help!




              • 4. Re: Testing URL validity

                Yes, using a Get (ScriptParameter) is simpler, and very good for running one via a button script. I tried similar with a Variable, and it worked well, and would be useful if you were doing a Loop thru a found set. I set the entire "shell script" into a Variable (so I could see what it looked like first; in my test file:

                Set Variable [ $shell; "do shell script \"curl -I '" & URL_Test::URL & "'\"" ]
                Perform AppleScript [ $shell ]

                • 5. Re: Testing URL validity

                  Yes. Probably since this time I'm going to to fit it into a loop I'd better avoid using a FM script just for a simple test... The fact is in general I prefer to hide reusable "complex" code into subroutines. But running time might suffer.

                  • 6. Re: Testing URL validity

                    A follow up.

                    Unfortunately while testing and using the above solution I ran into a problem: I got a number of return codes = 2, or 6, or -2740, -2741 while testing apparently valid URL (they do exist in the file system and Open URL can open them without any problem).

                    After some testing I realized that the problem is the presence of single quotes ('), or double quotes (") in the path.
                    I tried to escape them in the Perform AppleScript command with mixed results: I can easily escape the double quote, but I could do nothing with single quote. And this is bad because single quotes are often used in Italian names and so also in many filenames in my system.

                    My partial "solution":
                    Set Variable [$path; Value:Substitute($path;["'"; "\\\'"];["\"";"\\\""])] // note: here [" ' "; "\\\' "];[" \" ";"\\\" "] added blanks for readability...
                    Perform AppleScript ["do shell script \"curl -I '" & $path & "'\""]

                    This works fine for existing paths without quotes and with double quotes; it does not with single quotes (error -2741)
                    I tried also with  [" ' "; "\' "], [" ' "; "\\' "], [" ' "; "\\\' "] but whatever I get in $path (', ', \' respectively) is misinterpreted by curl...

                    Any suggestion ?

                    Thanks a lot 






                    • 7. Re: Testing URL validity

                      Found the solution myself:
                      Set Variable [$path; Value:Substitute($path;["'"; "'\\\''"];["\"";"\\\""])] // note: here [" ' "; "' \\\' ' "];[" \" ";"\\\" "] added blanks for readability...

                      In other words: in bash quote backslash quote quote (i. single quote) is the correct escape for single quote (different from double quote where the correct escape is backslash double-quote)
                      Now I don't get  the 2, 6, -2740 or -2741 codes any more... but just 0 as supposed!