AppleScript: Wait for Safari page to fully load











up vote
4
down vote

favorite
1












I'm using this script for Google Chrome to pause the script until the page is fully loaded:



tell application "Google Chrome"
repeat until (loading of front window's tab 2 is false)
1 + 1 --just an arbitary line
end repeat
loading of front window's tab 2
end tell


This don't seems to work with Safari, any equivalent?










share|improve this question




























    up vote
    4
    down vote

    favorite
    1












    I'm using this script for Google Chrome to pause the script until the page is fully loaded:



    tell application "Google Chrome"
    repeat until (loading of front window's tab 2 is false)
    1 + 1 --just an arbitary line
    end repeat
    loading of front window's tab 2
    end tell


    This don't seems to work with Safari, any equivalent?










    share|improve this question


























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      I'm using this script for Google Chrome to pause the script until the page is fully loaded:



      tell application "Google Chrome"
      repeat until (loading of front window's tab 2 is false)
      1 + 1 --just an arbitary line
      end repeat
      loading of front window's tab 2
      end tell


      This don't seems to work with Safari, any equivalent?










      share|improve this question















      I'm using this script for Google Chrome to pause the script until the page is fully loaded:



      tell application "Google Chrome"
      repeat until (loading of front window's tab 2 is false)
      1 + 1 --just an arbitary line
      end repeat
      loading of front window's tab 2
      end tell


      This don't seems to work with Safari, any equivalent?







      safari applescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 at 19:05









      Hara Sitaphal

      208114




      208114










      asked Nov 24 at 15:18









      Kevin

      830523




      830523






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          1. AppleScript source property



          You could try using Safari's source property, which is "" until the HTML code gets loaded into it (which can only be done once the page is loaded). Note, however, that this doesn't necessarily imply that the page has been rendered:



          tell application "Safari"
          .
          .
          repeat while document 1's source = ""
          delay 0.5
          end repeat
          .
          .
          end tell


          The source property is reset to "" even between page loads/reloads.



          2. Reload button UI element



          If you want to know that a page is loaded and rendered on screen, then a reliable method is to determine whether the button in the URL bar is a "reload" button (page loaded and rendered), or a "cancel" button (page still loading/rendering):



          tell application "System Events" to repeat until exists (buttons of ¬
          UI elements of groups of toolbar 1 of window 1 of ¬
          process "Safari" whose name = "Reload this page")

          delay 0.5
          end repeat


          3. JavaScript readyState property



          If you have Allow JavaScript from Apple Events ticked in the Develop menu, then you can access the readyState property of the document:



          tell application "Safari"
          .
          .
          tell document 1 to repeat
          do JavaScript "document.readyState"
          if the result = "complete" then exit repeat
          delay 0.5
          end repeat
          .
          .
          end tell


          The document.readyState JavaScript property returns one of five values:





          • uninitialized: Has not started loading yet


          • loading: Is loading


          • loaded: Has been loaded


          • interactive: Has loaded enough and the user can interact with it


          • complete: Fully loaded






          share|improve this answer






























            up vote
            1
            down vote













            If I really want to wait until the page is completely loaded, here is one way that I've found to be more reliable then other methods I've found for Safari:



            Example AppleScript code:



            tell application "Safari" to ¬
            make new document with properties ¬
            {URL:"https://apple.stackexchange.com/questions/343624/applescript-wait-for-page-fully-loaded"}

            tell application "System Events" to tell process "Safari" to ¬
            set reload_button to a reference to ¬
            (first button whose name is "Reload this page") of ¬
            UI element 1 of groups of toolbar 1 of window 1

            using terms from application "System Events"
            repeat until the accessibility description ¬
            of the reload_button ¬
            contains "Reload this page"
            delay 0.25 -- # Adjust value of delay as appropriate.
            end repeat
            end using terms from

            -- # Code to run after the page is completely loaded goes here:


            Adapted for my use from CJK's answer to AppleScript Help: Wait for Safari Page to load





            Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors.






            share|improve this answer





















            • Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
              – Kevin
              Nov 24 at 16:10











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "118"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f343624%2fapplescript-wait-for-safari-page-to-fully-load%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            1. AppleScript source property



            You could try using Safari's source property, which is "" until the HTML code gets loaded into it (which can only be done once the page is loaded). Note, however, that this doesn't necessarily imply that the page has been rendered:



            tell application "Safari"
            .
            .
            repeat while document 1's source = ""
            delay 0.5
            end repeat
            .
            .
            end tell


            The source property is reset to "" even between page loads/reloads.



            2. Reload button UI element



            If you want to know that a page is loaded and rendered on screen, then a reliable method is to determine whether the button in the URL bar is a "reload" button (page loaded and rendered), or a "cancel" button (page still loading/rendering):



            tell application "System Events" to repeat until exists (buttons of ¬
            UI elements of groups of toolbar 1 of window 1 of ¬
            process "Safari" whose name = "Reload this page")

            delay 0.5
            end repeat


            3. JavaScript readyState property



            If you have Allow JavaScript from Apple Events ticked in the Develop menu, then you can access the readyState property of the document:



            tell application "Safari"
            .
            .
            tell document 1 to repeat
            do JavaScript "document.readyState"
            if the result = "complete" then exit repeat
            delay 0.5
            end repeat
            .
            .
            end tell


            The document.readyState JavaScript property returns one of five values:





            • uninitialized: Has not started loading yet


            • loading: Is loading


            • loaded: Has been loaded


            • interactive: Has loaded enough and the user can interact with it


            • complete: Fully loaded






            share|improve this answer



























              up vote
              5
              down vote



              accepted










              1. AppleScript source property



              You could try using Safari's source property, which is "" until the HTML code gets loaded into it (which can only be done once the page is loaded). Note, however, that this doesn't necessarily imply that the page has been rendered:



              tell application "Safari"
              .
              .
              repeat while document 1's source = ""
              delay 0.5
              end repeat
              .
              .
              end tell


              The source property is reset to "" even between page loads/reloads.



              2. Reload button UI element



              If you want to know that a page is loaded and rendered on screen, then a reliable method is to determine whether the button in the URL bar is a "reload" button (page loaded and rendered), or a "cancel" button (page still loading/rendering):



              tell application "System Events" to repeat until exists (buttons of ¬
              UI elements of groups of toolbar 1 of window 1 of ¬
              process "Safari" whose name = "Reload this page")

              delay 0.5
              end repeat


              3. JavaScript readyState property



              If you have Allow JavaScript from Apple Events ticked in the Develop menu, then you can access the readyState property of the document:



              tell application "Safari"
              .
              .
              tell document 1 to repeat
              do JavaScript "document.readyState"
              if the result = "complete" then exit repeat
              delay 0.5
              end repeat
              .
              .
              end tell


              The document.readyState JavaScript property returns one of five values:





              • uninitialized: Has not started loading yet


              • loading: Is loading


              • loaded: Has been loaded


              • interactive: Has loaded enough and the user can interact with it


              • complete: Fully loaded






              share|improve this answer

























                up vote
                5
                down vote



                accepted







                up vote
                5
                down vote



                accepted






                1. AppleScript source property



                You could try using Safari's source property, which is "" until the HTML code gets loaded into it (which can only be done once the page is loaded). Note, however, that this doesn't necessarily imply that the page has been rendered:



                tell application "Safari"
                .
                .
                repeat while document 1's source = ""
                delay 0.5
                end repeat
                .
                .
                end tell


                The source property is reset to "" even between page loads/reloads.



                2. Reload button UI element



                If you want to know that a page is loaded and rendered on screen, then a reliable method is to determine whether the button in the URL bar is a "reload" button (page loaded and rendered), or a "cancel" button (page still loading/rendering):



                tell application "System Events" to repeat until exists (buttons of ¬
                UI elements of groups of toolbar 1 of window 1 of ¬
                process "Safari" whose name = "Reload this page")

                delay 0.5
                end repeat


                3. JavaScript readyState property



                If you have Allow JavaScript from Apple Events ticked in the Develop menu, then you can access the readyState property of the document:



                tell application "Safari"
                .
                .
                tell document 1 to repeat
                do JavaScript "document.readyState"
                if the result = "complete" then exit repeat
                delay 0.5
                end repeat
                .
                .
                end tell


                The document.readyState JavaScript property returns one of five values:





                • uninitialized: Has not started loading yet


                • loading: Is loading


                • loaded: Has been loaded


                • interactive: Has loaded enough and the user can interact with it


                • complete: Fully loaded






                share|improve this answer














                1. AppleScript source property



                You could try using Safari's source property, which is "" until the HTML code gets loaded into it (which can only be done once the page is loaded). Note, however, that this doesn't necessarily imply that the page has been rendered:



                tell application "Safari"
                .
                .
                repeat while document 1's source = ""
                delay 0.5
                end repeat
                .
                .
                end tell


                The source property is reset to "" even between page loads/reloads.



                2. Reload button UI element



                If you want to know that a page is loaded and rendered on screen, then a reliable method is to determine whether the button in the URL bar is a "reload" button (page loaded and rendered), or a "cancel" button (page still loading/rendering):



                tell application "System Events" to repeat until exists (buttons of ¬
                UI elements of groups of toolbar 1 of window 1 of ¬
                process "Safari" whose name = "Reload this page")

                delay 0.5
                end repeat


                3. JavaScript readyState property



                If you have Allow JavaScript from Apple Events ticked in the Develop menu, then you can access the readyState property of the document:



                tell application "Safari"
                .
                .
                tell document 1 to repeat
                do JavaScript "document.readyState"
                if the result = "complete" then exit repeat
                delay 0.5
                end repeat
                .
                .
                end tell


                The document.readyState JavaScript property returns one of five values:





                • uninitialized: Has not started loading yet


                • loading: Is loading


                • loaded: Has been loaded


                • interactive: Has loaded enough and the user can interact with it


                • complete: Fully loaded







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 24 at 16:07

























                answered Nov 24 at 16:01









                CJK

                2,328112




                2,328112
























                    up vote
                    1
                    down vote













                    If I really want to wait until the page is completely loaded, here is one way that I've found to be more reliable then other methods I've found for Safari:



                    Example AppleScript code:



                    tell application "Safari" to ¬
                    make new document with properties ¬
                    {URL:"https://apple.stackexchange.com/questions/343624/applescript-wait-for-page-fully-loaded"}

                    tell application "System Events" to tell process "Safari" to ¬
                    set reload_button to a reference to ¬
                    (first button whose name is "Reload this page") of ¬
                    UI element 1 of groups of toolbar 1 of window 1

                    using terms from application "System Events"
                    repeat until the accessibility description ¬
                    of the reload_button ¬
                    contains "Reload this page"
                    delay 0.25 -- # Adjust value of delay as appropriate.
                    end repeat
                    end using terms from

                    -- # Code to run after the page is completely loaded goes here:


                    Adapted for my use from CJK's answer to AppleScript Help: Wait for Safari Page to load





                    Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors.






                    share|improve this answer





















                    • Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                      – Kevin
                      Nov 24 at 16:10















                    up vote
                    1
                    down vote













                    If I really want to wait until the page is completely loaded, here is one way that I've found to be more reliable then other methods I've found for Safari:



                    Example AppleScript code:



                    tell application "Safari" to ¬
                    make new document with properties ¬
                    {URL:"https://apple.stackexchange.com/questions/343624/applescript-wait-for-page-fully-loaded"}

                    tell application "System Events" to tell process "Safari" to ¬
                    set reload_button to a reference to ¬
                    (first button whose name is "Reload this page") of ¬
                    UI element 1 of groups of toolbar 1 of window 1

                    using terms from application "System Events"
                    repeat until the accessibility description ¬
                    of the reload_button ¬
                    contains "Reload this page"
                    delay 0.25 -- # Adjust value of delay as appropriate.
                    end repeat
                    end using terms from

                    -- # Code to run after the page is completely loaded goes here:


                    Adapted for my use from CJK's answer to AppleScript Help: Wait for Safari Page to load





                    Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors.






                    share|improve this answer





















                    • Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                      – Kevin
                      Nov 24 at 16:10













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    If I really want to wait until the page is completely loaded, here is one way that I've found to be more reliable then other methods I've found for Safari:



                    Example AppleScript code:



                    tell application "Safari" to ¬
                    make new document with properties ¬
                    {URL:"https://apple.stackexchange.com/questions/343624/applescript-wait-for-page-fully-loaded"}

                    tell application "System Events" to tell process "Safari" to ¬
                    set reload_button to a reference to ¬
                    (first button whose name is "Reload this page") of ¬
                    UI element 1 of groups of toolbar 1 of window 1

                    using terms from application "System Events"
                    repeat until the accessibility description ¬
                    of the reload_button ¬
                    contains "Reload this page"
                    delay 0.25 -- # Adjust value of delay as appropriate.
                    end repeat
                    end using terms from

                    -- # Code to run after the page is completely loaded goes here:


                    Adapted for my use from CJK's answer to AppleScript Help: Wait for Safari Page to load





                    Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors.






                    share|improve this answer












                    If I really want to wait until the page is completely loaded, here is one way that I've found to be more reliable then other methods I've found for Safari:



                    Example AppleScript code:



                    tell application "Safari" to ¬
                    make new document with properties ¬
                    {URL:"https://apple.stackexchange.com/questions/343624/applescript-wait-for-page-fully-loaded"}

                    tell application "System Events" to tell process "Safari" to ¬
                    set reload_button to a reference to ¬
                    (first button whose name is "Reload this page") of ¬
                    UI element 1 of groups of toolbar 1 of window 1

                    using terms from application "System Events"
                    repeat until the accessibility description ¬
                    of the reload_button ¬
                    contains "Reload this page"
                    delay 0.25 -- # Adjust value of delay as appropriate.
                    end repeat
                    end using terms from

                    -- # Code to run after the page is completely loaded goes here:


                    Adapted for my use from CJK's answer to AppleScript Help: Wait for Safari Page to load





                    Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 at 15:59









                    user3439894

                    26.6k64060




                    26.6k64060












                    • Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                      – Kevin
                      Nov 24 at 16:10


















                    • Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                      – Kevin
                      Nov 24 at 16:10
















                    Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                    – Kevin
                    Nov 24 at 16:10




                    Thank you, the reload_button seems a great idea, but in my case I don't have the actual tab focused , so I used your ready state option "repeat 20 times tell application "Safari" do JavaScript "document.readyState" in tab 2 of window 1"
                    – Kevin
                    Nov 24 at 16:10


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Ask Different!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f343624%2fapplescript-wait-for-safari-page-to-fully-load%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ellipse (mathématiques)

                    Quarter-circle Tiles

                    Mont Emei