<?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>Ash Searle's Blog &#187; AppleScript</title>
	<atom:link href="http://hexmen.com/blog/category/applescript/feed/" rel="self" type="application/rss+xml" />
	<link>http://hexmen.com/blog</link>
	<description>On programming, and other things...</description>
	<lastBuildDate>Mon, 03 Jan 2011 16:21:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>AppleScript &#8211; a frustrating&#160;beginning</title>
		<link>http://hexmen.com/blog/2009/04/applescript-a-frustrating-beginning/</link>
		<comments>http://hexmen.com/blog/2009/04/applescript-a-frustrating-beginning/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:01:10 +0000</pubDate>
		<dc:creator>Ash</dc:creator>
				<category><![CDATA[AppleScript]]></category>

		<guid isPermaLink="false">http://hexmen.com/blog/?p=84</guid>
		<description><![CDATA[Academia and professional work has exposed me to a few different programming paradigms: functional, logical, procedural and object-orientated. I usually get along fine with a new paradigm, starting off with a few example programs and hacking away at them to get a decent grasp of the language. But, I&#8217;m not getting anywhere with AppleScript &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Academia and professional work has exposed me to a few different programming paradigms: functional, logical, procedural and object-orientated.  I usually get along fine with a new paradigm, starting off with a few example programs and hacking away at them to get a decent grasp of the language.  But, I&#8217;m not getting anywhere with AppleScript &#8211; it&#8217;s a frustrating beastie!</p>
<p>I think the root cause is the documentation.  A quick read of <a href="http://en.wikipedia.org/wiki/AppleScript">wikipedia&#8217;s page on AppleScript</a> shows you you&#8217;re dealing with a <em>natural language</em>.  AppleScript statements read like english sentences with padding words like &#8220;the&#8221; being optional, for example: <code>tell application "Finder" to say the name of the front Finder window as string</code>.</p>
<p>Maybe I&#8217;m being mislead because I&#8217;m holding onto <a href="http://www.apple.com/support/tiger/">Tiger</a> while waiting for <a href="http://www.apple.com/macosx/snowleopard/">Snow Leopard</a> &#8211; I can&#8217;t see why you&#8217;d need to use <code>as string</code> at the end of the example given?  A Finder window&#8217;s name property is documented as unicode text and yet I need to explicitly convert it to a string (or equivalently and for no good reason <code>as text</code> works too).  It&#8217;s rubbish!</p>
<p>Accesing properties is a little odd too;  I&#8217;m not even sure whether AppleScripts trying to be object-oreiented or not. We can rewrite the example to use a posessive <code>'s</code> instead of the <code>of</code> operator.  e.g. <code>tell app "Finder" to say front Finder window's name as text</code> &#8211; which is nice.  But sometimes it works, and sometimes it doesn&#8217;t.   (note: I arbitrarily abbreviated <code>application</code> to <code>app</code>, but I&#8217;ve no idea where to find a full list of supported abbreviations&#8230;!)</p>
<p>I don&#8217;t grok the Script Editor at all.  You can easily inspect the command, classes and properties supported by an application, but where the hell&#8217;s the core language documentation?  Where do you go to find out &#8211; when it comes to strings &#8211; <code>count</code> is a method, but <code>length</code> is a property?  <code>count "some string"</code>  is OK, but <code>count <em>of</em> "some string"</code> isn&#8217;t!  How do I find a list of all properties of the AppleScript class &#8211; or any other arbitrary class, record or structure?</p>
<p>In the days and weeks ahead I hope to progress to such <em>advanced</em> topics as:</p>
<ul>
<li>writing arbitrary text to standard output (<a href="http://groups.google.com/group/alt.comp.lang.applescript/browse_thread/thread/d6fd2cd13927d5b3/5cba6813346a1750">this doesn&#8217;t work</a> and I&#8217;d like to write to standard output whenever I need to, not solely at the end of the program.)</li>
<li>checking a file exists (without using Finder or throwing an error)</li>
<li>list files in a directory (without using <code>do shell script</code> or using Finder)</li>
<li>get the urls of all tabs of all windows of safari (<code>tell app "Safari" to get URL of every document</code> only shows one URL per window, and my natural language instinct to use <code>every document of every tab of every window</code> falls flat on its face&#8230;)</li>
<li>access the <code>/dev</code> directory (<code>POSIX file "/usr" as alias</code> is fine, <code>POSIX file "<strong>/dev</strong>" as alias</code> throws an error!  WTF!?)</li>
</ul>
<p>To end on a semi-useful note, here are some very basic things I&#8217;ve discovered while developing AppleScript&#8217;s in a terminal window:</p>
<dl>
<dt>Determine AppleScript version number</dt>
<dd><code>$ osascript -e "AppleScript's version"</code></dd>
<dt>Show current track name from iTunes</dt>
<dd>(Note: I&#8217;m coming round to the idea that using <code>$'...'</code> is the best way of wrapping the applescript on a bash command-line &#8211; it allows you to backslash escape single-quotes and keep your double-quotes handy for literal strings.)<br />
<code>$ osascript -e $'tell application "iTunes" to get current track's name'</code>
</dd>
</dl>
<p>(you&#8217;d think there&#8217;d be more&#8230;)</p>
<p>I&#8217;ll follow up with another AppleScript article once I&#8217;ve mastered the art of controlling iTunes to extract artwork and iterate over albums and artists.</p>
]]></content:encoded>
			<wfw:commentRss>http://hexmen.com/blog/2009/04/applescript-a-frustrating-beginning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

