AppleScript не может удалить файлы из /Library

Я пишу AppleScript:

  1. чтобы проверить, выходят ли файлы двух конкретных словарей (File1 и File2) в /Library/Dictionaries/
  2. если какой-либо из двух файлов выйдет, полностью удалите этот файл, а другой

Скрипт отлично работает в редакторе AppleScript. Затем я сохраняю скрипт как приложение, тестирую его, и приложение также работает нормально. Я имею в виду, что и скрипт в редакторе AppleScript, и сохраненное приложение обнаруживают и удаляют файлы File1 и File2 из /Library/Dictionaries/.

Однако в PackageMaker Post Action при вызове приложение не удаляет ни File1, ни File2, хотя обнаруживает их и даже показывает диалоговое сообщение (см. строку кода ниже).

Вот код:

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

person Niamh Doyle    schedule 17.06.2012    source источник
comment
Он закрывает Dictionary.app или скрипт просто не запускается?   -  person Flambino    schedule 17.06.2012
comment
Спасибо. Скрипт отлично работает при запуске из редактора AppleScript. Я сохраняю этот скрипт в приложении, и это приложение также отлично работает при прямом запуске (щелчке). Однако, когда это приложение вызывается PackageMaker Post Action, кажется, что оно запускается, но не удаляет File1 и/или File2. Как ни странно, он закрывает Dictionary.app, показывает диалоговое окно, в котором существует File1/File2, и повторно запускает Dictionary.app, но не может удалить File1/File2.   -  person Niamh Doyle    schedule 17.06.2012


Ответы (1)


Попробуй это:

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"

Это должно сработать. В данный момент я использую ПК, иначе я бы сначала проверил, но я думаю, что это будет работать правильно.

person Panah Neshati    schedule 17.10.2012