Applescript for unlocked 1Password Mini - repeat until?

ui_control
applescript

#1

Hello to the applescript experts.

I have made my first applescript. If the “1Password Mini” is unlocked, it works perfectly.

If however “1Password Mini” is locked, I get it not to interrupt the Applescript as long as I have this with my password unlocked.

Does somebody has any idea

Sorry for my english … “Google Translate is my best friend :wink:

The Script:

tell application “System Events” to tell process "1Password mini"
open location "x-onepassword-helper://search/icloud"
repeat until (sheet 1 of “x-onepassword-helper://search/icloud”) exists
end repeat
delay 0.5
keystroke “c” using {command down, shift down}
end tell

delay 0.5

tell application “App Store” to activate
tell application "System Events"
delay 0.5
keystroke “v” using command down
delay 0.5
keystroke "return"
end tell


#2

Try this instead. It checks if 1P is locked and if not, it copies the first password in the search results to the clipboard, switches to the App Store, pastes the password, then sets the clipboard back to whatever it was before.

You will need to set the searchTerm value in the first line to what you want to search for in 1Password. You should not have to change any other code beyond that.

Hope this helps!

set searchTerm to "icloud"

open location "x-onepassword-helper://search/" & searchTerm
delay 0.2
tell application "System Events"
  set preClip to the clipboard as text
  set thePass to ""
  tell its UI element "2BUA8C4S2C.com.agilebits.onepassword4-helper"
    set isLocked to ((text field "Master Password" of window 1) exists)
    if not isLocked then
      set frontmost to true
      keystroke "c" using {command down, shift down}
      delay 0.5
      set thePass to the clipboard as text
    end if
  end tell

  if not isLocked then
    tell application "App Store" to activate
    delay 0.5
    keystroke thePass
    key code 36
    set the clipboard to preClip
  end if
end tell

#3

Hello @Onan thanks for the quick reply and the applescript :+1:

The applescript did not work. I had to make 2 changes until it ran with the unlocked 1Password Mini. I have highlighted these in the video.

The problem is still in the locked mode. The applescript remains after unlocking. Do you have an idea?


#4

“Master Passwort” is a typo when you copied the code. You can see in my post above that it should be “Master Password”

“2BUA8C4S2C.com.agilebits.onepassword4-helper” is the proper name for the 1Password Mini menu element. You should not change it.

Once you fix the “Passwort” typo, it should detect when 1P is locked.


#5

Many thanks for your response. Unfortunately it does not work if I copy it 1: 1.
Here is a video.


#6

I think the problem was due to language differences. Here is a version of the script that should work under any language. Now it checks if the 1PMini window has a secure text field (for your password). If so, it is considered locked.

set searchTerm to "icloud"

tell application "System Events"
  set preClip to the clipboard as text
  set elements to every UI element whose creator type is "1Ph*"
  if elements is {} then
    log "1Password Mini is not running"
    return
  end if
  set mini to (first item of elements)
  log short name of mini & " is running as " & name of mini
  open location "x-onepassword-helper://search/" & searchTerm
  delay 0.2
  tell its UI element (name of mini)
    set isLocked to (subrole of text field 1 of window 1 is "AXSecureTextField")
    if isLocked then
      log "1Password Mini is locked"
      return
    else
      set frontmost to true
      keystroke "c" using {command down, shift down}
      delay 0.5
      set thePass to the clipboard as text
      tell application "App Store" to activate
      delay 0.5
      keystroke thePass
      key code 36
      set the clipboard to preClip
    end if
  end tell
end tell

#7

Many many thanks for your work to help me :+1:

If 1Password mini is already unlocked, your macro will work.
But if 1PWM is locked and I entered the password, the script remains.

Here is a short video.

Do you have an idea?


#8

Previous version assumed 1P was going to already be unlocked. If it was locked, it just finished. The 1PMini window showing was from the open location command, which is a background job that runs the search and is unrelated to this script.

Here is an updated script that checks if 1P is locked and if so, waits 10 seconds for you to enter your password. Once it has been unlocked, it copies the search result as desired.

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

on run
  set searchTerm to "icloud"

  tell application "System Events"
    set preClip to (the clipboard) as text
    set elements to every UI element whose creator type is "1Ph*"
    if elements is {} then
      log "1Password Mini is not running"
      return false
    end if
    set mini to (first item of elements)
    set miniName to short name of mini
    log miniName & " is running as " & name of mini
    set isLocked to my isOPMiniLocked(miniName)
    open location "x-onepassword-helper://search/" & searchTerm
    if isLocked then
      if not (my waitForUnlock(miniName, 10)) then
        return false
      end if
    end if
    tell its UI element (name of mini)
      set frontmost to true
      set i to 0
      repeat
        try
          if (focused of text field 1 of window 1) is true then
            tell text field 1 of window 1
              set value to searchTerm
              perform action "AXConfirm"
            end tell
            delay 1.0
            keystroke "c" using {command down, shift down}
            delay 0.5
            set thePass to (the clipboard) as text
            exit repeat
          end if
        end try
        delay 0.1
        set i to i + 1
        if i > 100 then
          return false
        end if
      end repeat
  
      tell application "App Store" to activate
      delay 0.5
      keystroke thePass
      key code 36
      set the clipboard to preClip
    end tell
  end tell
end run

on isOPMiniLocked(miniName)
  tell application "System Events"
    tell application process miniName
      perform action "AXPress" of menu bar item 1 of menu bar 1
      set isLocked to (subrole of text field 1 of window 1 is "AXSecureTextField")
      delay 0.1
      key code 53 -- Escape key to close 1PMini window
      if isLocked then
        log "1Password Mini is locked"
        return true
      end if
      log "1Password Mini is unlocked"
      return false
    end tell
  end tell
end isOPMiniLocked

on waitForUnlock(pProcessName, pMaxTimeSec)
  local startTime, elapTime, errMsg
  set startTime to current application's NSDate's |date|()
  log startTime
  tell application "System Events"
    repeat
      set elapTime to (-(round ((startTime's timeIntervalSinceNow()) * 100)) / 100.0)
      if (elapTime > pMaxTimeSec) then
        set errMsg to "Max Time of " & pMaxTimeSec & " seconds exceeded waiting for " & pProcessName
        log errMsg
        return false
      end if
      try
        set roleMode to (subrole of text field 1 of window 1) of (application process pProcessName)
        if roleMode is "AXSearchField" then
          return true
        end if
      end try
      delay 0.1
    end repeat
  end tell
end waitForUnlock

#9

Thousand thanks for your help :+1: The script works now. Since I’m an absolute beginner with Applescript, what do I have to do to expand the 10 seconds to 15 seconds? Longer passwords would be better.

I would like to introduce the applescript in a tutorial for KM macros. Is that okay for you? Of course, I refer to your post as a source.
My YouTube channel http://d.pr/lxpDlP is not commercial or monetarized. I would like to make KM more popular for Germany.

Thanks again for your work.

My english… "Google Translate is my best friend :wink:


#10

To increase the wait time, change the number 10 (around line 21)

  if not (my waitForUnlock(miniName, 10)) then

to 15 like this

  if not (my waitForUnlock(miniName, 15)) then

#11

Thanks for sharing your scripts.
It is best if you put them in a code block:

###How to Put Script in a Forum Code Block
Just insert your script between the lines with the triple backquotes:

 ```applescript
 -- Your Script Here

If your script is another language, like JavaScript, then use the keyword in lower case for that language, like:
`javascript`

Here is a macro that will paste the script on the clipboard into the forum in the proper format:
####[MACRO: KM Forum -- Paste Script Block](https://forum.keyboardmaestro.com/t/paste-script-block-in-km-forum/4047)

#12

Thanks! I was trying to do that earlier but I think I was using 2 or 4 backticks.


#13

No problem.
Just so you, and others, know, this format (triple backquotes) is pretty much a Markdown standard.


#14

Hello @Onan & @JMichaelTX I , thank you for your help. Unfortunately, the applescript didn’t work with the Touch ID on the new MacBook Pro with Touch Bar.

Since I’m a newcomer to applescripts, I kept my macro simple and solved the problem with unlocking the 1Password Mini’s with screenshots. Therefore I am not only dependent on seconds to enter the master password.

I have made a tutorial in german. Maybe it also helps others to work with it :blush:

07)iCloud.kmmacros (199,4 KB)


Input Password
#15

The applescript was specifically calling elements in the 1PMini menu in the OSX menu bar. If you were calling the floating 1PMini window or using the TouchBar (which is like an extension to the display) then the script would not work.

Interesting way to do the macro. Glad you found a solution that works for you.