AppleScript无法从/ Library中删除文件

时间:2012-06-17 11:44:18

标签: applescript packagemaker

我写了一个AppleScript:

  1. 检查/Library/Dictionaries/
  2. 中是否有两个特定词典(File1和File2)文件退出
  3. 如果两个文件中的任何一个退出,则完全删除该文件而另一个
  4. 该脚本在AppleScript编辑器中工作正常。然后我将脚本保存为应用程序,测试它,该应用程序也可以正常工作。我的意思是AppleScript编辑器中的脚本和保存的应用程序检测并从/Library/Dictionaries/中删除File1和File2。

    然而,在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
    

1 个答案:

答案 0 :(得分: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"

这应该可以解决问题。 我现在正在使用PC,否则我先检查,但我认为这将正常运行。