AppleScript to Enter a Webpage Login Credentials

I wrote a script to open a web page and enter the login credentials but am stuck in getting the login credentials code to work.

The relevant part of the script is as follows:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Safari"

set myUserName to "abcdefgh"
set myPassword to "ijklmnopq"

set tabList to index of every window
set tabName to "Scotiabank"
repeat with i in tabList
	try
		tell window i
			set current tab to (first tab whose name contains tabName)
		end tell
	end try
end repeat

tell application "System Events" to tell application process "Safari"
	delay 2
	keystroke tab
	keystroke myUserName
	keystroke tab
	keystroke myPassword
	keystroke return
end tell
end tell

The script fails when executing the keystroke tab command and produces the following error message:

I would appreciate assistance in getting this to work as I am seriously stuck.

Thank you.

Add an activate command in the Safari tell

What’s happening is that Script Debugger is still the frontmost application as the GUI commands are being entered.

tell application "System Events" tell application process "Safari"
	activate
	delay 2

@estockly, thank you, that worked great!

I am unfortunately stuck again as it does not seem to be entering the username or password, any ideas?

Thanks.

@estockly , all good, I figured it out. I had to coerce the variables myUserName and myPassword as text and now all is good.

I am not sure why I had to do so but it worked!

I really do need to understand this stuff better.

Thanks.

I’d say you’re definitely on your way.

Plus you have the best tool for learning AppleScript (ScriptDebugger) and the best forum for getting AppleScript advice (right here).

You may also check out Macsriper.net. Lots of sample scripts there (and lots of the same people as here).

Welcome to the party!

Ed

@estockly, I appreciate your encouraging words though I do feel that I have a very long to go! The one thing that I do find “amusing” is people preferring / pushing automation via Automator, Shortcuts, etc. which are great for what they are but not as powerful (i.e. cannot do the heavy lifting) as AppleScript. Would appreciate your thoughts.

I know macscripter well as I am so on that sight.

Yes, to Script Debugger and this forum!

Thanks for all of your help, it is greatly appreciated.

Joel

I can’t help you re automator or shortcuts. I don’t use either.

I spent part of the weekend playing around with Keyboard Maestro. I it fairly powerful and can do some things easier – at least for me – that AppleScript but at the end of the day AppleScript is the tool of choice because it is more and can do it all, relative to the others.

For example, in KM, it has teh ability to execute AS code when KM lacks the functionality, same thing as Automator.

Comments / thoughts?

PS. My AS journey will continue!

Keyboard Maestro is a great platform for automating / orchestrating AppleScript.

One thing it lends itself to is the gradual pushing down of function into AppleScript. You can push bits of function down as you learn and / or have time to experiment.

Apologies for coming back.

I had the below script running a few days ago and now it is not co-operating. The problem is that it is failing to enter the login credentials. Would greatly appreciate your help in getting this fixed as I can see no reason why it all of a sudden stopped working, SO FUSTRATED!

-- Open Safari, Load and Log into the Josh Portal Webpage
tell application "Safari"
activate

-- Open the Josh Portal Webpage
open location "https://portal.josh.ai/"
delay 5
close (every tab of window 1 whose name is not equal to "Josh Portal")

-- Find the Josh Portal Wepage tab
set tabList to index of every window
set tabName to "Josh"
repeat with i in tabList
	try
		tell window i
			set current tab to (last tab whose name contains tabName)
		end tell
	end try
end repeat

-- Log into the Josh Portal Webpage
set myUserName to "e-mail"
set myPassword to "abcdefghi"

tell application "System Events" to tell application process "Safari"
	activate
	delay 2
	keystroke tab
	delay 2
	keystroke myUserName as text
	delay 2
	keystroke tab
	delay 2
	keystroke myPassword as text
	delay 2
	keystroke return
end tell

set windowPosition to {6, 29, 1434, 846}
tell its window "Josh Portal"
	set bounds to windowPosition
	set miniaturized to true
end tell
end tell

Thank you.

Hey Joel,

It looks to me like Safari’s Private Browsing mode interfere’s with System Events’ access to the page.

I have a script that works perfectly with a normal page, but when I adapted it to use Private Browsing it started failing…

-Chris

Hey Chris, I did as tep by step analysis and the problem has nothing to with the private browsing, it is burping at the keystroke commands, they are not executing at all even thought System Events is activated.

That said, I did add System Event to Security & Privacy settings per the KM forum as per the attached, any ideas?

Thanks,

Joel

I didn’t use keystroke in the original script. I directly referenced the fields in question.

I refactored the script and got it working reliably (so far) on my Mojave system.

This script does use keystroke, because the password field rejects text that is entered directly via the text field’s value property.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/12/14 15:57
# dMod: 2021/12/14 16:02
# Appl: Safari, System Events
# Task: Activate Safari and Log-In to Josh.io
# Libs: None
# Osax: None
# Tags: @ccstone, @SDForum, @Joel, @Applescript, @Script, @Safari, @System_Events
--------------------------------------------------------

set theURL to "https://portal.josh.ai"

tell application "Safari"
   if not running then
      run
      delay 2
   end if
   activate
end tell

tell application "System Events"
   set quit delay to 0
   tell application process "Safari"
      tell menu bar 1
         tell menu bar item "File"
            tell menu "File"
               tell menu item "New Private Window"
                  
                  repeat until exists of it
                     delay 0.25
                  end repeat
                  
                  perform action "AXPress"
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

tell application "Safari"
   set URL of front document to theURL
end tell

--------------------------------------------------------

tell application "System Events"
   tell application process "Safari"
      tell (first window whose subrole is "AXStandardWindow")
         tell splitter group 1
            tell tab group 1
               tell group 1
                  tell group 1
                     tell scroll area 1
                        tell UI element 1
                           -----------------------------------------
                           tell text field 1 -- User Name / Email Address
                              
                              repeat until exists of it
                                 delay 0.25
                              end repeat
                              
                              set its focused to true
                              set its value to "joel@iamnuts.com"
                           end tell
                           -----------------------------------------
                           tell text field 2 -- User Password
                              set its focused to true
                              keystroke "beastlyWebPage!" & return
                           end tell
                           -----------------------------------------
                        end tell
                     end tell
                  end tell
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

--------------------------------------------------------
1 Like

Chris, I appreciate the difference and thank you for the script.

I have not yet learnt how to reference fields directly but promise you that I will. In the meantime, is there a way to fix my script to work so that I understand it?

Thanks.

Apologies for coming back but am wondering whether anyone can please jump in and help me, see my above post of December 14 that contains the offending script.

Thank you.

I wanted to do a quick test of UI scripting sending multiple letters via keystroke, so I made this script to go to the URL field in Safari and enter some text. Something funky is going on with Script Debugger and/or UI scripting. When I run the following script, sometimes it will enter “test” correctly into the URL field in Safari and other times just the “t” is entered and the “est” gets appended to my applescript in Scriptdebugger. Can you reproduce this error? It would explain why your password works sometimes and not others… (I’m on newest silicon MBPro and latest version of Monterey, so ymmv)

set myUserName to "test"
activate application "Safari"
tell application "System Events"
    tell process "Safari"
	keystroke "l" using {command down} -- shifts focus to URL field
	delay 1
	keystroke myUserName
end tell
end tell

@vinnie-bob , I appreciate you working with me on this.

I did run your above script and I got an even different result.

The script consistently did as you described above:

  1. Entered the letter “t” in the Safari window; and

  2. Entered the letters “set” in the in the script window in Script Debugger.

Good news: we MAY know why my script is not working (and it is not my coding).

Bad news: we need someone from Script Debugger or who has seen this before to assist.

Thanks.

Hey Vince,

I suspect you have this setting turned ON in Script Debugger:

I do not have that setting active, and this script works reliably for me – even when Safari needs to launch and reopens previous windows/tabs.

set myUserName to "test my internet speed"

tell application "Safari"
   activate
   make new document
end tell

tell application "System Events"
   
   repeat until name of (first process whose frontmost is true) = "Safari"
      delay 0.2
   end repeat
   
   tell application process "Safari"
      keystroke "l" using {command down} -- shifts focus to URL field
      delay 0.15
      keystroke myUserName
   end tell
   
end tell

When you’re monkeying with UI-Scripting timing is often an issue.

-Chris

@ccstone , nice catch! I learnt something new.

So, with that lesson learned, what is problem with my script?

While I know you rewrote it, I have not gotten that far in my reading so I am wondering whether there is a solution with what I started / wrote.

Thx!

That’s helpful, Chris… I believe that is probably a default setting.
Joel, it might be worth a try to insert some delays right after your script is inserting username and password to give it a few extra seconds to complete and actually get all the keystrokes into the fields. I thought that various commands in applescript took whatever time they needed and the script essentially waited for the step to complete before moving on, but if the UI script to put multiple keystrokes into the field is getting usurped by Script Debugger wrenching away focus…I’ll have to rethink this.