Permission Denied when running ruby scripts?
  • Hello!

    I've just started playing around with FastScripts and haven't been able to find anything that relates to my question on the forum.

    When I run certain ruby scripts I get permission denied errors if that script does any file read/write operations or any other system or exec bash commands.

    For example, I have a script that pulls JSON and parses it. It then creates a tab-separated file and opens the file in excel.

    storage = Crack::JSON.parse(RestClient.get(url))

    storage.each do |x|
    name = x["customer_name"]
    value = x["value"]

    content = "#{name}\t#{value}\n"
    target = "foo.tsv"

    File.open(target, "a") do |f|
    f.write(content)
    end
    end

    system ("open foo.tsv")

    When I run this script from FS I get the following error:

    /Users/spayne/.../.../.../scripts/foobar.rb:22:in `initialize': Permission denied - foo.tsv (Errno::EACCES)
    from /Users/spayne/.../.../.../scripts/foobar.rb:22:in `open'
    from /Users/spayne/.../.../.../scripts/foobar.rb:22
    from /Users/spayne/.../.../.../foobar.rb:15:in `each'
    from /Users/spayne/.../.../.../scripts/foobar.rb:15

    1

    The script runs perfectly from the command line. What permissions do I need to set to launch this script with FS?

    Thanks!

    Seth
  • Hi Seth - I haven't done any scripting myself with Ruby, but I'm happy to look into this. Do you think you could come up with a minimal example script that exhibits the problem but doesn't rely on e.g. any of your URLs/etc? Is it as simple as just creating a ruby script that tries to write to a file e.g. at /tmp, would that exhibit it?

    Since I don't know Ruby if you wouldn't mind throwing some simple test script like that together and pasting it in whole here, that would be a great start for getting my head around the problem. Thanks!

    Daniel
  • #!/usr/bin/ruby

    require 'rubygems' ## all of these are required You may need to install the crack ruby gem
    require 'crack'
    require 'open-uri'
    require 'rest-client'

    url='http://en.wikipedia.org/w/api.php?action=opensearch&search=At&namespace=0' # URL with JSON output to parse

    data = Crack::JSON.parse(RestClient.get(url)) # puts the JSON into an array

    data.each do |x| # loop

    target_file = 'foo.txt' # name of the file to be created/appended

    File.open(target_file, "a") do |f| # create/open file and append with parsed JSON

    f.write(x)

    end

    end

    system ("open foo.txt") # This runs a command in the shell

    I pulled most of this example from:

    http://ruby.bastardsbook.com/chapters/web-scraping/

    When I run the script from the shell, no problems.

    When I use FS I see this:

    Error Number:/Users/spayne/Library/Scripts/example.rb:16:in `initialize': Permission denied - foo.txt (Errno::EACCES)
    from /Users/spayne/Library/Scripts/example.rb:16:in `open'
    from /Users/spayne/Library/Scripts/example.rb:16
    from /Users/spayne/Library/Scripts/example.rb:12:in `each'
    from /Users/spayne/Library/Scripts/example.rb:12

    1

  • Thanks, Seth. I was able to figure out what's going on with your example. The script is using a relative path "foo.txt" for the location of the temporary file to write to. That's fine if you're in the same directory as the script when you run it, but FastScripts doesn't make any effort to change to the directory of the script when it runs it.

    Try changing "foo.txt" to "/tmp/foo.txt" in both the line that sets the target_file variable, and in the line that does the system "open" call. This worked for me.
Start a New Discussion

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!