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

时间:2015-02-27 02:33:58

标签: passwords applescript username inputbox

我想在AppleScript中创建一个与此类似的对话框输入框:

除了没有锁定左上方的图片。

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

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

1 个答案:

答案 0 :(得分:3)

本机地,从OSX 10.10开始,AppleScript不提供此功能

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

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

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

对话框如下所示:

enter image description here


设置Pashua概述

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

  • http://www.bluem.net/en/mac/pashua/
  • 下载并装入光盘映像
  • Pashua.app放在/Applications~/Applications或 - 在紧要关头 - 放在与调用脚本相同的文件夹中。
    • Pashua.app是呈现对话框的应用程序(对话框启动时,菜单栏显示Pashua)。
  • showDialog中的绑定代码(2个短处理程序,getPashuaPathExamples/AppleScript/Pashua.scpt)复制到您的脚本中。
    • 注意:由于撰写本文时存在错误,您必须对处理程序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