1

I write an AppleScript:

  1. to check if two specific dictionaries (File1 and File2) files exit in /Library/Dictionaries/
  2. if either of the two files exits, then completely delete that file and the other

The script works fine in AppleScript Editor. I then save the script as an app, test it, and the app also works just fine. I mean both the script in AppleScript Editor and the saved app detect and remove both File1 and File2 from /Library/Dictionaries/.

However in PackageMaker Post Action, when called, the app removes neither File1 nor File2 although it detects them and even shows the dialog message (see code line below).

Here is the code:

tell application "System Events"
  if exists (application process "Dictionary") then
    tell application "Dictionary" to quit
end if
    end tell
try
    set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
    set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"

tell application "Finder"
    if exists file theOtherFile1 then
        display dialog "File1/File2 exits. Do you want to remove it?" with title "Note" buttons {"No", "Yes"} default button "Yes"
        if the button returned of the result is "No" then
            delay 2
        else
            do shell script "rm -R /Library/Dictionaries/File1.dictionary" with administrator privileges
            do shell script "rm -R /Library/Dictionaries/File2.dictionary" with administrator privileges

        end if
    end if
end tell
end try

delay 5
tell application "Dictionary" to activate
Kara
  • 6,115
  • 16
  • 50
  • 57
Niamh Doyle
  • 1,909
  • 7
  • 30
  • 42
  • Does it quit Dictionary.app, or is the script simply not run at all? – Flambino Jun 17 '12 at 16:03
  • Thanks. The script works fine when run from AppleScript Editor. I save this script into an app, and this app works just fine as well when directly launched (clicked). However, when this app is called by PackageMaker Post Action, it appears to run but does not delete File1 and/or File2. Strangely, it closes Dictionary.app, shows dialog that File1/File2 exists, and re-launches Dictionary.app but can't remove File1/File2. – Niamh Doyle Jun 17 '12 at 17:38

1 Answers1

0

Try this:

tell application "System Events"
  if exists (application process "Dictionary") then
    tell application "Dictionary" to quit
  end if
end tell

try
    set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
    set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Terminal" to activate
tell application "System Events"
    keystroke ("sudo chmod 777 " & theOtherFile1) as string
    display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
    keystroke return
    keystroke ("chmod 777 " & theOtherFile2) as string
    display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
end tell
do shell script "rm -rf /Library/Dictionaries/File1.dictionary"
do shell script "rm -rf /Library/Dictionaries/File2.dictionary"

This should do the trick. I'm using a PC at the moment, otherwise I'd check first, but I think this will run correctly.

Stephan
  • 41,764
  • 65
  • 238
  • 329
Panah Neshati
  • 23
  • 1
  • 5