りんごの備忘録

へなちょこポカ大生のへなちょこブログ

AppleScriptでアートワークを入手するまで(part2)

前回,アルバムアートワークを入手するスクリプトを作ったがいかんせん精度が低い.
rinringo429.hatenablog.com


それならば画像を人間の目で選べばいいだろうと考え新しく作ってみました.

tell application "iTunes"
        set theAlbum to item 1 of album of selection
	set theAlbumArtist to item 1 of album artist of selection
	set theArtist to item 1 of artist of selection
	set searchURL to "https://www.google.co.jp/search?q=" & theAlbum & "+" & theAlbumArtist & "&source=lnms&tbm=isch&sa=X" as string
	
	tell application "Safari" to open location searchURL
	
	set the clipboard to theAlbum
end tell

display dialog "Please select the picture"

tell application "iTunes" to set theAlbum to album of item 1 of selection
set theClip to (get the clipboard)
set theDestination to "" -- 画像を保存するパスを指定
set thePath to theDestination & ":" & theAlbum & ".png"
do shell script "curl -L " & theClip & " -o \"" & POSIX path of thePath & "\""
set targetImageFile to thePath
tell application "iTunes"
	set theTrack to selection
	repeat with i in theTrack
		set data of artwork 1 of i to (read (file targetImageFile) as picture)
	end repeat
end tell
使い方
  1. iTunesで一つのアルバムを⌘+左クリックで選択する
  2. 上のコードを実行し適当な画像のurlをクリップボードにコピーする
  3. 元から出ていたdialog のOKボタンを押す
  4. 後は待つだけ

AppleScriptでアートワークに画像を差し込むまで

アルバムにアートワークをつける作業で時間がかかるのが画像を探すことと,それを反映させるところです.前回の記事では画像をダウンロードするところまでしたので今回は画像を反映させてみたいと思います.
iTunes10.2から仕様が変わっていたのに気付かず,画像をPICTに変換するところで戸惑ってました.しかし10.2からは直接画像を差し込むことができるようになったのでかなり簡単になりました.

tell application "iTunes" to set theAlbum to album of item 1 of selection
set theDestination to "" --PATH
set targetImageFile to theDestination & ":" & theAlbum & ".png"
tell application "iTunes"
	set theTrack to selection
	repeat with i in theTrack
		set data of artwork 1 of i to (read (file targetImageFile) as picture)
	end repeat
end tell

これでアルバム全体にアートワークを差し込むことができます.

AppleScriptを使ってiTunesのアートワークを入手するまで

iTunesの”アートワークの入手”の精度があまりにも低い!!これはなんとかしなければということで,AppleScriptを用いてwebからアートワークを持ってくるものを作ってみました.JavaScriptのsrcを使って画像のソースを取得してurlのときとbase64のときで分けて処理しました.
Googleの画像検索でフルサイズの画像を入手する方法が分からなかったため解像度はよくありません.
いい方法がございましたら教えて頂けたら嬉しいです.

--事前にアートワークを入手したいアルバムをcommand+左クリックで選択しておく
tell application "iTunes"
	--set theSelctionAlbum to a reference to selection
	set theAlbum to item 1 of album of selection
	set theArtist to item 1 of album artist of selection
	set searchURL to "https://www.google.co.jp/search?q=" & theAlbum & "+" & theArtist & "&source=lnms&tbm=isch&sa=X" as string
	
	tell application "Google Chrome" to open location searchURL
	
	tell application "Google Chrome"
		--display dialog "Please Wait"
		
		delay 5
		tell window 1
			tell active tab
				loading_confirm(20) of me
				set theNumber to execute javascript "document.images.length"
				if theNumber > 30 then
					set theNumber to 30
				end if
				set theURL to ""
				set theFlag to 0
				repeat with i from 1 to theNumber
					try
						set theHeight to execute javascript "document.images[" & (i as string) & "].height"
						set theWidth to execute javascript "document.images[" & (i as string) & "].width"
						set theSrc to execute javascript "document.images[" & (i as string) & "].src"
						set theCounter to (count of theSrc)
						--display dialog theCounter
						if (theHeight > 200) and (theWidth > 200) and (theCounter > 500) then
							set theBase64 to theSrc
							set theFlag to 1
							if (count of theSrc) > (count of theBase64) then
								set theBase64 to theSrc
							end if
						else if (theHeight > 200) and (theWidth > 200) and (theCounter < 500) then
							set theURL to theSrc
							set theFlag to 2
						end if
					end try
				end repeat
			end tell
                               close active tab
		end tell
	end tell
end tell


set theDestination to "" --your path
set thePath1 to theDestination & "/" & theAlbum & ".txt"
set thePath2 to theDestination & "/" & theAlbum & ".png"
--base64の場合デコードを行う
if theFlag is 1 then
	set theBase64Ref to refTextByDelimiters(theBase64)
	
	tell application "Finder"
		set theTextFile to open for access thePath1 with write permission
		set theEOF to get eof of theTextFile
		try
			write theBase64Ref starting at (theEOF + 1) to theTextFile
		on error theErrorText
			
			display dialog theErrorText
		end try
		close access theTextFile
	end tell
	
	do shell script "base64 -D " & thePath1 & " -o " & thePath2
        do shell script "rm " & thePath1
else if theFlag is 2 then
	do shell script "curl -L " & theURL & " -o " & POSIX path of thePath2
else if theFlag is 0 then
	display dialog "Cannot find an artwork of " & theAlbum
end if



--文章をあるデリミタで区切る
on refTextByDelimiters(theText)
	set tmp to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	set refText to last text item of (theText as string)
	set AppleScript's text item delimiters to tmp
	return refText
end refTextByDelimiters

--タイムアウトの時間設定
on loading_confirm(time_out)
	delay 2
	repeat with i from 1 to the time_out
		tell application "Google Chrome"
			tell window 1
				tell active tab
					if (execute javascript "document.readyState") is "complete" then
						return true
					else if i is the time_out then
						return false
					else
						delay 1
					end if
				end tell
			end tell
		end tell
	end repeat
	return false
end loading_confirm

AppleScript(シェルスクリプト)でメインディスプレイとセカンドディスプレイのプライマリを変えるまで

普段はI-O DATAのディスプレイをメインに使っていますが,東方などをする際にはmacのディスプレイを使ったほうがやりやすいのでそうしてます.しかし,そのためだけに設定→ディスプレイ→プライマリの変更をするのは面倒臭いですね.そこでAppleScript {ほとんどシェルスクリプト(他人の力)}を使って解決してみました.今回使ったのはこちら
github.com

まず,下のコマンドを実行しメインディスプレイのときと,セカンドディスプレイのときの結果を控えておきます.

fb-rotate -l

あとはこれを実行すればディスプレイのプライマリが交互に入れ替わります.

set infoDis to (do shell script "PATH of fb-routate -l")
set theMac to "result of main display"
set theExternal to "result of second display"

if infoDis is theMac then do shell script " PATH of fb-routate -d <your main display ID> -m"
if infoDis is theExternal then do shell script " PATH of fb-rotate -d <your second display ID> -m"

iTunesの再生回数をapplescriptでリセットするまで

iTunesの前のバージョンでは再生回数のリセットが出来たのですが,最新のでは出来なくっていました.Applescriptを使えば割と手軽にリセットを行うことができます.
これでいつでも再生回数を0にできますね!!!!

tell application "iTunes"
	set theTrack to selection
	if theTrack is not {} then
		repeat with i in theTrack
			try
				set played count of i to 0
			end try
			try
				set played date of i to missing value
			end try
		end repeat
	end if
end tell

TwythonでTLを追従してお気に入りしていくまで

特定の人がつぶやくたびにツイートをお気に入りするものを興味本位で作ってみました.ほとんどtwythonの構文を組み合わせて作っただけなので,ここを見ていただいたら好きなことができると思いまする.
Developer Interface — Twython 3.4.0 documentation

#coding:utf-8
#!/usr/bin/env python
from twython import TwythonStreamer,Twython
class MyStreamer(TwythonStreamer):
	def on_success(self, data):
		if 'text' in data:
# ここにuser id を入れる.検索すれば調べることができます
			if data['user']['id'] ==  : 
				twitter.create_favorite(id = data['id'])
			def on_error(self, status_code, data): print status_code

#twitterの認証情報を入力
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

stream = MyStreamer(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
stream.user()

Twythonを使ってtweetをお気に入りするまで

人のTweetをファボりまくって楽しみたいそこのあなた!!raspberry pi とtwython を使えば簡単に実装できます.乱用は控えましょう.友達がいなくなります

手順は以下の通りです.

  1. Twitterのアプリを制作してからkeyなどを取得する(様々なサイトで紹介されているため省略)
  2. raspberry pi にtwythonを導入(省r)

簡単ですね.
コードはこのようになりました.

#coding:utf-8
#!/usr/bin/env python
import sys
from twython import Twython

argvs = sys.argv

#twitterの認証情報を入力
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''

twitter = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)
user_timeline = twitter.get_user_timeline(screen_name= argvs[1],count = 200,page = 4,include_retweets=False)
for tweet in user_timeline:
	if tweet['favorited'] == 0:
		twitter.create_favorite(id=tweet['id'])

保存した(自分はtweet_favorite.py)ディレクトリで下記を実行する.

python tweet_favorite.py id --ここにお気に入りしたい人のidを入れる

すると,先ほど入力したidの人のtweetの過去200件を一瞬にしてお気に入りに入れることができます.ただこれだけです.有用性なんて全くありません.
ただ対象のtweetがもともとお気に入りだとうまく動かないので,お気に入りされているかどうかチェックするようにしました.