问题描述
我开始使用 launchd 时遇到了一个奇怪的问题。当我发送“显示通知”时,它可以在脚本编辑器中正常工作,但不能从 launchd 中正常工作。 “say”命令实际上从launchd开始工作。为了测试,我还尝试了“显示对话框”命令,该命令也不适用于launchd。这之后的最后一句命令也听不见。
我查看了 Launchctl 的手册页,但没有发现与此问题相关的任何内容。
我还尝试将 plist 放在 System Library/LaunchAgents 而不是 ~/Library/LaunchAgents 中,但行为是相同的。
这里是脚本代码,包含说明什么地方和什么时候起作用:
say "This is before the display notification"
## the say command is audible both from within
## the script editor AND from launchd
display notification "Less than 20% Battery Remaining,plug in soon." with title "Batterij opladen" sound name "Basso"
## the display notification command is working
## fine within Script Editor,but not from launchd
say "This is after the display notification"
## the say command is audible both from
## within the script editor AND from launchd
set theResponse to display dialog "What's your name?" default answer "" with icon note buttons {"Cancel","Continue"} default button "Continue"
--> {button returned:"Continue",text returned:"Jen"}
display dialog "Hello," & (text returned of theResponse) & "."
## the display dialog command is working
## fine within Script Editor,but not from launchd
say "This is after the display dialog"
## this say command is audible from within the
## script editor but NOT from launchd
## so this part is not executed
我的实现:(1) sudo su root 获取root权限 (2)我把脚本复制到/etc文件夹 (3) 我在 Apple TextEdit.app 中创建了 plist (4)我把plist复制到~/Library/LaunchAgents文件夹 (5)我使用plutil检查plist:没有错误 (6) 我使用 sudo chown root TestSpk.plist 设置 plist 属性 (7) 然后我使用 Launchctl load TestSpk.plist 运行 plist (8)launchctl列表| grep TestSpk 返回零,所以 plist 应该可以正常工作
这是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>KeepAlive</key>
<false/>
<key>Label</key>
<string>TestSpk</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/etc/TestSpk.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceDescription</key>
<string>TestSpk</string>
<key>StartInterval</key>
<integer>10</integer>
</dict>
</plist>
我错过了什么?
谢谢,皮特
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)