Applescript 检查文件是否存在 [英] Applescript to check if files exist
问题描述
我想检查/Library/Dictionaries/"中是否存在任何特定文件(字典).这是我的 Applescript 代码行:
I want to check if any of particular files (dictionaries) exist in "/Library/Dictionaries/". Here is my Applescript code lines:
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
奇怪的是,消息你已经得到了字典.
总是显示,尽管不存在列出的文件.
Weirdly, the message You have already got the dictionary.
is always shown albeit no listed files exist.
我的目的是检查是否存在任何列出的文件,如果其中一个或多个存在,则显示消息.
My purpose is to check if any of the listed files exits, and if one or more of them exits then the message is to be displayed.
事实上,这个脚本将通过 /usr/bin/osascript
作为 Unix bash 脚本运行,所以如果你能帮助使用 Apple 脚本或 Bash 脚本,我将非常感激.
In fact, this script will be run as a Unix bash script via /usr/bin/osascript
, so I will be very grateful if you can help with either Apple script or Bash script.
推荐答案
尝试
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
这篇关于Applescript 检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!