why is safari responding to this?

so, i wrote a script that will automatically log into a website. but, first, it checks if they are using Google Chrome…if so, then it will open chrome, take you to the page, then log in. If the user doesn’t have chrome installed, it will open safari and do the same. now, the weird part is that when the script is run, it automatically opens safari. doesn’t do anything with it (assuming that chrome is installed), but it opens it. any suggestions as to what i might be doing wrong here?

set doesExist to false
try
	do shell script "osascript -e 'exists application \"Google Chrome\"'"
	set doesExist to true
end try
delay 1
if doesExist is true then
	tell application "Google Chrome"
		open location "https://www.somepage.com"
		tell active tab of window 1
			repeat while loading is true
				delay 0.1
			end repeat
			execute javascript "document.forms['Form1']['UsernameTextbox'].value = 'Username'"
			execute javascript "document.forms['Form1']['PasswordTextbox'].value = 'Password'"
			execute javascript "document.forms['Form1']['LoginButton'].click()"
		end tell
	end tell
else
	tell application "Safari"
		tell (make new document) to set URL to "https://www.somepage.com"
		activate
		delay 1
		repeat
			local pageState
			set pageState to do JavaScript "document.readyState" in document 1
			if pageState = "Complete" then exit repeat
			delay 0.2
		end repeat
		delay 1
		set doc to document "Login"
		do JavaScript "document.forms['Form1']['UsernameTextbox'].value = 'Username'" in doc
		do JavaScript "document.forms['Form1']['PasswordTextbox'].value = 'Password'" in doc
		tell application "System Events"
			keystroke return
		end tell
	end tell
end if

maybe your ‘doesExist’ code is wrong

replace “/Applications/TextEdit.app” with your App Name

set doesExist to false
tell application "Finder"
	if exists POSIX file "/Applications/TextEdit.app" then
		set doesExist to true
	else
		set doesExist to false
	end if
end tell

As axel99092 has stated, your doesExists variable stays as false, because your do shell script command raises an error, but your try block is not handling the error.
When I paste the osascript command into a terminal, it returns nothing but a new cursor on my system, so you need an on error block in your try block, as below.


try
	tell application "Finder" to get application file id "com.google.chrome"
	set chromeExists to true
on error
	set chromeExists to false
end try

if chromeExists then
	return "Chrome Installed" as text
else
	return "Chrome Not Installed" as text
end if

I don’t know if Chrome’s id is “com.google.chrome”, as I don’t have it installed myself, so you might want to check the correct bundle ID for Google’s Chrome web browser.
But you can check the posted code with another standard OS X application like TextEdit by changing the above code too.


tell application "Finder" to get application file id "com.apple.textedit"

Regards Mark

seems to have worked. thank you both!