1

「/Library/ Dictionary /」に特定のファイル(辞書)が存在するか確認したい。これが私の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.奇妙なことに、リストされたファイルが存在しない場合でも、メッセージは常に表示されます。

私の目的は、リストされたファイルのいずれかが存在するかどうかを確認することです。それらの1つ以上が存在する場合は、メッセージが表示されます。

実際、このスクリプトはを介して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 に答える