2009-08-06 76 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