1

我想检查“/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 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

You have already got the dictionary.奇怪的是,尽管没有列出的文件存在,但始终显示该消息。

我的目的是检查是否存在任何列出的文件,如果其中一个或多个存在,则将显示该消息。

事实上,这个脚本将作为 Unix bash 脚本通过 运行/usr/bin/osascript,所以如果您能提供 Apple 脚本或 Bash 脚本方面的帮助,我将不胜感激。

4

2 回答 2

2

尝试

   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
于 2012-09-06T11:35:38.593 回答
1

另一种方法是尝试将文件对象强制为别名:

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
于 2012-09-06T11:56:48.037 回答