运用AppleScript分享nowplaying

嗯,玩儿G+玩儿多了,看到好多菊苣时不时分享一个#nowplaying出来,分享自己正在聆听的音乐,于是想自己搞个东西把#nowplaying自动搞到剪切板里面(直接分享什么的G+不开api啊魂淡!

于是想用cocoa写个小app,无奈刚开始学习cocoa,水平太渣,于是转向applescript。

本来只是写了一段很简陋的代码,只有复制当前曲目名称到剪切板的功能,不到10行,不过G+上的菊苣Haoyu Wen表示这太弱了,然后就给随手改了OTZ。配合Apollo Wayne菊苣的音乐分享服务,可以分享到tw,G+,fb和人人上面ლ(╹◡╹ლ)

效果图~

nowplaying

下面贴代码( > ◡╹)

1
tell application "iTunes"
	set trackName to name of current track
	set artistName to artist of current track
	set albumname to album of current track
	set timename to time of current track
end tell

on replaceChars(aString, charToReplace, newChar)
	set {TID, text item delimiters} to {text item delimiters, charToReplace}
	set aString to text items of aString
	set text item delimiters to newChar
	set aString to aString as text
	set text item delimiters to TID
	return aString
end replaceChars

set outputUrl to replaceChars("http://music.wayshine.us/nowplaying.php?album=" & albumname & "&artist=" & artistName & "&title=" & trackName & "&length=" & timename & "&from=233" & "&c=0", " ", "%20")


tell application "Safari"
	activate
	try
		tell window 1 to set current tab to make new tab with properties {URL:outputUrl & "&s=0"}
	on error
		open location outputUrl & "&s=0"
	end try
end tell

set the clipboard to outputUrl & "&s=3"

接下来是关于如何运用的教程,首先,打开OS X自带的automator,选择新建服务

service

接着在Uitilities菜单里面找到Run AppleScript(中文应该是实用工具和运行AppleScript),拖入到右侧空白区域中,将上面的代码复制进去,并将上面的Service receives选择为“no input”(图中用红框标出部分)

edit

选择自己喜欢的名字保存,然后在系统偏好设置→键盘→键盘快捷键里选择服务,找到自己刚才创建的那个,设置快捷键~(注意快捷键的冲突问题)

setting

然后按下设定的快捷键,分享网页自动打开,剪切板里面也是分享网页的url,分享到各大SNS吧~

share

PS:Chrome用户可以看这里

12.18更新,支持侦测iTunes是否运行