Ruby, Mac, Geektool question, file access rights?

谁说胖子不能爱 提交于 2019-12-02 11:06:21

问题


I have a Ruby script that I built in TextMate and can successfully run in TextMate. I can also successfully run this script straight from the terminal.

The script has this chunk of code in it:

# 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'

When the script is run by GeekTool, it never gets past puts 'File is opened'. It doesn't even hit puts 'The End'. It gives no error at all.

The script is under a folder under the /System folder on my Mac, but I have changed the file permissions to allow "everyone" to have "read & write" access. EDIT I just copied the files to a folder directly under my user home folder, and it still has the issue in GeekTool but not in TextMate or straight through the Terminal.

END EDIT

2nd Edit

I think GeekTool may have an issue with paths to files maybe.

For example, I changed the program to just read the XML file straight from the Internet for now and it does that just fine, but there are some images that the program is using for the icons in growlnotify. When run through TextMate, these icons display perfectly. When run using GeekTool...nope. No custom icon at all.

It's as if GeekTool just can't handle the file paths correctly. When I do puts __FILE__.to_s it gives me the correct filepath to my .rb file though.

** end 2nd edit** What should I do?


回答1:


Geektool runs all the commands from / so relative path names will not work when trying to run growlnotify.

puts Dir.pwd  #outputs "/"

You will need to pass the absolute paths of the images to growlnotify.

The current path can be retrieved with

File.dirname(__FILE__)

So you would use

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



回答2:


Try wrapping it all in a block like below, which will log to /tmp/geektool.txt. Then you can see if there are any exceptions happening that you aren't aware of (like file permission).

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

Also, don't forget there's the ruby-growl gem.




回答3:


Have you checked if GeekTool spews any output to console.log or system.log?

Also, if it never gets past 'File is opened', it might be an issue with gems and requiring Hpricot?



来源:https://stackoverflow.com/questions/1883664/ruby-mac-geektool-question-file-access-rights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!