Auto login with safari

Hi,

Very new with apple scripting, I need your help to learn apple scripting and automate some task :
My goal is to auto login into some account webpage and download some documents.
With the help of IA, I understood the base of the code :

tell application "Safari"
    activate  
    open location "https://www.your-website.com"
end tell

tell application "Safari"
    activate  
    delay 2 -- wait  
    tell front document  
        do JavaScript "document.getElementById('user-id').value='your-id';"
        do JavaScript "document.getElementById('password').value='your-password';"
        do JavaScript "document.forms[0].submit();" 
    end tell  
end tell


My question : Is there a way to use the MacOS password application of safari autofill, in order not to write my password into the script ?
MacOS version Sequoia,

Thanks for any help

I find a way to autofill the connexion form with the keystroke, here an example if it can help someone else :

tell application "System Events"
tell process "Safari"
key code 125 -- code for down_arrow
delay 1
key code 36 -- code for return
delay 3
key code 36

Just for learning purpose, is there another way to fill connexion form with registred password ?

I’m not aware on how to access Safari passwords from AppleScript, HOWEVER: you can access passwords stored in Keychain. Open Keychain Access.app and manually add new password item.

In this case I named my keychain item “password test”, so I can access it with:

set mPassword to (do shell script "security find-generic-password -l \"password test\" -w")

You’ll need to enter your user password to access it, but you can set it to “always allow”.

Is there a way to use the MacOS password application of safari autofill, in order not to write my password into the script ?

I’ve done something like this with 1Password 6, when the UI is still using a native macOS app. Passwords app is inspectable, so it looks to be doable. You will need to prompt the user to unlock via password or touch ID first though. I accomplished that with a recognizable chime and then go on a loop to wait for the user to unlock.

As mentioned, using keystrokes works too when the iCloud keychain sheet is present.