2006/11/11

RubyOSA Safari を試す

いま開いている Safari のロケーションから
body 要素を取り出しソースとテキストを表示してみました。

require 'rbosa'
require 'nkf'
app = OSA.app('Safari')

if app.documents.size > 0
  # Get HTML Source
  source = app.documents[0].source
  # Get Body
  body = source.split('<body>')[1].split('</body>')[0]
  puts '------ html source \n'
  puts NKF.nkf('-w -Lu', body)
  puts '------ text \n'
  # HTML タグの除去 - >かなりな大雑把版。それに Source が UTF8だと決めつけてます。
  print NKF.nkf('-w -Lu', body.gsub(/\<.*?\>|<\/|>|<script.*?\>/m, ''))
else
 # 空白ページが開かれていたら終わる.これ以外のエラーには未対処。
  puts 'no doc'
end


--imported_from
http://www.midore.net/daybook/2006/11/1163227004.html

+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

2006/11/08

RubyOSA 0.1 のFinderを試す

# Finder をアクティブにする

tell the application 'Finder' to activate

は、RubyOSA だと

OSA.app('Finder').activate

#home 以下全フォルダ名の取得 --get all folder's name of path to home

tell the application 'Finder'
  return name of folders of home
end tell

は、RubyOSA だと

OSA.app('Finder').home.folders.to_a.each{ |folder| puts folder.name }

#Desktop 上の全ファイル名の取得

tell the application 'Finder' to name of every document file in the desktop

は、RubyOSA だと

require 'nkf'
OSA.app('Finder').document_files.each{ |file| puts NKF.nkf('-w -Lu', file.name)}

end

--imported_from
http://www.midore.net/daybook/2006/11/1162972588.html


+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

2006/11/02

iTunes ライブラリの曲

rubyosa を使って iTunes ライブラリの曲(全曲)を次から次へと再生する試み。
sleep 3で3秒を指定しています。

require 'rbosa'
require 'nkf'
itunes = OSA.app('iTunes')
itunes_libray = itunes.sources[0].playlists[0]
#=> ['ライブラリ']
itunes_libray.play

until itunes.current_track.name.nil?
  puts NKF.nkf('-w -Lu', itunes.current_track.name ) #current_track.name.toutf8
  sleep 3
  itunes.next_track
end


itunes_libray = itunes.sources[0].playlists[0] ここの数字を変えるとライブラリじゃなくて自分で作成したプレイリストを指定できた。
でも自分で作ったプレイリストを指定したいときはダイアログから名前を指定できるといいのかなと、OSA.app('Finder').display dialogの命令を探してみたんですがわからなかった。。。

--imported_from
http://www.midore.net/daybook/2006/11/1162440770.html

+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

2006/11/01

rubyosa-0.1のインストール

Introduction to Ruby for Mac OS X

MacOSXでRubyお奨めするチュートリアルを読んでみました。
http://www.io.com/~jimm/writing/Intro_to_Ruby.html
翻訳ページの力を借りて。http://translate.google.com/translate_t

最後のチュートリアルは AppleScript との連携でした。標準外のRubyのライブラリーが必要とありまして
http://www.fobj.com/rubyaeosa/INDEX.ja.html
↑ここをへてrubyosa-0.1へ。
http://rubyosa.rubyforge.org/

rubyaeosa の後継はrubyosa というらしい (いまはAppleの人が作っているのかな!?) 早速、rubyosa-0.1のインストール。
いきなりサンプルにある TextEdit_hello_world.rb を実行!してみるとあえなくエラー。

インストールしたばかりの /usr/local/lib/ruby/site_ruby/1.8/rbosa.rb
の31 行目をみてみると
>begin require 'rubygems' rescue LoadError end
このマシンには rubygems をまだインストールしていなかったんでした。なので、サンプルが動きませんでした。
rubygems をインストールしてない人は論外と...逆に言うとソースからインストールした場合でも rubygems がありさえすればサンプル動かしてあげる、と。
ひょとして!新しいOSXには gems が標準で入っているということ?勝手なことを考えます。

ここで素直に gems をインストールすべきなんでしょうがこの行だけをコメントアウトするという選択もあるので...ちょっと#を付け足したてみたら サンプルにある TextEdit_hello_world.rb は無事動き、TextEditorが起動して新しいファイルを作り一行目に hellow word を書き出してくれました。

試しに日本語をいれてみたら(utf-8 でエンコーディングしたファイル)TextEditorは起動したものの文字は空白でだめでした。が sjis化 すれば大丈夫でした。
日本語 utf-8 を投げるのは無理みたいです。なんか方法あるのかしら。

str = 'はろー'
OSA.app('TextEdit').make(OSA::TextEdit::Document).text = str.tosjis

iTunesのサンプルは、ずらずらっと曲名と名前を列挙してくれるサンプルなんかが入っていて、iTunesに話しかける言葉をいろいろ用意してるから rdoc でドキュメント作るスクリプトを動かしてドキュメント作って読んでね!ということでした。なぜかこのスクリプトを動かすとエラーで途中で止まってしまったですが、/tmp/iTumes.rb というファイルができました。それをもとにドキュメントを作れました。 
で、safari さわってみました。

safari = OSA.app('Safari')
p safari.properties
p safari.version
safari.make(OSA::Safari::Document).url = 'http://google.com'
=> {'pnam'=>'Safari', 'pisf'=>false, 'pcls'=>#<OSA::Safari::Application:0x529248>, 'vers'=>'2.0.4'}
'2.0.4'

わーぃ。tell application 'Safari' get properties end tell っていう気持ちがそのまま通じたのでちょっとうれしかった。ただ、だんだん頭が混乱してゆくかもー。
str = 'hoge' としたくて set str to 'hoge' としたりして、どうしてエラーなの?と悩んでいそうな...

--imported_from
http://www.midore.net/daybook/2006/11/1162346274.html


+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

2006/06/27

ひさびさにAppleScriptエディタ使った

By:“midore” at:2006/06/27(火曜) 13:10曇り

ぐるりさん,http://www.gururi.com/applescript /2006-06-21T010223.htm に触発されて私も考えてみたです。100以下の素数という条件なのでとても素直に考えてみたつもり。けど、seedに予めいれておくのはやっぱ反則なのかな???これダメな感じか?そんな気もしつつ...。

set seed to {2, 3, 5, 7}

repeat with targetnum from 2 to 100
my check(targetnum, seed)
end repeat
return seed

to check(targetnum, seed)

set checklist to {}
repeat with i in seed
set end of checklist to targetnum mod i
end repeat

if checklist does not contain 0 then
set end of seed to targetnum
end if

end check

=> {2, 3, 5, 7, 13, 11, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}

--imported_from
http://www.midore.net/daybook/2006/5-9/1151381435.html

2006/06/19

文字コードの和

By:“midore” at:2006/06/19(月曜) 18:29曇のち雨

特に意味もなく自分の名前の文字コードの和が素数だったらいいなと期待したが、

def my_no(str)
  #unsigned char (8bit 符号なし整数)
  a = "#{str}".unpack("C*")
  mynum = 0
  a.each{|str|
    a_x = sprintf("%x", str).to_i
    mynum = mynum + a_x
    }
    return mynum
end
puts "sum = " + my_no("midore").to_s + "?n"
exit


=> sum = 282

素数じゃなかった...

http://ruby-lang.org/ja/man/?cmd=view;name=pack%A5%C6%A5%F3%A5%D7%A5%EC%A1%BC%A5%C8%CA%B8%BB%FA%CE%F3;em=unpack;em=C

--imported_from
http://www.midore.net/daybook/2006/5-9/1150709387.html