読者です 読者をやめる 読者になる 読者になる

りんごの備忘録

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

AppleScriptで聞いている曲をTwitterに呟くまで

AppleScript

Twitterで今聞いてる曲を#nowplayingとして呟きたい!!でもサードーパーティーで最適なアプリケーションが見つからない.それならば作ってしまえばいいのだ,ということでAppleScriptを使って作ってみた.
確実に前回の記事と順番を間違えた
今回もPashuaと
www.bluem.net
Twitter Scripterを使わせてもらいました
www.mousedown.net
Pashuaをダウンロード,解凍すると"Pashua.scpt"と"Pashua.app"の二つを探してApplicationのフォルダにぶち込んでおく

Pashua.scptをそのまま使うと不具合が起こる場合があるので下記の部分を修正しておく(原因は調べた気がするが忘れた)

tell application "Finder" to delete tmpfile

                      ⬇︎

tell application "Finder"
	delete tmpfile
end tell

PashuaとTwitterScripterを組み合わせるとこのようになった.Pictureフォルダにはアートワークがない場合の別の画像を用意しておいた.(名前はcover_not.png)

global sendText
tell application "iTunes"
	set theArtist to artist of current track
	set theAlbum to album of current track
	set theName to name of current track
	set sendText to "♪" & theArtist & "/" & theName & " /" & theAlbum & " #nowplaying" as text
	if (count of artwork of current track) ≥ 1 then
		set artworkExists to true
		set anArtwork to raw data of artwork 1 of current track
		tell application "iTunes" to tell artwork 1 of current track
			set d to raw data
			if format is «class PNG » then
				set x to "png"
			else
				set x to "jpg"
			end if
		end tell
		(((path to pictures folder) as text) & "cover." & x)
		set b to open for access file result with write permission
		set eof b to 0
		write d to b
		close access b
	else
		set artworkExists to false
	end if
end tell

tell application "Finder"
	set appPath to (path to applications folder)
	set thisFolder to appPath as string
	if "Pashua:Pashua.app:" exists then
		-- Looks like the Pashua disk image is mounted. Run from there.
		set customLocation to "Pashua:"
	else
		-- Search for Pashua in the standard locations
		set customLocation to ""
	end if
end tell

try
	set thePath to alias (thisFolder & "Pashua.scpt")
	set pashuaBinding to load script thePath
	
	tell pashuaBinding
		-- Display the dialog
		
		try
			set pashuaLocation to getPashuaPath(customLocation)
			
			if artworkExists is true then
				set dialogConfiguration to my getDialogConfiguration((((path to pictures folder) as text) & "cover." & x))
			else
				set dialogConfiguration to my getDialogConfiguration((((path to pictures folder) as text) & "cover_not.png"))
				
			end if
			
			set theResult to showDialog(dialogConfiguration, customLocation)
			if {} = theResult then
				
			else if cb of theResult is not "1" then
				tell application "Twitter Scripter"
					if artworkExists is true then
						tweet sendText & return & tf of theResult using account pop of theResult with image (((path to pictures folder) as text) & "cover." & x)
					else if artworkExists is false then
						tweet sendText & return & tf of theResult using account pop of theResult
					end if
				end tell
				
			end if
		end try
	end tell
end try


-- Returns the configuration string for an example dialog
on getDialogConfiguration(pashuaLocation)
	
	if pashuaLocation is not "" then
		set img to "img.type = image
	          img.x = 320
	          img.y = 90
			  img.maxwidth = 128
			  img.tooltip = Album Art
	          img.path =" & (POSIX path of pashuaLocation) & return
	else
		set img to ""
	end if
	
	return "
# Set window title
*.title = Currently playing...

# Introductory text
txt.type = text
txt.label = Track/Album Info
txt.default =" & sendText & return & "
txt.height = 10
txt.width = 300
txt.x = 0
txt.y = 160
#txt.tooltip = This is an element of type “text”

# Add a cancel button with default label
cb.type = cancelbutton
cb.label = Cansel

# Add a text field
tf.type = textfield
tf.label = 
tf.default = 
tf.width = 300
tf.tooltip = This is an element of type “textfield”

# Add a popup menu
pop.type = popup
pop.label = Tweet Account
pop.width = 300
pop.option = 
pop.option = 
pop.default = 
pop.tooltip = This is an element of type “popup”

db.type = defaultbutton
db.label = Tweet
" & img
end getDialogConfiguration