ゆっくり技術ノート!

いつかきっとできるだろうよ

Ruby で Pushbullet を使う

PushbulletAPI を使って各デバイスに通知を送ってみる

Pushbullet

Pushbullet はブラウザ又は Mac/Windows/iOS/Android のアプリからアクセスできて特定のデバイスにテキストや地図、ファイル等を送信できる。送信を受けたデバイスには通知センターに表示できる
今回はこれを使って Mac から任意のタイミングで iPhone にpush通知 を送るようにしたい

Gem インストール

Ruby で扱うための gem が公開されていたのでこれをインストール

gem install washbullet

自分のアクセストークンを Pushbullet の Setting から入手

32桁の文字列がアクセストークンになっている

アクセストークンを使って API から自分のid とデバイスのid を調べる

ターミナルで以下のコマンドを実行する

自分のid

curl -u Your_Token: https://api.pushbullet.com/v2/users/me

バイスのid

curl -u Your_Token: https://api.pushbullet.com/v2/devices

JSON が帰ってる。 "iden":に続く文字列がidになる

取得したidで通知を送る

require 'washbullet'

client = Washbullet::Client.new('Your_Token')

client.push_note('Your_Device_Id', 'title', 'messege')

これを実行するとで送信された

f:id:Coro:20150201222227p:plain

id を Ruby から調べる

手動でidを読むのは面倒なのでアクセストークンを入力すると自動で自分のid と Pushbullet に登録されたデバイスのid を返すようにしてみた

require "json"

def getUserId (token)

    api_response = `curl -s -u #{token}: https://api.pushbullet.com/v2/users/me`
    userId = JSON.parse(api_response)

    return userId["iden"]
end

def getDeviceId (token)

    api_response = `curl -s -u #{token}: https://api.pushbullet.com/v2/devices`
    deviceInfo = JSON.parse(api_response)

    deviceIds = Array.new
    deviceInfo["devices"].size.times do |i|
        deviceId = deviceInfo["devices"][i]["iden"]
        deviceName = deviceInfo["devices"][i]["nickname"]
        deviceModel = deviceInfo["devices"][i]["model"]

        deviceIds.push([deviceId, deviceName, deviceModel])
    end

    return deviceIds
end


token = "Your_Token"

p getUserId (token)
p getDeviceId (token)

実行すると自分のidとデバイスid,デバイスのニックネーム,デバイスモデルが表示される

参考ページ

  1. Pushbullet API Documentation
    • 公式ドキュメント
  2. hrysd/washbullet
    • 今回使ったgem、お世話になります
  3. Ruby 1.9 以降で JSON を扱う - 見上げれば、空

ありがとうございます。

TerminalでAppleScriptを実行した時のlogコマンド挙動

追記 この情報は古くなりました。

  • OS X 10.10 Yosimete ではtell application内のlogも表示されるようです

Ruby 経由でAppleScriptを実行する
test.rb

result = `osascript test.scpt`

test.scpt

on run {}
    funcA()
end run

on funcA()
    log "in funcA"
    funcB()
    funcC()
end funcA

on funcB()
    log "in funcB"
end funcB

on funcC()
    log "in funcC"
    tell application "Finder"
        log "in tell application Finder"
        
        tell application "Terminal"
            log "in tell application Terminal"
        end tell
    end tell
end funcC

すると
結果

$ ruby test.rb
in funcA
in funcB
in funcC

他のハンドラ内のlogコマンドを表示されるのに、tell application内では表示されなかった
そこで表示用のハンドラを作りtell application内から呼ぶことで解決した
test.rb

on run {}
    funcA()
end run

on funcA()
    log "in funcA"
    funcB()
    funcC()
end funcA

on funcB()
    log "in funcB"
end funcB

on funcC()
    log "in funcC"
    tell application "Finder"
        display("in tell application Finder") of me
        
        tell application "Terminal"
            display("in tell application Terminal") of me
        end tell
    end tell
end funcC

on display(message)
    log message
end display

結果

$ ruby test.rb
in funcA
in funcB
in funcC
in tell application Finder
in tell application Terminal

crontabでAppleScriptを定期的に実行する

Macで定期的に処理を実行する方法は幾つかあって crontab と launchdがあるらしい launchd は XML で記述するらしくちょっと敷居が高い感じがするので、サクッとcrontabを使うことにする

crontab

crontab(クロンタブ、あるいはクローンタブ、クーロンタブとも)コマンドはUnix系OSにおいて、コマンドの定時実行のスケジュール管理を行うために用いられるコマンドである。標準入力からコマンド列を読み取り、crontabと呼ばれるファイルにそれを記録する。この記録を元に定時になると、その命令内容を読み取り、実行が行われる。cronという名称はギリシア語のクロノス (χρόνος)に由来するという説がある(Command Run ON の略という説も)。日本ではクーロンという読みが慣習的に広く用いられているが、海外では通常クロンまたはクローンと発音する
via crontab - Wikipedia

crontab コマンド

crontab -e              crontabの設定ファイルをエディタで開く

crontab testcron.txt    テキストファイルを与えると設定ファイルを上書きする

crontab 設定の書き方

分 時 日 月 曜日の順でスペースをあけて指定する

* * * * *  コマンド     1分ごと(毎分)
14 * * * * コマンド     14分になったら(毎時)
* 13 * * * コマンド     13時になったら(毎日)
* * 12 * * コマンド     12日になったら(毎月)
* * * 11 * コマンド     11月になったら(毎年)
* * * * 3  コマンド     水曜日になったら(毎週)

複数指定もできる

0,20,40 * * * * コマンド  0分,20分,40分,になったら
1-5 * * * *     コマンド  1分,2分,3分,4分,5分になったら
*/10 * * * *    コマンド  10分ごと

処理

やりたいのはapplescriptの実行なのでスクリプトのある場所に移動してosascriptというコマンドを使ってapplescriptを実行する

0 * * * * cd /Users/theUser/Desktop/ && osascript test.applescript

とすることでデスクトップにあるtest.applescriptを毎時間実行できる様になった

おまけ

>/dev/null 2>&1をコマンドの後ろに付けるとmailを出力しないようにできる

0 * * * * cd /Users/theUser/Desktop/ && osascript tast.applescript >/dev/null 2>&1

参考ページ

  1. crontabの書き方 — server-memo.net
    • crontabの書き方
  2. crontab -e は「絶対に」使ってはいけない - ろば電子が詰まっている
  3. crontabの設定メモ - ザリガニが見ていた...。
    • 複数コマンドの指定方法と不要なmailをキャンセルする方法

大変参考になりました。ありがとうございます。

AppleScriptでiTunesのビデオにアートワークを付ける時に嵌った箇所

追記(2016.05.23)

iTunes 12.4からdescription of artwork 1 of the_trackがmissing valueを返すようになったようです なので

set artwork_description to description of artwork 1 of the_track
if artwork_description is missing value then
   ...
end if

となります missing valueはnillのようなものです

追記終わり

既に設定しているファイルをスキップする

既にアートワークを設定したファイルをスキップしたい時に音楽ファイルの場合は
if exists data of artwork1 of theTrack is false
if data of artwork1 of theTrack is not ""とすれば設定されていないことが分かる
だたビデオファイルの場合はビデオの中から自動的にサムネイルを作成してアートワークに設定してくれている様だった(違うかもしれない)

artworkの設定個数 scriptで出力されるファイル
0 artwork サムネイル
1 artwork artwork1
2 artwork artwork1 / artwork2

アートワークの説明を使う

そこでスクリプトでアートワークを設定する際にアートワークに説明(text)に任意のキーワードを設定して次にスクリプトを実行した時、説明にキーワードが設定されていたらスキップするようにした

set theDescription to description of artwork 1 of theTrack

if theDescription is "" then
    --アートワークの説明が設定されていない
end if

こうすることで1度このスクリプトで設定されたファイルはスキップできる

参考ページ

  1. itunes - Applescript editor not responding - Stack Overflow
    • exitsの使い方