AppleScript无法从/Library中删除文件

AppleScript无法从/Library中删除文件,applescript,packagemaker,Applescript,Packagemaker,我写了一份申请书: 检查/Library/dictionaries/ 如果两个文件中有一个退出,则完全删除该文件和另一个文件 该脚本在AppleScript编辑器中运行良好。然后我将脚本保存为一个应用程序,对其进行测试,该应用程序也可以正常工作。我的意思是,AppleScript编辑器中的脚本和保存的应用程序都会检测并从/Library/Dictionaries/中删除File1和File2 但是,在PackageMaker Post操作中,当调用时,应用程序既不删除文件1也不删除文件2,尽管它

我写了一份申请书:

  • 检查
    /Library/dictionaries/
  • 如果两个文件中有一个退出,则完全删除该文件和另一个文件
  • 该脚本在AppleScript编辑器中运行良好。然后我将脚本保存为一个应用程序,对其进行测试,该应用程序也可以正常工作。我的意思是,AppleScript编辑器中的脚本和保存的应用程序都会检测并从
    /Library/Dictionaries/
    中删除File1和File2

    但是,在PackageMaker Post操作中,当调用时,应用程序既不删除文件1也不删除文件2,尽管它检测到它们,甚至显示对话框消息(请参见下面的代码行)

    代码如下:

    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
    
    试试这个:

    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"
    
    这应该能奏效。
    目前我正在使用电脑,否则我会先检查,但我认为这会正常运行。

    它是退出Dictionary.app,还是脚本根本不运行?谢谢。从AppleScript编辑器运行脚本时,脚本工作正常。我将此脚本保存到一个应用程序中,当直接启动(单击)时,此应用程序也可以正常工作。但是,当PackageMaker Post操作调用此应用程序时,它似乎正在运行,但不会删除文件1和/或文件2。奇怪的是,它关闭Dictionary.app,显示File1/File2存在的对话框,并重新启动Dictionary.app,但无法删除File1/File2。