2009-12-10 100 views
1

我有一個我在TextMate中構建的Ruby腳本,並且可以在TextMate中成功運行。 我也可以直接從終端成功運行這個腳本。Ruby,Mac,Geektool問題,文件訪問權限?

該腳本的代碼,這個塊在裏面:

# Get the XML file 
puts 'Opening the file' 
open("messages.xml", "r") do |f| 
    puts 'File is opened' 

    theXML = Hpricot::XML(f) 
    puts 'Trying to get the message_entity' 
    message_entity = GetMessage(theXML) 

    # Build the system command 
    puts 'Getting the author and the message' 
    theAuthor = message_entity.search(:author).text 
    theMessage = message_entity.search(:messagetext).text  

    # Get the correct image for this author 
    theAuthorImage = '' 

    case theAuthor 
     when 'James' : theAuthorImage = 'images/me32.png' 
     when 'Zuzu' : theAuthorImage = 'images/Zuzu32.png' 
    end 

    puts "/usr/local/bin/growlnotify '" + theAuthor + " says' -m '" + theMessage + "' -n 'Laurens Notes' --image '" + theAuthorImage + "'" 
    #system("/usr/local/bin/growlnotify '" + theAuthor + " says' -m '" + theMessage + "' -n 'Laurens Notes' --image '" + theAuthorImage + "'") 
end 
puts 'The End' 

當腳本由GeekTool運行時,它永遠不會越過puts 'File is opened'。它甚至沒有打到puts 'The End'。它根本沒有錯誤。

該腳本位於我Mac上/System文件夾下的文件夾下,但我已更改文件權限以允許「everyone」具有「讀取&寫入」權限。 編輯 我只是將文件複製到我的用戶主文件夾下的文件夾,它仍然存在GeekTool中的問題,但不是在TextMate中,或直接通過終端。

編輯完

第二編輯

我認爲GeekTool可能會出現問題與路徑也許文件。

例如,我改變了計劃,而只是直接從互聯網上讀取XML文件,現在它確實是不錯,但也有該程序的growlnotify使用的圖標一些圖像。通過TextMate運行時,這些圖標完美顯示。當使用GeekTool運行時...不行。根本沒有自定義圖標。

就好像GeekTool無法正確處理文件路徑一樣。 當我做puts __FILE__.to_s它雖然給我正確的文件路徑到我的.rb文件。

** end第二次編輯** 我該怎麼辦?

+0

是什麼在geektool實際shell命令,你試圖運行? – wesgarrison 2009-12-18 05:15:46

+0

ruby​​ /path/to/my/file.rb 如果我在終端鍵入確切的命令它的作品。 – 2009-12-18 16:08:15

回答

1

geektool運行/中的所有命令,因此在嘗試運行growlnotify時,相對路徑名不起作用。

puts Dir.pwd #outputs "/" 

您將需要傳遞圖像的絕對路徑growlnotify。

電流路徑可以

File.dirname(__FILE__) 

進行檢索,因此會使用

theAuthorImage = File.dirname(__FILE__) 

case theAuthor 
    when 'James' : theAuthorImage += '/images/me32.png' 
    when 'Zuzu' : theAuthorImage += '/images/Zuzu32.png' 
end 

cmd = "/usr/local/bin/growlnotify '#{theAuthor} says' -m '#{theMessage}' -n 'Laurens Notes' --image '#{theAuthorImage}'" 
puts cmd 
system cmd 
+1

當您嘗試在本地打開xml文件時也存在同樣的問題。 – 2009-12-20 23:51:30

1

嘗試將其全部包裝在下面的塊中,該塊將記錄到/tmp/geektool.txt。然後,您可以查看是否有任何您不知道的異常(如文件權限)。

begin 
    #file.open... etc 
rescue Exception => e 
    file.open('/tmp/geektool.txt', 'w'){|f| f.puts "#{e}\n\n#{e.backtrace}"} 
end 

此外,別忘了還有ruby-growl寶石。

+0

「另外,別忘了還有紅寶石寶石。」 Wiggity-什麼? – 2009-12-10 21:08:01

+0

哦,等一下,我已經看過了。我需要這個工作,沒有網絡連接。不過謝謝,我會嘗試記錄事情。 – 2009-12-10 21:08:55

+0

我沒有收到任何錯誤,但具有諷刺意味的是,試圖直接在我的塊內寫入同一個tmp/geektool.txt文件,只是將當前時間寫入它。這在Textmate和Terminal中起作用,但在腳本由GeekTool運行時不起作用。 Soooo ........不知道該從哪裏出發。 – 2009-12-10 21:19:29

0

您是否檢查過GeekTool是否向console.log或system.log輸出了任何輸出?

此外,如果它永遠不會過去'文件被打開',這可能是一個問題與寶石和要求Hpricot?

+0

我不知道在哪裏檢查console.log或system.log。我會試着弄明白。 它甚至沒有使「文件被打開」,這意味着它與常規的「打開」代碼有問題。 – 2009-12-12 20:00:00

+0

根本不在日誌中。 – 2009-12-19 05:29:56