1

我写了一个 AppleScript:

  1. 检查两个特定的字典(File1 和 File2)文件是否退出/Library/Dictionaries/
  2. 如果两个文件中的任何一个存在,则完全删除该文件和另一个

该脚本在 AppleScript 编辑器中运行良好。然后我将脚本保存为应用程序,对其进行测试,该应用程序也可以正常工作。我的意思是 AppleScript 编辑器中的脚本和保存的应用程序都检测并从/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
4

1 回答 1

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,否则我会先检查,但我认为这会正确运行。

于 2012-10-17T13:56:56.040 回答