2012-06-17 3 views
1

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

  1. , чтобы проверить, если два конкретных словарей (ФАЙЛ1 и ФАЙЛ2) файлы выход в /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 
+0

Завершает ли он Dictionary.app, или скрипт просто не запускается вообще? – Flambino

+0

Спасибо. Сценарий отлично работает при запуске из редактора AppleScript. Я сохраняю этот скрипт в приложении, и это приложение прекрасно работает, когда он запускается напрямую (щелкнут). Однако, когда это приложение вызывается действием PostMaker Post Action, оно запускается, но не удаляет File1 и/или File2. Как ни странно, он закрывает Dictionary.app, показывает диалог, что File1/File2 существует, и перезапускает Dictionary.app, но не может удалить File1/File2. –

ответ

0

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

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" 

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

Смежные вопросы