简体   繁体   English

如果列表Applescript上没有单词,则显示对话框

[英]Display dialog if a word isn't on a list applescript

I'm writing a script that can open and close apps for me. 我正在编写一个脚本,可以为我打开和关闭应用程序。 Basically, I type in an app name, and then it opens the app. 基本上,我输入一个应用程序名称,然后打开该应用程序。 The only thing is, if the app isn't found, it throws an error and quits. 唯一的事情是,如果找不到该应用程序,它将引发错误并退出。 I want to make the script just display a dialog that says something like "App not found". 我想让脚本只显示一个对话框,上面写着“找不到应用”。

Here's what I have so far: 这是我到目前为止的内容:

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

BTW, this is just a snippet of my script, which is why there are some undefined variables here. 顺便说一句,这只是我脚本的一小段,这就是为什么这里有一些未定义的变量的原因。

I tried: 我试过了:

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

Huh, Applescript syntax can be so annoying sometimes. 呵呵,Applescript语法有时会很烦人。

Thanks. 谢谢。

There's exists application but that would also popup a dialog asking "Where is xyz?". 有一个exists application但是还会弹出一个对话框,询问“ xyz在哪里?”。

So the best thing you can do seems to be: 因此,您可以做的最好的事情似乎是:

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

Your code is then: 您的代码如下:

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

Or try to catch the error if the user presses Cancel: 或者,如果用户按“取消”,则尝试捕获错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM