2009-08-06 33 views
2

我试图在启动plist内运行Applescript,但由于某种原因,它只是不工作。这可能是因为它是我的电脑,但我认为它可能有其他问题。如果有人可以看看这篇文章并发表评论,我会非常感激!启动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"' -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> 

感谢您的帮助!

最新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> 

回答

1

一个可能的问题是launchd会不执行你的AppleScript登录的用户的GUI语境,因此AppleScript的不能谈论到Finder。

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

尝试添加以下的plist中,以使在GUI上下文中运行该脚本:

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

注意,这只会在10.5及以上的可靠工作;我无法让每个用户的LaunchAgents在10.4上正常工作。

+0

嗨昵称: 感谢您的回复。不幸的是,即使在使用LimitLoadToSessionType代码并仔细检查路径中是否存在文件后,它仍然无法运行。这真的很奇怪,尤其是因为当我在终端中运行相同的代码时,它工作得很好......任何想法? – PF1 2009-08-12 17:01:21

+1

另一个想法:不要将applescript命令作为参数传递,而是尝试将它们放入单独的脚本文件中,并将其路径传递给osascript。 – 2009-08-13 12:07:05

1

我认为你需要将你的最终论证分解成单独的参数 - 每个参数(-e和AppleScript的各行)应该在单独的<string />元素中。如果不是这样,或者说尼克刚刚在.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\'' 

这是不是你的意思。

+0

嗨Graham:我已经完成了你的建议,但是现在当我使用StandardErrorPath记录问题时,出现以下错误:0:1:语法错误:未知令牌无法前往此处。 (-2740) – PF1 2009-08-14 20:48:17

+0

如果您在脚本编辑器中运行完全相同的脚本,是否告诉您错误在哪里? – 2009-08-14 23:34:08

+0

不,脚本编辑器的唯一区别是我将它格式化为一个Applescript文档。这工作正常。而且相同的代码在终端中运行良好,并且不会产生错误。该代码是否适合你? – PF1 2009-08-15 00:12:40