[ANN] unclose 0.1 alpha

  • Thread starter Koen Vervloesem
  • Start date
K

Koen Vervloesem

--Apple-Mail-5--332282667
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed

Hi,

Today I made a simple tool for MacOS X. I heavily use the "open"
command in MacOS X: When I'm working in the Terminal and I want to open
a PDF file in Acrobat Reader, I just have to type "open file.pdf" and
it opens quickly in Acrobat Reader. But when I know the url of a PDF
file on the net, an I type "open http://blabla.org/file.pdf", it opens
an empty Safari window before opening the PDF file in Acrobat Reader.
That's because "open" opens all files in a http url with the standard
browser.

That gave me the idea to make a configurable script that can open local
and remote files like the command "open", but you can specify in a
configuration file which remote files the script has to open with the
default browser. Regularly, you want html files (and php and some
others) to be opened by your browser directly, and all others (pdf, ps,
tar.gz) downloaded by the unclose script and opened by their standard
application. So you don't get an empty browser window with Safari.

I have included the source code of unclose.rb. All arguments to the
script are opened as files or urls. You can configure unclose.rb in a
file .uncloserc in your home directory. This file can contain e.g.:

download_dir ~/Desktop/Download
no_download html
no_download php

This says all downloaded files will be downloaded in de directory
Desktop/Download in your home directory, and that remote html and php
files will be viewed directly by your browser and not downloaded by
unclose to be viewed locally by your browser.

I'd like to get some comments. I want to extend unclose to be used with
other operating systems if e.g. GNU/Linux or MS Windows have a similar
command as the "open" command. And I will support more protocols, like
ftp and scp. After that, I will package it in RAA.

Regards,

Koen Vervloesem
--
"Men have a gene which enables them to answer any question, no matter
how complex or important, with Mmm."
--Apple-Mail-5--332282667
Content-Disposition: attachment;
filename=unclose.rb
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
x-unix-mode=0644;
name="unclose.rb"

# unclose 0.1 alpha
# A helper script for the MacOS X "open" command.
#
# (c) 2003 Koen Vervloesem
# mailto: (e-mail address removed)

require 'uri'
require 'net/http'

# The class Encloser has one public method: open.
# With this method, one can open a remote or local file.
class Uncloser

# Open <document>.
def Uncloser.open(document)
doc_uri = uri(document)
if doc_uri != nil and download?(doc_uri)
document = to_download_dir(document)
download(doc_uri, document)
end
openlocal(document)
end

private

# Download <uri> and save it in file with name <filename>
def Uncloser.download(uri, filename)
Net::HTTP.start(uri.host, uri.port) {|http|
response = http.get(uri.path)
f = File.new(filename, "w")
f.syswrite(response.body)
f.close
}
end

# Do we have to download <uri>?
def Uncloser.download?(uri)
filename = uri.path
if filename.include?(".")
extension = filename.split(".").last
puts extension
UncloseConfig.download?(extension)
else
true
end
end

# Open the local file <document> with the system's open command.
def Uncloser.openlocal(document)
system(UncloseConfig.open_command, document)
end

# Give the full path of the downloaded file <document>.
def Uncloser.to_download_dir(document)
UncloseConfig.download_dir + "/" + File.basename(URI.parse(document).path)
end

# Give a supported uri object of <document>.
# Now only the http scheme is supported.
def Uncloser.uri(document)
uri = URI.parse(document)
if uri.scheme == 'http'
uri
else
nil
end
end

end

# This class reads the configuration of Unclose in its configuration file.
class UncloseConfig

CONFIG_FILE = ENV["HOME"] + "/.uncloserc"

# Do we have to download a file with extension <extension>?
def UncloseConfig.download?(extension)
ignore =[]
if FileTest.exists?(CONFIG_FILE)
IO.foreach(CONFIG_FILE) { |line|
puts line
if line =~ /^no_download\s+(\S+)/
ignore << $1.upcase
end
}
end
!ignore.include?(extension.upcase)
end

# What is the download directory?
def UncloseConfig.download_dir
dir = "."
if FileTest.exists?(CONFIG_FILE)
IO.foreach(CONFIG_FILE) { |line|
if line =~ /^download_dir\s+(\S+)/ and FileTest.directory?(File.expand_path($1))
dir = File.expand_path($1)
end
}
end
dir
end

# What is the open command of our operating system?
def UncloseConfig.open_command
command = "open"
if FileTest.exists?(CONFIG_FILE)
IO.foreach(CONFIG_FILE) { |line|
if line =~ /^open_command\s+(\S+)/ and FileTest.executable?($1)
dir = $1
end
}
end
command
end

end

# Open all arguments with Uncloser.
ARGV.each {|x| Uncloser.open(x)}

--Apple-Mail-5--332282667--
 
G

gabriele renzi

il Sun, 6 Jul 2003 04:21:48 +0900, Joel VanderWerf
KDE has "kfmclient exec", which is very powerful. It's a shell interface
to everything you can do with the KDE tools--download, ftp, http, pdf,
or open any mime type with the associated app.

btw, windows has the dos prompt or run or whatever, just type in and
get what you want ;)
BUT this is a really cool script anyway
 
D

Damphyr

gabriele said:
il Sun, 6 Jul 2003 04:21:48 +0900, Joel VanderWerf



btw, windows has the dos prompt or run or whatever, just type in and
get what you want ;)
BUT this is a really cool script anyway
Yeap, but you cannot do 'cmd http://some.or.other/file.pdf' and have the
thing open up acrobat. Nor can you "execute" files and have the coomand
box call the associated application.
It could be interesting to see if you can have such an app read the
associations correctly for all three window systems (KDE (maybe even
Gnome), MacOS X and Windows). Can't think it would do much good for CLI
application on Linux though.
V.-



____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
G

gabriele renzi

il Sun, 6 Jul 2003 22:06:19 +0900, Damphyr <[email protected]> ha
scritto::

Yeap, but you cannot do 'cmd http://some.or.other/file.pdf' and have the
thing open up acrobat.

well, you have the acrobat OLE in IE..
Nor can you "execute" files and have the coomand
box call the associated application.

sure you can,
open cmd.exe and write
yourfile.ext
and you'll get it opened with the associated app
It could be interesting to see if you can have such an app read the
associations correctly for all three window systems (KDE (maybe even
Gnome), MacOS X and Windows). Can't think it would do much good for CLI
application on Linux though.
V.-

I suppose rox/gnome/kde/xfce now have a common mime database, so you
can rely on that and write back ends to get the mime->app tuples from
every database (i.e GConf or whatever)
good luck!
 
J

Josef 'Jupp' Schugt

Saluton!

* Damphyr; 2003-07-06, 14:08 UTC:
Yeap, but you cannot do 'cmd http://some.or.other/file.pdf' and
have the thing open up acrobat. Nor can you "execute" files and
have the coomand box call the associated application.

Adding a text file to Autostart does automatically open that file in
notepad. I no longer use Windows but the 'command'

http://some.or.other/file.pdf

should open the file in the application associated to the Portable
Document File type (usually Acrobat or Acrobat Reader).

Gis,

Josef 'Jupp' Schugt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top