1

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.

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.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42
  • You haven't tested anything - all you have done is initialise two variables. Also, if you're calling this form a bash script anyway, then why not just test for the existence of the files directly from bash, i.e. without resorting to AppleScript ? – Paul R Sep 06 '12 at 10:36
  • For Bash: `for s in 1 2 3 _n; do t=true; test -f /Library/Dictionaries/dict"$s".dictionary && break; t=false; done; $t && echo dict"$s".dictionary exists` – tripleee Sep 06 '12 at 13:02

2 Answers2

2

Try

   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
adayzdone
  • 11,120
  • 2
  • 20
  • 37
  • Many thanks, it works well but the dialog keeps repeating every time a listed file is found. Are there any ways to get the dialog displayed just one time regardless of one or more than one listed file exists? – Niamh Doyle Sep 06 '12 at 12:04
  • Thank you very much! The edited version does not only the trick but also the magic :-). It works like a charm. You're number one! – Niamh Doyle Sep 06 '12 at 12:17
  • Can you have a look at [this problem](http://stackoverflow.com/questions/12332864/why-does-this-preinstall-script-just-partly-run). Thanks. http://stackoverflow.com/questions/12332864/why-does-this-preinstall-script-just-partly-run – Niamh Doyle Sep 09 '12 at 05:22
1

Another method is to try to coerce a file object to an alias:

set p to (system attribute "HOME") & "/Desktop/test.txt"
try
    POSIX file p as alias
    true
on error
    false
end try

Note that this results in an error when as alias is evaluated:

"/non-existing/path"
tell application "Finder" to exists POSIX file result as alias

This doesn't result in an error:

POSIX file "/non-existing/path"
tell application "Finder" to exists result
Lri
  • 26,768
  • 8
  • 84
  • 82