Bash Applescript检查文件是否存在
我想检查“/Library/dictionaries/”中是否存在任何特定文件(字典)。以下是我的Applescript代码行:Bash Applescript检查文件是否存在,bash,unix,applescript,Bash,Unix,Applescript,我想检查“/Library/dictionaries/”中是否存在任何特定文件(字典)。以下是我的Applescript代码行: tell application "Finder" try set theFolder to ("/Library/Dictionaries/") set fileNames to {"dict1.dictionary", "dict2.dictionary", "dict3.dictionary", "dict_n.dictionary"} on e
tell application "Finder"
try
set theFolder to ("/Library/Dictionaries/")
set fileNames to {"dict1.dictionary", "dict2.dictionary", "dict3.dictionary", "dict_n.dictionary"}
on error
set fileNames to false
end try
if fileNames is not false then
try
display dialog "You have already got the dictionary."
end try
end if
end tell
奇怪的是,消息您已经得到了字典。
总是显示出来,尽管不存在列出的文件
我的目的是检查是否有任何列出的文件退出,如果有一个或多个文件退出,则将显示消息
事实上,此脚本将通过/usr/bin/osascript
以Unix bash脚本的形式运行,因此如果您能帮助使用Apple脚本或bash脚本,我将非常感激。试试看
set theFolder to (path to library folder as text) & "Dictionaries:"
set fileNames to {"Apple Dictionary.dictionary", "dict2.dictionary", "dict3.dictionary", "dict_n.dictionary"}
set dict to {}
repeat with aFile in fileNames
tell application "Finder"
if exists file (theFolder & aFile as text) then set end of dict to aFile & return
end tell
end repeat
if dict ≠ {} then display dialog "You have the following dictionaries installed:" & return & dict
另一种方法是尝试将文件对象强制为别名:
set p to (system attribute "HOME") & "/Desktop/test.txt"
try
POSIX file p as alias
true
on error
false
end try
请注意,当计算as alias
时,这会导致错误:
"/non-existing/path"
tell application "Finder" to exists POSIX file result as alias
这不会导致错误:
POSIX file "/non-existing/path"
tell application "Finder" to exists result
您还没有测试任何东西-您所做的只是初始化两个变量。另外,如果您将此表单称为bash脚本,那么为什么不直接从bash测试文件的存在性,即不使用AppleScript?for bash:
for s in 1 2 3\n;Don t=正确;测试-f/库/字典/dict“$s”。字典和中断;t=假;完成$t&&echo dict“$s”。字典存在
非常感谢,它运行良好,但每次找到列出的文件时,对话框都会重复。是否有任何方法可以使对话框只显示一次,而不考虑存在一个或多个列出的文件?非常感谢!编辑后的版本不仅有技巧,还有魔力:-)。它就像一个符咒。你是第一!你能看一看吗。谢谢