– go to post
Bookmark this on Delicious
このエントリーをはてなブックマークに追加
はてなブックマーク - Notes/Ruby フィールドリスト作成

NotesDBのフィールドリストが必要になったので
簡単にRubyで書きました。

たいした内容じゃないので公開する必要なんてないんだけど
以前も書いたのですが、どっかにいっちゃって困ったのでなくさないようにメモ

#!ruby -Ks
require 'win32ole'
ns = WIN32OLE.new('Notes.NotesSession')
 
sv = gets.chomp
fn = gets.chomp
 
db = ns.GetDatabase(sv, fn)
 
title = db.Title
puts "DBTitle:#{title}"
 
forms = db.Forms
 
forms.each{|form|
  puts "FormTitle:#{form.name}"
  fields = form.fields
  fields.each{|field|
    puts field
  }
}

Posted – 12月 16th, 2008
under ruby, ノーツ
Leave a comment
 
– go to post
Bookmark this on Delicious
このエントリーをはてなブックマークに追加
はてなブックマーク - Notes 添付ファイル その2

RubyでNotesのメールを書き出す
を参考に、Notes添付ファイルを一括出力する処理をRubyで書いてみました。
まんまコピーなんですが、忘れないように記録

全件対象で処理した場合、エラーが発生し落ちてしまったので、
@Attachmentsで抽出したViewを作成し処理対象を添付ファイルが
存在する文書のみに変更しました。

require 'win32ole'
 
class Log
  def initialize(s)
    @@logger ||= Proc.new { |x| puts x }
    @@logger.call(s)
  end
 
  def Log.register_logger(p)
    @@logger = p
  end
end
 
class Notes
  class Session
    def initialize(server, dbname)
      @server, @dbname = server, dbname
      @session = WIN32OLE.new('Notes.NotesSession') or return nil
      @db = @session.GetDatabase(@Server, @dbname)
    end
 
    def each
      view = @db.GetView('Attachments')
      doc = view.GetFirstDocument
 
      while doc
        yield Notes::Document.new(doc)
        doc = view.GetNextDocument(doc)
      end
    end
    include Enumerable
 
    #添付ファイルをdirに書き出す
    def extract_files(dir)
      self.each do |doc|
        doc.each_attachment do |obj|
          name = obj.name
          Log.new "\t#{name}"
 
          #対象外の拡張子をチェックする
          ext = File.extname(name).downcase
          next if %w(htm html csv pif scr com bat).include?(ext)
 
          #書き出しメソッドを呼び出し
          doc.extract_file(name, "#{dir}/#{name}")
        end
      end
    end
 
    #添付ファイルをdir/fromに書き出す
    def extract_files_by_sender(dir, body_hash = nil)
      self.each do |doc|
        k = doc.unid
 
        #出力先の作成
        extract_dir = "#{dir}/#{k}"
        Dir.mkdir(extract_dir) unless File.exist?(extract_dir)
 
        doc.each_attachment do |obj|
          name = obj.name
          Log.new "\t#{name}"
          ext  = File.extname(name).downcase
          next if %w(htm html csv pif com bat).include?(ext)
          doc.extract_file(name, "#{extract_dir}/#{name}")
        end
      end
    end
  end
 
  class Document
    def initialize(doc)
      @doc = doc
    end
 
    def each_attachment
      a = @doc.Items or return
      a.each do |obj|
        yield Notes::Attachment.new(obj) if obj.Type == 1084 #ATTACHMENT
      end
    end
 
    def unid;    @doc.UniversalID; end
 
    def extract_file(name, path)
      obj = @doc.GetAttachment(name) or return
      begin
        obj.ExtractFile(path)
      rescue => e
        Log.new "**** #{e.message} ****"
      end
    end
  end
 
  class Attachment
    def initialize(obj)
      @obj = obj
    end
 
    def extract_file(path)
      @obj.ExtractFile(path)
    end
 
    def name
      @obj.Values[0]
    end
  end
end

呼出し側

require 'notesruby'
require 'optparse'
 
opt_hash = Hash.new
opt_hash['dir'] = '.'
 
p ARGV
ARGV.options do |opt|
    opt.on('-d [dir]') { |v| opt_hash['dir'] = v }
    opt.on('-s') { |v| opt_hash['same_folder'] = v }
    opt.parse!
end
 
dir = File.expand_path(opt_hash['dir'])
if !File.writable?(dir) || ARGV.size == 0
  Log.new "Usage: ruby #{$0} [-s] [-d dir] file [file...]"
  exit
end
 
body_hash = Hash.new
ARGV.each do |arg|
  if m = arg.match(/(.*)@(.*)/)
    nsf, svr = m[1, 2]
  else
    nsf, svr = [arg, '']
  end
 
  if db = Notes::Session.new(svr, nsf)
    Log.new "添付ファイル抽出...(#{arg})"
    if opt_hash['same_folder']
      db.extract_files(dir)
    else
      db.extract_files_by_sender(dir, body_hash)
    end
  end
end

Posted – 10月 23rd, 2008
under ノーツ
Leave a comment
 
– go to post
Bookmark this on Delicious
このエントリーをはてなブックマークに追加
はてなブックマーク - Notes 添付ファイル

ノーツ移行に伴って添付ファイル一括書き出しを書いたのでメモ
Viewアクションから実行
文書IDごとにディレクトリを切って添付ファイルを保存
これですむといいな。

Sub Click(Source As Button)
	Dim session As New NotesSession
	Dim db As NotesDatabase
	Dim collection As NotesDocumentCollection
	Dim doc As NotesDocument
	Dim rtitem As Variant
	Dim itemname As Variant
	Dim item As NotesItem
	Dim object As NotesEmbeddedObject
 
	Set db = session.CurrentDatabase
	Set collection = db.UnprocessedDocuments
 
	For i = 1 To collection.Count
		Set doc = collection.GetNthDocument( i )
		If doc.HasEmbedded Then
			Do While doc.HasItem( "$FILE" )
				Set item = doc.GetFirstItem( "$FILE" )
				If item.Type = ATTACHMENT Then
					folderpath = "c:\temp" & "\" & doc.universalid
					If Dir(folderpath,16)="" Then
						Mkdir folderpath
					End If
					itemname = doc.GetItemValue( "$FILE" )
						Set object = doc.GetAttachment( itemname(0) )
					Call object.ExtractFile( folderpath & "\" & itemname(0))
				End If
				Call item.Remove
			Loop
		End If
	Next
End Sub