<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erik A. Hanson &#187; AppleScript</title>
	<atom:link href="http://www.eahanson.com/category/applescript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eahanson.com</link>
	<description>My weblog</description>
	<lastBuildDate>Thu, 06 Jan 2011 02:27:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AppleScripts for Creating Podcast Playlists in iTunes</title>
		<link>http://www.eahanson.com/2009/03/16/applescripts-for-creating-podcast-playlists-in-itunes/</link>
		<comments>http://www.eahanson.com/2009/03/16/applescripts-for-creating-podcast-playlists-in-itunes/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 03:02:10 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[My Software]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/?p=70</guid>
		<description><![CDATA[I have a 2nd generation iPod Shuffle that I use with the super-awesome Arriva iPod headphones: I love these headphones because I can wear them at the gym without having to deal with wires. I actually bought the shuffle because of these headphones. At the gym, I like to start out by listening to a [...]]]></description>
			<content:encoded><![CDATA[<p>I have a <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26rs%3D172282%26ref%255F%3Dsr%255Fnr%255Fn%255F2%26keywords%3Dapple%2520ipod%2520shuffle%2520%25282nd%2520Generation%2529%26bbn%3D172623%26qid%3D1237257133%26rnid%3D493964%26rh%3Di%253Aaps%252Ck%253Aapple%2520ipod%2520shuffle%2520%25282nd%2520Generation%2529%252Ci%253Aelectronics%252Cn%253A172282%252Cp%255F4%253AApple%252Cn%253A172623&#038;tag=wshlst-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=390957">2nd generation iPod Shuffle</a><img src="https://www.assoc-amazon.com/e/ir?t=wshlst-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> that I use with the super-awesome <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref%255F%3Dnb%255Fss%255Fgw%26y%3D0%26field-keywords%3Darriva%2520ipod%2520shuffle%26url%3Dsearch-alias%253Daps&#038;tag=wshlst-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=390957">Arriva iPod headphones</a><img src="https://www.assoc-amazon.com/e/ir?t=wshlst-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />:<br />
<a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref%255F%3Dnb%255Fss%255Fgw%26y%3D0%26field-keywords%3Darriva%2520ipod%2520shuffle%26url%3Dsearch-alias%253Daps&#038;tag=wshlst-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=390957"></p>
<p><img src="http://www.eahanson.com/weblog/wp-content/uploads/2009/03/arriva.jpg" alt="arriva" title="arriva" width="199" height="145" class="aligncenter size-full wp-image-71" /></a></p>
<p>I love these headphones because I can wear them at the gym without having to deal with wires. I actually bought the shuffle because of these headphones. At the gym, I like to start out by listening to a few news podcasts while I warm up and then I like to hear some random music.</p>
<p>Unfortunately, version 8.1 of Apple&#8217;s iTunes removed the ability to autofill podcasts on the shuffle. (See <a href="http://discussions.apple.com/thread.jspa?threadID=1937830&#038;tstart=0">this thread on Apple&#8217;s support page</a> for example.)</p>
<p>Luckily, it can still be done with AppleScript. Here&#8217;s a script that copies all of the songs from my &#8220;Gym&#8221; playlist, including podcasts, to my shuffle:</p>
<pre>
tell application "iTunes"
  repeat with thisSource in sources
    if the name of thisSource = "Erik's Shuffle" then set myIpod to thisSource
  end repeat

  set destinationPlaylist to the first playlist in myIpod
  delete tracks in destinationPlaylist

  set sourcePlaylist to playlist "Gym"
  if the number of tracks in sourcePlaylist is greater than 0 then
    set shufflable of (every track of sourcePlaylist) to true
    duplicate every track of sourcePlaylist to destinationPlaylist
  end if
end tell
</pre>
<p>And here&#8217;s the full script that I use these days. It deletes everything on the shuffle, then adds the contents of three of my podcast playlists (which are &#8220;smart playlists&#8221; that only contain the most recent unplayed episode), and then adds one song from a short list of songs I like to start my workout with, and then finally adds 25 random songs from a collection of music that&#8217;s suitable for the gym.</p>
<pre>
on addRandomSongs(sourcePlaylistName, destinationPlaylist, songCount)
  tell application "iTunes"
    set sourcePlaylist to playlist sourcePlaylistName
    set alreadyUsedTrackNumbers to {}
    set numberOfTracksInSourcePlaylist to the number of tracks in sourcePlaylist
    repeat songCount times
      set trackNumber to (random number from 1 to numberOfTracksInSourcePlaylist)
      if alreadyUsedTrackNumbers does not contain trackNumber then
        duplicate (track trackNumber of sourcePlaylist) to destinationPlaylist
        copy trackNumber to the end of alreadyUsedTrackNumbers
      end if
    end repeat
  end tell
end addRandomSongs

on addPodcast(sourcePodcastName, destinationPlaylist)
  tell application "iTunes"
    set sourcePodcast to playlist sourcePodcastName
    if the number of tracks in sourcePodcast is greater than 0 then
      set shufflable of (every track of sourcePodcast) to true
      duplicate every track of sourcePodcast to destinationPlaylist
    end if
  end tell
end addPodcast

on findDevice(deviceName)
  tell application "iTunes"
    repeat with thisSource in sources
      if the name of thisSource = deviceName then return thisSource
    end repeat
  end tell
end findDevice

--
-- begin
--

set myShuffle to findDevice("Erik's Shuffle")

-- delete all tracks from the shuffle
tell application "iTunes"
  set shufflePlaylist to the first playlist in myShuffle
  delete tracks in shufflePlaylist
end tell

-- add podcasts
addPodcast("NPR Hourly News Summary", shufflePlaylist)
addPodcast("California Report", shufflePlaylist)
addPodcast("NPR Story Of The Day", shufflePlaylist)

-- add songs
addRandomSongs("Gym Kickoff", shufflePlaylist, 1)
addRandomSongs("Gym Music", shufflePlaylist, 25)
</pre>
<p>To use either of these scripts, fire up ScriptEditor, paste the script in, choose &#8220;Save As&#8221; from the &#8220;File&#8221; menu and choose &#8220;Application&#8221; as the file format, and then save it in your Library/Scripts/Applications/iTunes folder. A script menu will appear in the right portion of your menu bar when you&#8217;re in iTunes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2009/03/16/applescripts-for-creating-podcast-playlists-in-itunes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>An AppleScript to Clean Up My Downloads Folder</title>
		<link>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/</link>
		<comments>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/#comments</comments>
		<pubDate>Sat, 11 Aug 2007 18:55:02 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/</guid>
		<description><![CDATA[My downloads folder always gets so full after a while that I have a hard time finding things. I like to keep stuff around for a little while in case I need it again, and if I have the space, I like to keep things around for a long time, just in case. So I wrote a simple AppleScript to move old files to an "Old" subdirectory.]]></description>
			<content:encoded><![CDATA[<p>My downloads folder always gets so full after a while that I have a hard time finding things. I like to keep stuff around for a little while in case I need it again, and if I have the space, I like to keep things around for a long time, just in case.</p>
<p>So I wrote a simple AppleScript to move old files to an &#8220;Old&#8221; subdirectory. I thought I&#8217;d post it here in case anyone else needed something similar. It&#8217;s a simple script, but AppleScript is hard for me because I write scripts so rarely that I forget everything about it before I need to write another script.</p>
<p>(Many years ago, I actually got paid to write a few AppleScripts.)</p>
<pre>
set folderName to "Macintosh HD:Users:<em>your-user-name</em>:Temp:"

repeat with fileName in list folder folderName
  set tempFile to (folderName as string) &amp; contents of fileName
  set modificationDate to modification date of (info for alias tempFile)
  set age to ((current date) - modificationDate) / days

  if age &gt; 7 and fileName is not " Clean Up Temp Dir" then
    tell application "Finder"
      move alias tempFile to alias (folderName &amp; " Old")
    end tell
  end if
end repeat
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.eahanson.com/2007/08/11/an-applescript-to-clean-up-my-downloads-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

