【问题标题】:AppleScript cannot remove files from /LibraryAppleScript 无法从 /Library 中删除文件
【发布时间】:2012-06-17 11:44:18
【问题描述】:

我写了一个 AppleScript:

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

脚本在 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

【问题讨论】:

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

标签: applescript packagemaker


【解决方案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"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-05
    • 2011-12-21
    • 1970-01-01
    • 2023-01-20
    • 2020-04-08
    • 2016-09-27
    • 1970-01-01
    • 2017-01-12
    相关资源
    最近更新 更多