使用 AppleScript 创建用户名和密码输入框

Make Username and Password input box with AppleScript

我想在 AppleScript 中制作一个看起来完全像这样的对话框输入框:

锁左上角没有图片除外

此外,我需要能够保存两个输入。

我知道我可以使用 tell application "System Events" to display dialog "blah blah" default answer "" end tell,但我找不到方法来拥有多个带标签的字段。

从 OSX 10.10.

开始,AppleScript 本身不提供此功能

要查看支持哪些 GUI 操作,请查看标准添加词典的 User Interaction 套件(StandardAdditions.def,可从 Script Editor.app 通过 File > Open Dictionary... > StandardAdditions.osax).

最接近的近似值是单个输入字段对话框 - 仅提示输入密码 - 如下(您的限制已经在问题中指出,但只是为了说明如何提示输入 密码 以及如何使用 自定义按钮 ):

display dialog ¬
    "Installer is ..." default answer ¬
    "" buttons {"Cancel", "Install Software"} ¬
    default button 2 ¬
    with hidden answer

要得到你想要的,你需要一个第三方库,例如 Pashua

以下是您将如何在 Pashua 中定义请求的对话框并处理返回值:

# Define the dialog using a textual definition similar to a properties file.
set dlgDef to "
# Window title (if you don't set one explicitly (even if empty), it'll be 'Pashua')
*.title = 

# Add the hint (static text).
st.type = text
st.text = Installer is trying to install new software. Type your password to allow this.

# Add the username field.
tfn.type = textfield
tfn.label = Name:

# Add the password field.
tfp.type = password
tfp.label = Password:

# Add the buttons.
cb.type = cancelbutton
db.type = defaultbutton
db.label = Install Software
"

# Show the dialog. 
# Return value is a record whose keys are the element names from the dialog
# definition (e.g., "tfn" for the  usernam text field) and whose
# values are the values entered (for input fields) or 
# whether a given button was clicked or not ("1" or "0")
set theResult to showDialog(dlgDef, "")

# Process the returned values.
if cb of theResult is "1" then
    display alert "User canceled the dialog."
else
    display alert "name=[" & tfn of theResult & "]; password=[" & tfp of theResult & "]"
end if

对话框将如下所示:


Pashua 设置概述

注意:这是一个简化的概述;有关完整说明,请参阅下载的光盘映像中的 Read me.htmlDocumentation.html

  • http://www.bluem.net/en/mac/pashua/
  • 下载并安装光盘镜像
  • Pashua.app 放在 /Applications~/Applications 中,或者 - 在紧要关头 - 与调用脚本放在同一文件夹中。
    • Pashua.app 是呈现对话框的应用程序(当对话框打开时,菜单栏显示 Pashua)。
  • Examples/AppleScript/Pashua.scpt 中的绑定代码(2 个短处理程序,showDialoggetPashuaPath)复制到您的脚本中。
    • 注意:由于在撰写本文时存在错误,您必须对处理程序 getPashuaPath 进行小的更正:将行 return (path to applications folder from system domain as text) & "Pashua.app" 替换为 return (path to applications folder from system domain as text) & "Pashua.app:"
    • 或者,直接从 GitHub 存储库获取最新的绑定代码:https://github.com/BlueM/Pashua-Binding-AppleScript