Ruby,Mac,Geektool问题,文件访问权限? [英] Ruby, Mac, Geektool question, file access rights?
问题描述
我有一个在 TextMate 中构建的Ruby脚本,并且可以在TextMate中成功运行。
我也可以直接从终端成功运行此脚本。
该脚本中包含以下代码段:
#获取XML文件
将'打开文件'
open( messages.xml, r)做| f |
放置'文件已打开'
theXML = Hpricot :: XML(f)
放置'试图获取message_entity'
message_entity = GetMessage(theXML)
#生成系统命令
放置获取作者和消息
theAuthor = message_entity.search(:author).text
theMessage = message_entity.search( :messagetext).text
#获取该作者的正确图像
theAuthorImage =''
case theAuthor
when'James':theAuthorImage = 'images / me32.png'
当'Zuzu'时:theAuthorImage ='images / Zuzu32.png'
end
放置 / usr / local / bin / growlnotify' + theAuthor +说'-m' + theMessage +'-n'Laurens Notes'--image' + theAuthorImage +'
#system( / usr / local / bin / growlnotify' + theAuthor + said'-m' + theMessage +'-n'Laurens Notes'--image' + theAuthorImage +')
end
放置'The End'
当脚本为r时由 GeekTool 进行,它永远不会超过 puts'文件已打开'
。它甚至都没有达到放置终结
的地步。
该脚本位于Mac上 / System
文件夹下的文件夹下,但我更改了文件权限,以允许所有人具有读取和写入访问权限。
编辑
我只是将文件复制到用户主文件夹下的文件夹中,但GeekTool中仍然存在问题,但TextMate或直接通过Terminal都没有。 / p>
结束编辑
第二次编辑
我认为GeekTool可能存在文件路径问题。
例如,我将程序更改为仅读取现在直接从Internet上获取XML文件,就可以了,但是该程序正在使用一些图像作为发出通知。通过TextMate运行时,这些图标可以完美显示。使用GeekTool运行时...不。完全没有自定义图标。
好像GeekTool不能正确处理文件路径。
当我执行 put __FILE __。to_s
时,它为我提供了.rb文件的正确文件路径。
**第2次编辑**
我应该怎么做?
Geektool从/运行所有命令因此,相对路径名称在尝试运行growlnotify时将不起作用。
放入Dir.pwd #outputs /
您将需要传递图像的绝对路径以进行增长通知。
当前路径可以使用
File.dirname(__ FILE__)
因此您将使用
theAuthorImage = File.dirname(__ FILE__)
case'James':theAuthorImage + ='/images/me32.png'时的作者
'Zuzu':theAuthorImage + ='/images/Zuzu32.png'
时
$ b cmd = / usr / local / bin / growlnotify'#{theAuthor} said'-m'#{theMessage}'-n'Laurens Notes'--image'#{theAuthorImage}'
放置cmd
系统cmd
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?
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
这篇关于Ruby,Mac,Geektool问题,文件访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!