从bash脚本中提示使用osascript时增加超时

从bash脚本中提示使用osascript时增加超时,bash,applescript,Bash,Applescript,我正在编写一些bash脚本,以便在我的环境中的JAMF中使用。我的脚本技能包括:用谷歌搜索其他人的脚本,剪下其中的片段并粘贴到我自己的脚本中。然后吃着糊状物。我在bash脚本中运行的一段AppleScript有点奇怪。原文如下: #!/bin/sh # Remove pre-existing settings rm -rfv /var/db/scrubbed/hostName rm -rfv /var/db/scrubbed/imageTech rm -rfv /var/db/scrubbe

我正在编写一些bash脚本,以便在我的环境中的JAMF中使用。我的脚本技能包括:用谷歌搜索其他人的脚本,剪下其中的片段并粘贴到我自己的脚本中。然后吃着糊状物。我在bash脚本中运行的一段AppleScript有点奇怪。原文如下:

#!/bin/sh

# Remove pre-existing settings

rm -rfv /var/db/scrubbed/hostName
rm -rfv /var/db/scrubbed/imageTech
rm -rfv /var/db/scrubbed/adBinding

# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
列出的代码工作正常。按预期弹出我的对话框。不幸的是,盒子在60秒后超时。这是成像过程的一部分,因此如果技术人员离开几分钟,脚本将继续,主机名将无法设置。通过研究,我在AppleScript中找到了超时为X的
命令。当我将第一段代码更新为以下内容时:

# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" with timeout of 86400 to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"
如果有帮助的话,它将在实际的apple脚本应用程序中运行

tell application "System Events"
    with timeout of 86400 seconds
        display dialog "Please enter the hostname of the new computer" default answer "" with title "Hostname" buttons {"Ok"} default button 1
    end timeout
end tell

我是一个磨砂工!快来帮忙

脚本中需要换行符,就像在常规AppleScript中一样。您还缺少
结束超时
结束提示
,以及
单位

hostName="$(/usr/bin/osascript -e 'tell application "System Events" 
    with timeout of 86400 seconds
        display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1
    end timeout
end tell' -e 'text returned of result')"

如果您希望实现的只是获取计算机的实际主机名…这一行AppleScript代码将为您实现这一点

set hostName to host name of (system info)
否则

首先,显示对话框命令不由系统事件处理。
显示对话框
命令是标准的添加命令

需要指出的另一点是,在使用带有timeout的
子句时,脚本将在子句中设置的指定时间之后继续运行。这将使您回到最初的问题,即脚本在超时后继续运行,而无需用户输入

显示对话框
命令有一个
之后放弃选项,允许您设置对话框自动关闭并继续之前等待的秒数。然后,您可以添加一个
if…then…
子句,以在
显示对话框在指定的时间后放弃时停止脚本

下面是一个可能更适合您需要的AppleScript示例。我添加了一个“取消”按钮,用户可以选择停止脚本。我还使用了10秒来进行测试,而不是86400秒…。很容易编辑

set enterHostName to display dialog ¬
    "Please enter the hostname of the new computer" default answer "" with title ¬
    "Hostname" buttons {"Cancel", "Ok"} default button 2 giving up after 10

if gave up of enterHostName then
    return
else
    set hostName to text returned of enterHostName
end if

同样,代码只是一个示例。您必须调整它才能在终端内正常工作。或者,您可以将代码另存为AppleScript文件,并使用终端中的
osascript
命令运行AppleScript文件。

将第一个代码块更新为该文件时会发生什么情况?从代码段的这一横截面来看,您主要是在
bash
中编写脚本,但是调用
osascript
来获取用户输入。如果有必要了解,您可以从
bash
请求用户输入,例如
printf“%s:”“输入新计算机的主机名”;读取主机名
。然后,您的用户条目将存储在变量
hostname
(使用
$hostname
访问)。如果您想包括一些验证,那么类似于:
unset hostname;re=“^[:alnum:][](?[:alnum:][])*$”;而[[!“$hostname”=~$re]];执行printf“%s:”“输入新主机名”;读取主机名;完成