views:

226

answers:

3

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  A: 

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.

Colin Curtin
"Also, don't forget there's the ruby-growl gem."Wiggity-What?!
Pselus
Oh wait, I already looked into that. I need this to work without network connectivity. Thanks though, I'll try the log thing.
Pselus
I get no errors, but ironically enough, trying to write to that same tmp/geektool.txt file straight inside of my block, just writing the current time to it. That works in Textmate and Terminal, but not when the script is run by GeekTool.Soooo........no idea where to go from here.
Pselus
It could be GeekTool prevents scripts from opening file handles. I'll take a look at it in a bit.
Colin Curtin
A: 

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?

kejadlen
I'm not sure where to check console.log or system.log. I'll try to figure it out.It doesn't even make it to "File is opened", which means that it is having problems with the regular "open" code.
Pselus
Nothing in the logs at all.
Pselus
+1  A: 

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
Mark Carey
Same issue is behind when you are trying to open the xml file locally as well.
Mark Carey