0

So I have found this RUBY script, which looks for all PNG images in sub folders and folders and converts PNG images using TinyPNG API but for some reason I get runtime error

C:/Users/Vygantas/Desktop/tinypng.rb:14:in `': Usage: ./tinypng.rb C:/Users/Vygantas/Desktop/product C:/Users/Vygantas/Desktop/product(RuntimeError)

#!/usr/bin/ruby -w

#
# tinypng.rb — Placed into the public domain by Daniel Reese.
#

require 'rubygems'
require 'json'

# Set API key.
apikey = "xxxxxxxxxxxxxxxx"

# Verify arguments.
ARGV.length == 2 or fail("Usage: ./tinypng.rb C:\Users\Vygantas\Desktop\product C:\Users\Vygantas\Desktop\product*emphasized text*")
src = ARGV[0]
dst = ARGV[1]
File.exist?(src) or fail("Input folder does not exist: " + src)
File.exist?(dst) or fail("Output folder does not exist: " + dst)

# Optimize each image in the source folder.
Dir.chdir(src)
Dir.glob('*.png') do |png_file|
    puts "\nOptimizing #{png_file}"

    # Optimize and deflate both images.
    cmd = "curl -u api:#{apikey} --data-binary @#{png_file} 'https://api.tinypng.com/shrink'"
    puts cmd
    r = JSON.parse `#{cmd}`
    if r['error']
        puts "TinyPNG Error: #{r['message']} (#{r['error']})"
        exit(1)
    end
    url = r['output']['url']
    cmd = "curl '#{url}' -o #{dst}/#{png_file}"
    puts cmd
    `#{cmd}`
end
Dir.chdir("..")

puts 'Done'
4
  • I need whole error message and backtrace to debug this error. Commented May 14, 2015 at 7:48
  • @MarekLipka Of course you don’t need it. Take a precise look: it’s a script who prints it because an expectation on amount of arguments was not met :) Commented May 14, 2015 at 7:49
  • @mudasobwa You're right. Go ahead with an answer. :) Commented May 14, 2015 at 7:51
  • @Lipka, that's all I get Commented May 14, 2015 at 7:53

1 Answer 1

1

As you might see in the code, line 14 (as printed on script execution):

ARGV.length == 2 or fail("Usage: ./tinypng.rb 
    C:\...\product C:\...\product*emphasized text*")

That said, a script requires two parameters to run. Let me guess: you did not pass two parameters. Those are btw source and destination folders.

Sign up to request clarification or add additional context in comments.

6 Comments

If I change to ARGV.length == 2 or fail("Usage: "./tinypng.rb", "/product", "/product"") I get: C:/Users/x/Desktop/tinypng.rb:14: syntax error, unexpected tSTRING_BEG, e xpecting keyword_do or '{' or '(' ...age: "./tinypng.rb", "/product", "/product"")
You have to specify arguments to a script, you are not supposed to modify the script itself. Like this (in console): ./tinypng.rb C:/Users/Vygantas/Desktop/product C:/Users/Vygantas/Desktop/productModified. NB Source and destination folder should likely differ.
I am an idiot! Still, looks like it is broken :/ Optimizing textpicture.png curl -u api:cfgibsPM-f_7ETa9jgv_WkONlpEGsU920 --data-binary @textpicture.png 'api.tinypng. om/shrink' C:/Users/Vygantas/Desktop/tinypng.rb:28:in ``': No such file or directory - cur -u api:cfgibsPM-f_7ETa9jgv_WkONlpEGsU920 --data-binary @textpicture.png 'api.tinypng.com hrink' (Errno::ENOENT) from C:/Users/Vygantas/Desktop/tinypng.rb:28:in block in <main>' from C:/Users/Vygantas/Desktop/tinypng.rb:22:in glob' from C:/Users/Vygantas/Desktop/tinypng.rb:22:in `<main>'
Install curl on the target machine? Whether it’s Ubuntu, run sudo aptitude install curl.
Now just says done and no files are outputed, anyway, thanks for help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.