Asking Firefox to open an URL

This is my very first step in web crawling using Applescript … and as it often happens, it is a failure :


to goToWebPage(theWebPage)
	tell application "Firefox"
		activate
		set URL of document 1 to theWebPage
	end tell
end goToWebPage

goToWebPage("https://google.com")

The error message I get is

Hi,

I’m not a fan of third-party browsers, and therefore I don’t have Firefox installed to test the following script on it. But, it should work with Firefox too. Test yourself:


to goToWebPage(theWebPage)
	tell application "Safari" -- or, tell application "Firefox"
		«event WWW!OURL» theWebPage
		activate
	end tell
end goToWebPage


goToWebPage("https://google.com")
1 Like

Thanks KniazidisR for your feedback.

Your solutions “sort of works” for me. It opens a new Tab in Firefox but that tab does not go the specified location, it just stays on Firefox’s welcome page (it works fully however on Safari).

By the way, I’m not using a third-party browser just for the fun of it, but because Safari is unable to reach the URL (namely, https://gmx.com) I intend to make my final script work on in the end.

The reason is that Firefox doesn’t have the scripting interface and isn’t suitable even for GUI scripting. Its AppleScript dictionary is very rudimentary and contains only the Standard and Definitions Suites. However, you can open web-pages in it by using the open location command belonging to Standard Additions and, hence, it’s available globally. Just so happened that today I replied to a post raising a similar problem. Please, have a look. Although it concerns the usage of the clipboard its skeleton fits the purpose.

https://macscripter.net/viewtopic.php?id=48217

Hello. Again.

I found it very odd that my code from post #2 didn’t work for the OP with FireFox. Therefore, I took the trouble to install FireFox on my computer.

Result: the script perfectly opens the given URL. I ran the script several times to be sure. The “Welcome” page appeared only when I first opened FireFox. As it should be with a decent application.

I don’t know why the OP has a different answer. Perhaps the site server had some kind of glitch yesterday. Or, maybe, yesterday its trusted sertificat was expired and now it works again. No need keystrokes and GUI scripting. Code tested:


to goToWebPage(theWebPage)
	tell application "Firefox"
		«event WWW!OURL» theWebPage
		activate
	end tell
end goToWebPage


goToWebPage("https://gmx.com/")

GUI scripting is more justified because Firefox is wonky itself and clumsy responding to AppleScript in general. At least, this is what I see on my set-up. More often than not when activating Firefox clamps down on the URL I send loading only a default page. No sooner than after some time it begins responding to commands again which is not what the behaviour is in Safari. It’s completely unpredictable and I suspect this was the culprit of OP issues with this command which is what I experienced too when I ran it. And, BTW, raw codes is very unintuitive; the use is poorly documented so relying on GUI scripting is more friendly at this point. My code works 100/100.

An additional option FWIW. I don’t have Firefox installed but it works with Safari and I would expect it to work with Firefox.

set theBrowser to "/Applications/Safari.app/" -- change to path to Firefox
set theWebSite to "https://google.com"

do shell script "open -a " & quoted form of theBrowser & " " & quoted form of theWebSite

I have a confession to make here. I retried this morning with FireFox and got exactly the same result as you (“Welcome” page when first opened, but expected result the second time).
Morals of the story : You need to have FireFox opened before launching your script.

In my initial trials before posting this question, I expected the script to “do it all by itself” and always quitted Firefox before retrying.

Anyway, thanks for answering my silly newbie question.

Damn it. I couldn’t get my script to open the “Wecome” page like yours. You did not understand me. This page appeared to me only for the very, very first time after installation. With any other launches, it opens the site you need, successfully.

I can’t figure out why you get it every time you open Firefox. Check your Firefox settings. Or restore defaults.