【问题标题】:Display dialog if a word isn't on a list applescript如果单词不在列表applescript中,则显示对话框
【发布时间】:2014-10-17 16:32:45
【问题描述】:

我正在编写一个可以为我打开和关闭应用程序的脚本。基本上,我输入一个应用程序名称,然后它会打开应用程序。唯一的事情是,如果未找到该应用程序,它会引发错误并退出。我想让脚本只显示一个对话框,上面写着“找不到应用程序”。

这是我目前所拥有的:

if userInput contains "Activate " then set {TID, text item delimiters} to {text item delimiters, {"Activate "}}
if length of userInput is less than or equal to 1 then say (resultString as string)
if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
set openApp to (resultString as string)
if userInput contains "Activate " then set text item delimiters to TID
if userInput contains "Activate " then tell application (openApp as string) to activate

顺便说一句,这只是我脚本的一个 sn-p,这就是为什么这里有一些未定义的变量。

我试过了:

set appList to do shell script "cd /Applications; ls"
if openApp is not in appList then display dialog "App not found"

嗯,Applescript 语法有时很烦人。

谢谢。

【问题讨论】:

  • 但是您没有看到一个弹出窗口询问“xyz 在哪里?”
  • 我愿意,但如果我按下取消键,我会收到一条奇怪的消息

标签: macos applescript osx-mavericks


【解决方案1】:

exists application,但也会弹出一个对话框询问“xyz 在哪里?”。

所以你能做的最好的事情似乎是:

tell application "Finder" to set appExists to (exists file myApp of folder "Applications" of startup disk)

if not appExists then
    display alert "App not found"
end if

你的代码是:

if userInput contains "Activate " then set {TID, text item delimiters} to {text item delimiters, {"Activate "}}
if length of userInput is less than or equal to 1 then say (resultString as string)
if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
set openApp to (resultString as string)

if userInput contains "Activate " then set text item delimiters to TID

tell application "Finder" to set appExists to (exists file (openApp & ".app") of folder "Applications" of startup disk)
if not appExists then
    display alert "App not found"
else
    if userInput contains "Activate " then tell application (openApp as string) to activate
end if

或者在用户按下取消时尝试捕捉错误:

try
    if userInput contains "Activate " then tell application (openApp as string) to activate
on error
    display alert "App not found"
end try

【讨论】:

  • 谢谢,最后一个建议对我来说效果最好。
猜你喜欢
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2020-05-03
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2018-10-28
  • 2010-09-05
相关资源
最近更新 更多