<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Bending iTunes to my will with RubyOSA: take 1 Comments</title>
  <id>tag:www.opensoul.org,2009:/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1/comments</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://www.opensoul.org/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1" rel="alternate" type="text/html"/>
  <updated>2007-09-16T23:36:33Z</updated>
  <entry xml:base="http://www.opensoul.org/">
    <author>
      <name>Federico Zagarzaz&#250;</name>
    </author>
    <id>tag:www.opensoul.org,2007-06-30:3157:3727</id>
    <published>2007-09-16T23:36:33Z</published>
    <updated>2007-09-16T23:36:33Z</updated>
    <category term="Code"/>
    <link href="http://www.opensoul.org/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1" rel="alternate" type="text/html"/>
    <title>Comment on 'Bending iTunes to my will with RubyOSA: take 1' by Federico Zagarzaz&#250;</title>
<content type="html">&lt;p&gt;Hi,&lt;/p&gt;


	&lt;p&gt;Just to let you know that you can also use CocoaDialog for displaying confirmations, this is what I use in tagloko&lt;a href=&quot;music tagger&quot;&gt;1&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;[1] tagloko.rubyforge.org&lt;/p&gt;


	&lt;p&gt;Bye, thanks for the info,&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;Federico&lt;/code&gt;&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://www.opensoul.org/">
    <author>
      <name>has</name>
    </author>
    <id>tag:www.opensoul.org,2007-06-30:3157:3168</id>
    <published>2007-07-03T22:01:27Z</published>
    <updated>2007-07-03T22:01:27Z</updated>
    <category term="Code"/>
    <link href="http://www.opensoul.org/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1" rel="alternate" type="text/html"/>
    <title>Comment on 'Bending iTunes to my will with RubyOSA: take 1' by has</title>
<content type="html">&lt;p&gt;&#8220;1. I can’t figure out how to delete and add the track through RubyOSA. There is a delete method on the track, but doesn’t appear to do anything.&#8221;&lt;/p&gt;


	&lt;p&gt;I don&#8217;t know about RubyOSA, but to add a file to iTunes using rb-appscript (example code):&lt;/p&gt;


&lt;pre&gt;
require 'appscript'
include Appscript

Itunes = app('iTunes')

Itunes.add(MacTypes::Alias.path('/Users/foo/lizard scum.mp3'))
&lt;/pre&gt;

	&lt;p&gt;That&#8217;ll add it to your main library only. If you want to add it to a user playlist as well, specify that playlist via the optional &#8216;to&#8217; parameter:&lt;/p&gt;


&lt;pre&gt;
Itunes.add(MacTypes::Alias.path('/Users/foo/lizard scum.mp3'), :to=&amp;gt;app.playlists['MyList'])
&lt;/pre&gt;

	&lt;p&gt;To delete a track from a user playlist (example code):&lt;/p&gt;


&lt;pre&gt;
track_ref = Itunes.playlists['MyList'].tracks['Goodbye you lizard scum']
track_ref.delete
&lt;/pre&gt;

	&lt;p&gt;If you want to delete the track completely from your library, you&#8217;ll need to reference it there:&lt;/p&gt;


&lt;pre&gt; 
Itunes.library_playlists[1].tracks['Goodbye you lizard scum'].delete
&lt;/pre&gt;

	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;BTW&lt;/span&gt;, if you want to delete a track completely but all you&#8217;ve got is a reference to it in a user playlist, get its database ID and then use a &#8216;whose&#8217; clause to locate that track in the main library:&lt;/p&gt;


&lt;pre&gt;
user_playlist_track_ref = Itunes.playlists['MyList'].tracks['Goodbye you lizard scum']

db_id = user_playlist_track_ref.database_ID.get
library_track_ref = Itunes.library_playlists[1].tracks[its.database_ID.eq(db_id)].first.get&lt;/pre&gt;

	&lt;p&gt;You can then call #delete on that reference to delete the track completely.&lt;/p&gt;


	&lt;p&gt;&#8220;2. I want the script to display a dialog confirming the actions about to take place.&#8221;&lt;/p&gt;


	&lt;p&gt;In appscript:&lt;/p&gt;


&lt;pre&gt;
#!/usr/local/bin/ruby

require 'osax'

standard_additions = OSAX.osax

standard_additions.display_dialog('Hello world!')
&lt;/pre&gt;

	&lt;p&gt;If you want the dialog to appear in a specific application, e.g. iTunes:&lt;/p&gt;


&lt;pre&gt;
standard_additions = OSAX.osax.by_name('iTunes')

standard_additions.activate
standard_additions.display_dialog('Hello world!')
&lt;/pre&gt;

	&lt;p&gt;&#8220;3. I want to be able to execute the script from the Scripts menu in iTunes, which appears to involve installing &lt;span class=&quot;caps&quot;&gt;OSA&lt;/span&gt; Components.&#8221;&lt;/p&gt;


	&lt;p&gt;I don&#8217;t think that&#8217;ll work &#8211; in order to send Apple events to the host application, you need an &lt;span class=&quot;caps&quot;&gt;OSA&lt;/span&gt; component that provides integrated support for this, which the RubyOSA component (no relation to the RubyOSA Apple event bridge) doesn&#8217;t. The only third-party components that currently do this are JavaScriptOSA (although it has a number of other flaws that limit its usefulness) and PyOSA (which is still under development and not quite ready for general use).&lt;/p&gt;


	&lt;p&gt;The simplest solution would be to put your script into the system-wide Script Menu (which accepts shell scripts as well as AppleScripts, so no need for an &lt;span class=&quot;caps&quot;&gt;OSA&lt;/span&gt; component). Use /Applications/AppleScript/AppleScript Utility.app to enable it if you&#8217;ve not already done so, then put your .rb script in ~/Library/Scripts.&lt;/p&gt;


	&lt;p&gt;Alternatively, write an AppleScript that uses &#8216;do shell script&#8217; to start your .rb script and then detach the ruby process so that the &#8216;do shell script&#8217; command returns immediately.&lt;/p&gt;


	&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;HTH&lt;/span&gt;&lt;/p&gt;


&lt;h2&gt;has&lt;/h2&gt;	&lt;p&gt;http://appscript.sourceforge.net
http://rb-appscript.rubyforge.org&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://www.opensoul.org/">
    <author>
      <name>Conrad</name>
    </author>
    <id>tag:www.opensoul.org,2007-06-30:3157:3158</id>
    <published>2007-07-01T06:28:47Z</published>
    <updated>2007-07-01T06:28:47Z</updated>
    <category term="Code"/>
    <link href="http://www.opensoul.org/2007/6/30/bending-itunes-to-my-will-with-rubyosa-take-1" rel="alternate" type="text/html"/>
    <title>Comment on 'Bending iTunes to my will with RubyOSA: take 1' by Conrad</title>
<content type="html">&lt;p&gt;I think the reason you can&#8217;t delete the track is because you do a get&lt;/p&gt;


	&lt;p&gt;itunes.selection.get.each do |track|&lt;/p&gt;


	&lt;p&gt;This converts the specifier list object (i.e. reference to the tune in iTunes) to an actual &lt;span class=&quot;caps&quot;&gt;MP3&lt;/span&gt;, which I think is no longer connected to iTunes.&lt;/p&gt;


	&lt;p&gt;If you did&lt;/p&gt;


	&lt;p&gt;itunes.selection.each do |track|&lt;/p&gt;


	&lt;p&gt;then I think you would be able to do itunes.delete(track) &lt;span class=&quot;caps&quot;&gt;BUT I&lt;/span&gt; think you might then have problem with the &lt;span class=&quot;caps&quot;&gt;ID3&lt;/span&gt; tags :-)&lt;/p&gt;


	&lt;p&gt;There is some info on this sort of thing here&lt;/p&gt;


	&lt;p&gt;http://rubyosa.rubyforge.org/guide/object-specifiers.html&lt;/p&gt;</content>  </entry>
</feed>
