Open Web Page & Pause Until Load Complete [Sub-Macro]

This subject was much discussed in this topic:
Wait only until the very moment that Chrome finishes loading?

I doubt there is a completely fool-proof way of doing this, short of having a excessive manual pause time.
So, here is my attempt to provide a reusable Sub-Macro that will open a URL and wait until it has completed loading. You can call this Sub-Macro from any Macro using the Execute a Macro action (KM Wiki).

Please let me know if you have any issues or suggestions for improvement concerning this macro.

Below I have also uploaded a Macro to test this Sub-Macro.


UPDATED: 2018-08-06 13:21 GMT-5
    • Fix broken link in Download link

MACRO:   Open Web Page & Pause Until Load Complete [Sub-Macro]

~~~ VER: 2.0    2017-11-15 ~~~

DOWNLOAD:

Open Web Page & Pause Until Load Complete [Sub-Macro].kmmacros (90 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


You may call this sub-macro with a parameter like this:
<OpenWhere>,<URL>

where
  <OpenWhere> is one of the following:
      "win" -- open the URL in a new window
      "tab" -- open the URL in a new tab

  <URL> -- URL to be opened

For example:
win,http://www.apple.com

OR, just

<URL>
  and the URL will be opened in the existing window/tab.

If no Parameter is provided, then the Sub-Macro will CANCEL itself and the calling macro.

ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Open Safari or Chrome to URL, & Wait Until It is Loaded
    .
    • It should NOT be directly triggered, but called from another Macro using the Execute Macro Action.
    • It uses the new KM8 FrontMost Browser Actions, so it should work with both Safari and Chrome.

REQUIRES:

  1. KM 8.0.4+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

NOTICE: This macro/script is just an Example

  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

HOW TO USE:

  1. Call this SUB-MACRO by using an Execute Macro Action in another Macro
  2. In the Execute Macro, set the Parameter as indicated in the next Comment Action

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .

TAGS: @Web @Safari @Chrome @PauseUntil @SubMacro

USER SETTINGS:

  • There are NO Actions or settings in this Macro that are intended to be changed by the user.
  • Of course, like any macro, you can customize it as you like.
  • However, be careful before making any non-cosmetic changes
    • The process/actions used were developed after considerable research and testing.
    • You could break this macro if you are not careful.

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

5 Likes

Here's the macro to test the sub-macro.
Change the Parameter of the Execute Macro to test different URLs.


MACRO:   Web TEST Sub-Macro Pause Until Load Complete


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/8/e/8e3dace54c2d129afcecae91fc521a7e2c9b734c.kmmacros">Web TEST Sub-Macro Pause Until Load Complete.kmmacros</a> (36 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

![image|516x1207](upload://pGqLJjNO8HNxf9kPyoPa3m8RccO.jpg)
3 Likes

Here is a simpler script for Google Chrome, that may be almost as good as the above.
It tends to wait about 1 sec less, so it may be a bit short, depending on your needs.
OTOH, the above script may just be taking longer than is necessary. Hard to say. :wink:

This uses the "loading" property of a Chrome Tab. Unfortunately, there is no equivalent (that I have found) for Safari.

MACRO:   TEST Wait for Chrome Page to Fully Load Using Simple Script


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/e/8/e8d230133d96a5899f306b84cd03450b3d2d730d.kmmacros">TEST Wait for Chrome Page to Fully Load Using Simple Script.kmmacros</a> (3.9 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---



<img src="/uploads/default/original/3X/6/1/6105eef875ec95b0d5136e53099e0169dac428cd.png" width="546" height="733">

---

### AppleScript to Pause Until Chrome Page is Loaded

```applescript
property delayInterval : 0.1
property maxWaitTime : 30.0

set elapTime to 0.0

tell application "Google Chrome"
  repeat until (loading of active tab of front window is false)
    delay delayInterval
    set elapTime to elapTime + delayInterval
    if (elapTime > maxWaitTime) then exit repeat
  end repeat
  
  display notification "Page is loaded in " & elapTime & " sec"
end tell

```

Hey there JMichaelTX. I have been using KM for a while and have a super complicated arrangement on my work Mac here and often have to run a ton of scripts to help keep up with the hustle and bustle of incoming applicants here and think i may have a k.i.s.s. i suppose way to do this here as i have constant pop-ups and window changes and modifications always needed here and i think its as simple as doing:

53 PM

it seriously sped up my lengthy laggy caveman automations by great lengths and only waits until the webpage or atleast until the actions i needed done on it complete then closes the window.

If that is working for your then that's great!

However, the Pause Until will always immediately complete since the above Wait for Google... will always return an OK, even when the page has not actually completed loading.

One shortcut I have found is to use JavaScript in Browser to test for existence of a specific web page HTML element that you must have loaded.

What’s happened to your first post here? :thinking:

Looks like the latest Forum software update has changed some of the markdown codes. I think I've fixed things now, so let me know if you find any issues.

1 Like

The link in the first post doesn't work for me... any fix?

Thank you very much!

An update of the Forum software (Discourse) struck again!

Thank you!!

There is a simple way to check if a Chrome page has finished loading:

tell application "Google Chrome" to tell the second tab of its first window to reload -- Reloads the desired tab, You can set it to "active" tab
delay 2 -- this line can be removed if You wish to start checking instantly

tell application "Google Chrome"
	repeat until (loading of tab 2 of window 1 is false) -- checks if the desired tab has finished loading, can be replaced with "active" tab
		1 + 1
		delay 0.5
	end repeat
	set status to loading of tab 2 of window 1
end tell

Sadly, it doesn't work with Safari, still keep looking for a way to perform this action in Safari