【问题标题】:Launchd PLIST Not Running已启动 PLIST 未运行
【发布时间】:2009-08-06 00:51:48
【问题描述】:

我正在尝试在已启动的 plist 中运行 Applescript,但由于某种原因它无法正常工作。可能是我的电脑,但我认为它可能有其他问题。如果有人能看一下这篇文章并发表评论,我将不胜感激!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pf.Testing</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>'tell application "Finder"' -e  'set didQuit to (path to home folder as string) &amp; ".myApp"' -e 'if (exists file didQuit) then' -e 'tell application "TestApp"' -e 'activate' -e 'end tell' -e 'end if' -e 'end tell'</string>
</array>
<key>StartInterval</key>
<integer>20</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

感谢您的帮助!

最新列表:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pf.Testing</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>'tell application "Finder"'</string>
<string>-e</string>
<string>'set didQuit to (path to home folder as string) &amp; ".myApp"'</string>
<string>-e</string>
<string>'if (exists file didQuit) then'</string>
<string>-e</string>
<string>'tell application "TestApp"'</string>
<string>-e</string>
<string>'activate'</string>
<string>-e</string>
<string>'end tell'</string>
<string>-e</string>
<string>'end if'</string>
<string>-e</string>
<string>'end tell'</string>
</array>
<key>StandardErrorPath</key>
<string>/Users/pf/Desktop/Problem.log</string>
<key>StartInterval</key>
<integer>20</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

【问题讨论】:

    标签: applescript plist launchd


    【解决方案1】:

    一个可能的问题是,launchd 没有在登录用户的 GUI 上下文中执行您的 AppleScript,因此 AppleScript 无法与 Finder 对话。

    确保 plist 安装为 LaunchAgent,而不是 LaunchDaemon(plist 应位于 /Library/LauchAgents 或 ~/Library/LaunchAgents)。

    尝试将以下内容添加到 plist 中,以使脚本在 GUI 上下文中运行:

    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    

    请注意,这只能在 10.5 及更高版本上可靠运行;我无法让每个用户的 LaunchAgent 在 10.4 上正常工作。

    【讨论】:

    • 嗨尼克:感谢您的回复。不幸的是,即使在使用 LimitLoadToSessionType 代码并仔细检查路径中是否有文件之后,它仍然无法运行。这真的很奇怪,特别是因为当我在终端中运行相同的代码时,它工作得很好......有什么想法吗?
    • 另一个想法:不要将 applescript 命令作为参数传递,而是尝试将它们放在单独的脚本文件中并将其路径传递给 osascript。
    【解决方案2】:

    我认为您需要将最终参数分解为单独的参数 - 每个参数(-e 和 AppleScript 的各个行)应该在单独的 &lt;string /&gt; 元素中。要么,或者正如尼克所说,只需传入一个包含整个脚本的.applescript 文件。

    问题是您的命令被解释为:

    /usr/bin/osascript -e '\'tell application "Finder"\' -e  \'set didQuit to (path to home folder as string) & ".myApp"\' -e \'if (exists file didQuit) then\' -e \'tell application "TestApp"\' -e \'activate\' -e \'end tell\' -e \'end if\' -e \'end tell\''
    

    这不是你的意思。

    【讨论】:

    • 嗨格雷厄姆:我已经按照你的建议做了,但是现在当我使用 StandardErrorPath 记录问题时出现以下错误:0:1:语法错误:未知令牌不能去这里. (-2740)
    • 如果您在脚本编辑器中运行完全相同的脚本,这会告诉您错误在哪里吗?
    • 不,脚本编辑器的唯一区别是我将其格式化为 Applescript 文档。这工作正常。同样的代码在终端中运行良好并且不会产生错误。该代码对您有用吗?
    • 这表明我们需要查看新编辑的 plist 文件,因为问题必须仍然存在。
    • 只是一个猜测,但问题可能是因为我将 /usr/bin/osascript 包含在 ProgramArguments 部分而不是 Program 部分?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多