Monday, June 29, 2009

Invoking Spotify playlist using applescript

Spotify has plenty of Finnish children's music available so it has gained specific popularity in our household. This has rendered my older iTunes-control -scripts not that useful anymore. I decided to give Spotify scripting a go, even though I knew there's not applescript interface available. Thankfully simple key navigation is enough for the basic use case of "play random song from this playlist":

-- Invoke Spotify simply by opening a Spotify playlist or artist URL

open location "http://open.spotify.com/artist/4Zml6rzsSiVkfIIWJh6EWX"

-- Make sure we have the spotify-application on front and in focus
tell application "Spotify"
activate
end tell

-- Now navigate to the song we want to play and play it
tell application "System Events"
-- Three times tabulator = move selection to the songs list
key code 48
key code 48
key code 48
-- Wait for a while the spotify app to catch up
delay (2)
-- enter key = play selected song
key code 36
-- right key with command = next song (random assumed)
key code 124 using command down
end tell
Note that if the Spotify URL is your private playlist this still works, as the playlist is invoked locally!

Other assumptions:
  • "enable access to assistive devices" is enabled on the "Universal Access" -settings
  • Spotify URL:s properly associated to your favourite browser (tested only with firefox)
Known issues:
  • Invoking a random song requires you to set the shuffle on manually on the Spotify app
  • Naturally this generates new browser tabs on each time the script is invoked
  • As the key events are used, and even with a noticeable wait in between, this can cause really weird results if something is happening onscreen either interactively or autonomously.
  • There is no straightforward way of knowing if Spotify is already playing something. Toggling the playback off, if invoked second time (useful when used with RFID tags) isn't therefore easy.. or is there a apparent solution other than closing the app?
As this works OK, I have to print out new RFID-tag for Ti-Ti -nalle for my daughters ;)

Again, the script is not really a rocket science and most of the elements were just copy&pasted from invaluable Doug's AppleScripts. It is just fun to simply being able to control other apps in OSX!

Tuesday, May 05, 2009

Changes, changes..

Looking back couple years there has been "one big change" happening in our life each year. Change of country, moving places, change of job, getting married, birth of children etc.

This year it will be significant change in my employment.

I am leaving Nokia this month, after six years, and I'm heading to a researcher position in Finnish Defence Forces! One can say that this will be a big change in company ways of working, scope and the whole technology context. On the other hand, I am looking forward applying some of my experience in this new envinronment in new ways and learning a lot at the same time.

I am looking back to my years at Nokia with warmth. It is a very good, big company to work for - providing lots of exciting challenges with lot of variety. I have also had the pleasure of constantly working with very intelligent people, many of which are the absolute world class top professionals in their fields.

Now, maybe I can have a bit more opportunities for doing mobile programming as a hobby as it is not filling 110% of my work days? Well, you never know.. ;-)

Saturday, March 07, 2009

Simple applescript for playing music in iTunes

In addition to make skype video calls I thought it would be nice to give my kids some control over playing music on the iMac. So, a new rfid tag was assigned to following simple applescript:

tell application "iTunes"
if player state is playing then
stop
else
set the view of the front browser window to playlist "kertun musaa"
set myNumberOfTracks to number of tracks in playlist "kertun musaa"
set randomTrack to (random number (myNumberOfTracks - 1)) + 1
play track randomTrack of user playlist "kertun musaa"
end if
end tell

So when the tag is shown to the reader the script either
  • stops the playback if itunes is playing a something, or
  • if nothing is playing, starts a play a random song in a named playlist
The script would've been even more simple if iTunes would allow setting the current playlist. Then I could've used the native shuffle mode of the player.

Using applescript one can do almost anything on iTunes. A good source for further information is http://dougscripts.com/itunes/

Friday, March 06, 2009

RFID "calling cards" for OSX and Skype

I made a simple "calling cards" for my daughter(s) which enable her to make a Skype video call to me and to her grandparents without using keyboard and mouse. In order to make the call she would just show the card with right image to the reader. Computer would respond by saying aloud either "calling grandma" or "cannot make the call to grandma", depending whether the other party is online or not.

cheap rfid-reader and "calling cards"

Setup:
  • iMac with Skype installed
  • Tikitag/Touchatag rfid reader. Bought the reader and some tags for about 12€ (75% discount) when the company changed its name.
  • Applescripts for calling to grandmas and myself at work, saved as application. The scripts check if the target is online, gives voice feedback using speech synthesizer, and makes the call with the window maximized.
  • Rfid tags assigned with local URL:s at touchatag service. The local url is simply a link to the application to start when the tag is shown to the reader. As example, an URL to "call my work skype app" is
    file:///Applications/as/call_simo.app
  • Pictures in see-through plastic pouches. The Rfid tag is put behind the image. One could also e.g. laminate the cards.
I have also experimented having Nokia N770 internet tablet next to the reader showing visually who is online and who isn't. Having the extra device on the table did make the setup look a bit complex. Let's see with the target audience if is it needed or not. ;-)

Controlling a computer via non-traditional methods feels nice. If the setup works reliably and the actual interaction makes sense I will definitely expand the use to playback of some favourite music etc.

OK, nice hack but will it be used for real? We see it next week when the family comes back home ;)

EDIT: Added the example of a local URL

Thursday, March 05, 2009

Learning applescript: controlling skype

While I've always known that you can control many of the applications running in OSX using applescript I have very rarely tried that myself.

Now, as I have a use case of semi-automation of skype video calling for the kids I started experimenting. I did buy a tikitag rfid reader on cheap which will be used for triggering the call.

Here's what will be triggered by the rfid tag. This simple script checks if person xxxxxx is online and tries to open a call if he is. At the same time the script enlarges the call window to full screen for maximum video size. I have added some text-to speech notifications just for sake of experimenting, most probably I will replace them later with recorded samples.

Edit: Updated the script a bit and framed it in

SkypeCall("xxxxxx")

on SkypeCall(login)
set AppleScript's text item delimiters to " "
tell application "Skype"
set foo to send command "GET USER " & login & " ONLINESTATUS" script name "MyScript"
set UserStatus to my GetPart(foo, 4, " ")
if UserStatus is "ONLINE" then
say "soitan"
send command "CALL " & login script name "MyScript"
copy (run script "tell application \"Finder\" to desktop's window's bounds") to bounds of window 1
else
say "[a] onnnnissstu!"
end if
end tell
end SkypeCall

on GetPart(s, p, d)
set theList to every text item of s
return (item p of theList)
end GetPart
I used this macoshints article as an example.

Friday, January 23, 2009

New s60 freeware: deli.s

In the series of small s60 utilities I have made for myself: deli.s is an application which helps the user to post links to http://del.icio.us from the handset.

I was tired of copy-pasting URLs and using numerous dialogs. With deli.s you can send a link to delicious just by selecting the right bookmark and adding the tags and extra description you want.

As a piece of software made just for myself, it is a bit rough on the edges with known and unknown bugs and limitations. Decided to share this anyway as I bet I am not the only one that needed something like this.

Download and check it out from the following page:

http://huopio.fi/simo/software/delis/

Any comments and suggestions are appreciated. I am not promising quick turnaround on new features - as I am coding this only when commuting on a bus ;).