Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppleScript - Help with Safari

Tags:

applescript

I've been appointed the task of creating an applicaton that must

  1. Display a dialog saying "The webpage has finished loading." when a webpage finishes loading

  2. Determine how many items are currently in the process of downloading

For number 1, I've tried doing if the URL of the front document is "http://applescript.weebly.com" then, but the then part always runs even if the webpage doesn't load!

For number 2, I've tried this...

tell application "Safari" to get every item of window "Downloads"

But this returns references to every item, even the ones that havae already been downloaded!

Conclusion: I need help. :S

like image 458
AppleScript Avatar asked Sep 04 '26 16:09

AppleScript


1 Answers

Display a dialog saying "The webpage has finished loading." when a webpage finishes loading.

There is no way to do this with just pure AppleScript. However, you can use a combination of AppleScript and javascript...

tell application "Safari"
    repeat until (do JavaScript "document.readyState" in document 1) is "complete"
    end repeat
    display dialog "The webpage has finished loading."
end tell

WARNING: If the webpage doesn't load for some reason, the script will be stuck forever in an infinite repeat loop.


Determine how many items are currently in the process of downloading.

When you download files, they are temporarily given the name extension download, so AppleScript can tell the Finder to get the files with that extension and create an alert/dialog:

set the download_count to 0
set the download_folder to (path to downloads folder) as alias --or wherever the items are being downloaded
tell application "Finder" to set the download_count to the count of (every item of the download_folder whose name extension is "download")
if the download_count is 0 then
    display dialog "There are currently no downloads in progress."
else if the download_count is 1 then
    display dialog "There is currently one download in progress."
else
    display dialog "There are currently " & the download_count & " downloads in progress."
end if


P.S. Thanks for honoring my web site!
like image 110
fireshadow52 Avatar answered Sep 07 '26 09:09

fireshadow52



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!