<?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>Mac Developer Tips &#187; AppleScript</title>
	<atom:link href="http://MacDeveloperTips.com/tag/applescript/feed" rel="self" type="application/rss+xml" />
	<link>http://MacDeveloperTips.com</link>
	<description>Tips, tools and code for iPhone and Mac developers.</description>
	<lastBuildDate>Thu, 12 Aug 2010 23:45:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Toggle Finder Hidden File Status</title>
		<link>http://MacDeveloperTips.com/applescript/toggle-finder-hidden-file-status.html</link>
		<comments>http://MacDeveloperTips.com/applescript/toggle-finder-hidden-file-status.html#comments</comments>
		<pubDate>Thu, 01 May 2008 06:09:14 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[finder]]></category>
		<category><![CDATA[hidden files]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/applescript/toggle-finder-hidden-file-status.html</guid>
		<description><![CDATA[AppleScript code to toggle whether or not Finder on Mac OS X shows hidden files.]]></description>
			<content:encoded><![CDATA[<p>For some reason I prefer to have system files (hidden by default) shown in Finder (must be the Unix in my system from grad school). With that said, there are times when it would be nice to have these same files out of sight when working with a cluttered folder. After having thought many times about writing a short script to toggle hidden files on/off, I finally took a few minutes to crank out the code below:</p>
<p>Script Editor Click to paste code into the Script Editor</p>
<pre>
----------------------------------------
--  MacDeveloperTips.com
--  Toggle Finder hidden file status
----------------------------------------
try
  -- Get current value
  set toggle to do shell script "defaults read com.apple.finder AppleShowAllFiles"

  -- Toggle it
  if toggle = "ON" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
  else
    do shell script "defaults write com.apple.finder AppleShowAllFiles ON
  end if

  -- Restart Finder
  tell application "Finder" to quit
  delay 0.5 -- If you have problems, you can tweak the delay
  tell application "Finder" to launch
on error

  display dialog "Unable to toggle bit status." buttons {"Better luck next time"}
    with icon caution with title "Error"
end try
</pre>
<p>Notice the reference to delay &#8211; if you have problems with the scripting not running properly, you can experiment with a longer delay. There is minimal error handling, essentially just trapping errors and displaying the message below:</p>
<p>I prefer to save AppleScript code as an application, create an icon that serves as a reminder of what the script does, and drag/drop the application onto the Finder toolbar. You can see a screenshot below what my toolbar looks like (the icon for this script is the blue &#038; white Finder image).</p>
<p>And speaking of icons, in an upcoming tip I&#8217;ll show you how easy it is to change an application icon on a Mac.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/applescript/toggle-finder-hidden-file-status.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>NetBeans, Java, AppleScript and iTunes &#8211; Part 2</title>
		<link>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-2.html</link>
		<comments>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-2.html#comments</comments>
		<pubDate>Tue, 29 Apr 2008 12:03:45 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[screencast]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-2.html</guid>
		<description><![CDATA[Learn how to interact with iTunes using AppleScript and Java.]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://macdevelopertips.com/java/netbeans-java-applescript-and-itunes-part-1.html" target="_blank">part 1 of this series</a> I demonstrated how to create a short Java application in NetBeans that communicated, using AppleScript, to the Mac apple event system. The application was a no thrills look into how to invoke the TextEdit application. Despite the brevity of the application it provided the foundation for this next tip, which will build on the ideas to create something a little more salient, as in, something that you might actually find yourself using.</p>
<p>The gist of the application is to instruct iTunes to play a random song, move to the next song, pause, etc. I&#8217;ll show one use of the application by wrapping the code into a bash script that allows you to invoke the program (and all commands) from within a terminal. Watch the video that follows for all the details&#8230;</p>
<p><a href="javascript:lynkVideoPop(719,'1209472338');" class="lynkvp"><img src="http://macdevelopertips.com/wp-content/uploads/images/NetBeans-Tips-Intro-Click-Here.png" /></a></p>
<p>This application is all of about 80 lines, including the bash script. Sometimes a little creative thinking and a few lines of code are all that&#8217;s needed to write an intriguing (and hopefully useful) application.</p>
<hr color="#00008b" size="1" /><img src="/images/blank.gif" border="0" height="1" />The music in the video is Led Zeppelin and the song: Moby Dick. <a href="http://www.amazon.com/gp/product/B000002J03?ie=UTF8&amp;tag=macdevelopertips-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=B000002J03" target="_blank">Led Zeppelin at Amazon</a><br />
<img src="/images/blank.gif" border="0" height="3" /><br />
<img src="http://www.assoc-amazon.com/e/ir?t=macdevelopertips-20&amp;l=as2&amp;o=1&amp;a=B000002J03" style="border: medium none  ! important; margin: 0px ! important" border="0" height="1" width="1" /><br />
<img src="/images/blank.gif" border="0" height="3" /></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NetBeans, Java, AppleScript and iTunes &#8211; Part 1</title>
		<link>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-1.html</link>
		<comments>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-1.html#comments</comments>
		<pubDate>Thu, 24 Apr 2008 11:40:57 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[screencast]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-1.html</guid>
		<description><![CDATA[Part 1 of a series of screencasts working with NetBeans, Java and AppleScript to interact with iTunes on Mac OS X]]></description>
			<content:encoded><![CDATA[<p>A few weeks back I <a href="http://macdevelopertips.com/tools-utilities/netbeans-61-ruby-and-applescript.html" target="_blank">demonstrated</a> how to write Ruby code inside NetBeans to control scriptable applications on a Mac, that is, communicate between Ruby and the Apple Event system. In this post, I will turn things around a bit from the previous post and use NetBeans and Java to execute AppleScript.</p>
<p>There is a subtle difference, in the previous post the focus was on how to write code in Ruby (inside NetBeans) using the <a href="http://appscript.sourceforge.net/rb-appscript/index.html" target="_blank">rb-appscript</a> bridge. This time around the approach is to work with Java and pass AppleScript code to a set of Cocoa files (classes) that will act as the bridge between our application and the Mac system.</p>
<p><a href="javascript:lynkVideoPop(719,'1209037077');" class="lynkvp"><img src="http://macdevelopertips.com/wp-content/uploads/images/NetBeans-Tips-Intro-Click-Here.png" /></a></p>
<p>There is one caveat &#8211; the Cocoa-Java API is deprecated as of Mac OS X Tiger. The NSAppleScript and NSMutableDictionary classes are still available, however, they are no longer on the development path within Apple. There are <a href="http://devworld.apple.com/documentation/ScriptingAutomation/Reference/ScriptingBridgeFramework/index.html#//apple_ref/doc/uid/TP40005074" target="_blank">scripting bridges</a> that allow you to control scriptable applications using Python, Ruby, and Objective-C. Java Native Interface (JNI) is an additional option to call platform specific code. You can read more about JNI in this technical note: <a href="http://developer.apple.com/technotes/tn2005/tn2147.html" target="_blank">JNI development on Mac OS X</a>.</p>
<p>One more note, if you follow the steps in this video and the classes NSAppleScript and NSMutableDictionary are shown with lines through them (for example, <strike>NSAppleScript</strike>), this has to do with a preference setting inside NetBeans to show deprecated classes with a strike-through. You can change this as follows: From the Preferences dialog, choose Fonts/Colors; click the Syntax option; from the Language list choose Java; click on Deprecated Element; in the Effects option, choose None.</p>
<p>Join me in Part 2 of this tip where I&#8217;ll show a more comprehensive (read: interesting) example where it&#8217;s all about controlling iTunes using Java.</p>
<hr color="#00008b" size="1" /><img src="/images/blank.gif" border="0" height="1" />The music in the video is Led Zeppelin and the song: Moby Dick.<br />
Click the image to see more about about Led Zeppelin and the CD (at Amazon)<br />
<img src="/images/blank.gif" border="0" height="3" /><br />
<a href="http://www.amazon.com/gp/product/B000002J03?ie=UTF8&amp;tag=macdevelopertips-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B000002J03" target="_blank"><img src="http://macdevelopertips.com/wp-content/uploads/2008/04/led-zep-ii.jpg" border="0" /></a><img src="http://www.assoc-amazon.com/e/ir?t=macdevelopertips-20&amp;l=as2&amp;o=1&amp;a=B000002J03" style="border: medium none  ! important; margin: 0px ! important" border="0" height="1" width="1" /><br />
<img src="/images/blank.gif" border="0" height="3" /></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/java/netbeans-java-applescript-and-itunes-part-1.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finder and glob</title>
		<link>http://MacDeveloperTips.com/applescript/finder-and-glob.html</link>
		<comments>http://MacDeveloperTips.com/applescript/finder-and-glob.html#comments</comments>
		<pubDate>Mon, 14 Apr 2008 12:12:42 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[finder]]></category>
		<category><![CDATA[glob]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/applescript/finder-and-glob.html</guid>
		<description><![CDATA[Using Unix path pattern expansion (glob) with Finder.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found a really nice, well documented, AppleScript application that makes for easy work to use Unix path pattern expansion (glob) syntax along with Finder. This is the utility you want around when you need to select a specific set of files within Finder, and command-clicking just isn&#8217;t going to cut it.</p>
<p>For instance, when you need to select files with a unique patterns such as: files that contain the string &#8220;test&#8221; (anywhere within the filename), or only those files where the name contains &#8220;test&#8221; and the extension is &#8220;rb&#8221;.</p>
<p>The application I am referring to is <a href="http://anoved.net/lselect.html" target="_blank">lselect</a>, and here are a few examples:</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/04/glob1.png" height="152" width="368" /></p>
<p>All files with &#8220;test&#8221; in the name and the extension &#8220;rb&#8221; will be selected:</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/04/glob-out1.png" /></p>
<p>Easy enough, however, lselect also allows you to add to the current list of selected files, as shown below where I ask for all files with a &#8220;C&#8221; extension (notice the button &#8216;Add Matches&#8217;):</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/04/glob2.png" /></p>
<p>The resulting list of files is shown below:</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/04/glob-out2.png" /></p>
<p>The source code for lselect is shown below (it looks rather long, however, much of the listing is comment blocks):</p>
<p><img src="http://macdevelopertips.com/images/script-editor.png" alt="Script Editor" /> <a href="applescript://com.apple.scripteditor?action=new&amp;script=%28%2A%0A%09lselect%201.1%20by%20Jim%20DeVona%0A%09http%3A%2F%2Fanoved.net%2Flselect.html%0A%091.0%3A%201%20November%202006%0A%091.1%3A%2018%20December%202006%20%28somewhat%20improved%20Column%20view%20behavior%29%0A%09%09%0A%09Select%20files%20in%20the%20current%20Finder%20folder%20using%20shell%20glob%20syntax.%0A%09When%20invoked%2C%20the%20user%20will%20be%20prompted%20to%20supply%20a%20glob%20pattern.%0A%09The%20%22ls%22%20command%20line%20utility%20is%20used%20to%20determine%20which%20files%20match%0A%09the%20pattern%2C%20and%20then%20they%20are%20selected.%0A%09%0A%09Suggested%20installation%20location%3A%0A%09%09%7E%2FLibrary%2FScripts%2FApplications%2FFinder%2Flselect.scpt%0A%09%0A%09The%20script%20can%20be%20invoked%20with%20the%20standard%20Mac%20OS%20X%20Script%20Menu%2C%0A%09but%20I%27ve%20found%20FastScripts%20%28http%3A%2F%2Fwww.red-sweater.com%2Ffastscripts%2F%29%0A%09to%20be%20a%20preferable%20alternative%2C%20primarily%20because%20of%20the%20ease%20with%20%0A%09which%20reliable%20keyboard%20bindings%20can%20be%20assigned.%20I%20use%20Command-G.%0A%09%0A%09Issues%3A%0A%09%09-%20If%20the%20last%20match%20is%20a%20directory%20and%20the%20current%20view%20type%20is%0A%09%09%20%20Column%2C%20other%20matches%20will%20not%20end%20up%20selected%20%28they%20appear%0A%09%09%20%20to%20lose%20selection%20when%20the%20Finder%20come%20to%20rest%20on%20and%20reveals%20the%0A%09%09%20%20contents%20of%20the%20last%20directory%29.%20I%20don%27t%20know%20how%20to%20prevent%20this.%0A%09%09-%20Spaces%20must%20be%20escaped%20to%20match%20properly%3A%20%22iTunes%5C%20Music%22%0A%09%09-%20Performance%20is%20poor%20with%20hundreds%20of%20matches%3B%20see%20notes%20below%0A%09%0A%09Wishes%3A%0A%09%09-%20Select%20sub-matches%20within%20current%20selection%0A%09%09-%20Select%20matches%20on%20desktop%20instead%20of%20in%20%22Desktop%22%20window%0A%09%09-%20Support%20patterns%20like%20%22..%2F%22%20%28subfolder%20patterns%20work%2C%20but%20not%20parent%29%0A%2A%29%0A%0Atell%20application%20%22Finder%22%0A%09%0A%09%28%2A%0A%09%09Determine%20the%20present%20working%20directory%20as%20alias%20and%20POSIX%20path.%0A%09%09If%20the%20insertion%20location%20is%20not%20a%20folder%2C%20use%20its%20parent.%20This%20is%20the%0A%09%09case%20when%20a%20file%20is%20selected%20in%20Column%20view%20%28otherwise%2C%20file%20selections%0A%09%09do%20not%20seem%20to%20be%20treated%20as%20the%20insertion%20location%29.%0A%09%2A%29%0A%09set%20pwdAlias%20to%20insertion%20location%20as%20alias%0A%09if%20not%20%28exists%20folder%20pwdAlias%29%20then%0A%09%09set%20pwdAlias%20to%20%28container%20of%20pwdAlias%29%20as%20alias%0A%09end%20if%0A%09set%20pwd%20to%20POSIX%20path%20of%20pwdAlias%0A%09%0A%09%28%2A%0A%09%09Ask%20the%20user%20what%20to%20select.%20Dialog%20time%20out%20is%20equivalent%20to%20cancellation.%0A%09%09The%20default%20%22Select%20Matches%22%20option%20clears%20the%20current%20Finder%20selection%2C%0A%09%09whereas%20%22Add%20Matches%22%20leaves%20it%20intact.%20Clearing%20the%20selection%20is%20not%20done%0A%09%09in%20Column%20view%20if%20the%20displayed%20folder%20is%20the%20only%20thing%20selected.%0A%09%2A%29%0A%09set%20dr%20to%20display%20dialog%20%22Glob%20pattern%3A%22%20default%20answer%20%C2%AC%0A%09%09%22%22%20buttons%20%7B%22Cancel%22%2C%20%22Add%20Matches%22%2C%20%22Select%20Matches%22%7D%20%C2%AC%0A%09%09default%20button%203%20cancel%20button%201%20with%20title%20pwd%20giving%20up%20after%2060%0A%09if%20button%20returned%20of%20dr%20is%20equal%20to%20%22%22%20then%0A%09%09return%0A%09else%20if%20button%20returned%20of%20dr%20is%20equal%20to%20%22Select%20Matches%22%20then%0A%09%09try%0A%09%09%09--%20do%20not%20clear%20selection%20if%20the%20only%20thing%20selected%20is%20the%20focal%20folder%0A%09%09%09if%20selection%20as%20alias%20is%20not%20equal%20to%20pwdAlias%20then%20select%20%7B%7D%0A%09%09on%20error%0A%09%09%09--%20more%20than%20one%20thing%20already%20selected%0A%09%09%09select%20%7B%7D%0A%09%09end%20try%0A%09end%20if%0A%09%0A%09%28%2A%0A%09%09Initialize%20list%20of%20selected%20files.%20Generally%20identical%20to%20selection%20returned%0A%09%09by%20Finder%2C%20except%20the%20present%20working%20directory%20should%20not%20be%20included%2C%0A%09%09which%20is%20initially%20selected%20in%20some%20Column%20view%20circumstances%20%28see%20above%29.%0A%09%09This%20is%20a%20little%20clumsy%3B%20selection%20state%20is%20vaguely%20defined%20in%20Column%20view.%0A%09%2A%29%0A%09set%20selectables%20to%20selection%0A%09try%0A%09%09if%20selection%20as%20alias%20is%20equal%20to%20pwdAlias%20then%20set%20selectables%20to%20%7B%7D%0A%09end%20try%0A%09%0A%09%28%2A%0A%09%09Get%20the%20glob%20pattern%20given%20by%20the%20user.%0A%09%09We%20treat%20a%20blank%20pattern%20as%20cancellation%20%28use%20%2A%20to%20select%20everything%29.%0A%09%09Alternatively%2C%20omit%20this%20conditional%20to%20select%20the%20containing%20folder%3B%0A%09%09this%20ought%20to%20be%20the%20default%20behavior%20once%20%22..%2F%22%20issues%20are%20ironed%20out.%0A%09%2A%29%0A%09set%20query%20to%20text%20returned%20of%20dr%0A%09if%20query%20%3D%20%22%22%20then%20return%0A%09%0A%09%28%2A%0A%09%09Ask%20ls%20for%20a%20listing%20of%20files%20that%20match%20the%20given%20pattern.%0A%09%09From%20the%20ls%20man%20page%3A%0A%09%09%09-d%20Directories%20are%20listed%20as%20plain%20files%20%28not%20searched%20recursively%29.%0A%09%09If%20nothing%20matches%20the%20query%2C%20ls%20will%20return%20an%20error%3B%20just%20stop.%0A%09%2A%29%0A%09try%0A%09%09tell%20me%20to%20set%20matches%20to%20do%20shell%20script%20%28%22%2Fbin%2Fls%20-d%20%22%20%26%20quoted%20form%20of%20pwd%20%26%20query%29%0A%09on%20error%0A%09%09return%0A%09end%20try%0A%09%0A%09%28%2A%0A%09%09Parse%20each%20line%20of%20the%20response%20from%20ls%20as%20the%20path%20to%20a%20match.%0A%09%09The%20visibility%20test%20is%20twofold%3A%20the%20%22info%20for%22%20test%20throws%20an%20error%20on%0A%09%09Icon%5EM%20%28the%20full%20name%20doesn%27t%20survive%20all%20translations%20and%20transmissions%29.%0A%09%09This%20try-info-for-visibilty%20test%20is%20the%20main%20bottleneck%3B%0A%09%09for%20faster%20handling%20of%20many%20matches%20%28100s%29%2C%20replace%20this%0A%09%09repeat%20body%20with%20%22set%20end%20of%20selectables%20to%20matchpath%20as%20POSIX%20file%22%0A%09%2A%29%0A%09repeat%20with%20matchpath%20in%20paragraphs%20of%20matches%0A%09%09set%20posixmatch%20to%20matchpath%20as%20POSIX%20file%0A%09%09try%0A%09%09%09set%20fileinfo%20to%20info%20for%20posixmatch%20without%20size%0A%09%09%09if%20visible%20of%20fileinfo%20then%20set%20end%20of%20selectables%20to%20posixmatch%0A%09%09end%20try%0A%09end%20repeat%0A%09%0A%09%28%2A%0A%09%09Conclude%20by%20selecting%20the%20results.%0A%09%09The%20%22try%22%20protects%20against%20cases%20we%20don%27t%20%5Byet%5D%20handle%2C%0A%09%09such%20as%20certain%20%22..%2F%22%20path%20traversals%20and%20anything%20else%20that%20may%20come%20up.%0A%09%09If%20the%20last%20item%20of%20selectables%20is%20a%20directory%20and%20we%27re%20using%0A%09%09Column%20view%2C%20other%20items%20may%20not%20end%20up%20selected.%0A%09%2A%29%0A%09try%0A%09%09select%20every%20item%20of%20selectables%0A%09on%20error%20errMsg%20number%20errNum%0A%09%09display%20alert%20%22Could%20not%20make%20selection%20%28%22%20%26%20errNum%20%26%20%22%29%3A%22%20message%20errMsg%20as%20critical%0A%09%09return%0A%09end%20try%0A%09%0Aend%20tell">Click here</a> to paste the code below into the Script Editor</p>
<pre>
(*
  lselect 1.1 by Jim DeVona

http://anoved.net/lselect.html

  1.0: 1 November 2006
  1.1: 18 December 2006 (somewhat improved Column view behavior)

  Select files in the current Finder folder using shell glob syntax.
  When invoked, the user will be prompted to supply a glob pattern.
  The "ls" command line utility is used to determine which files match
  the pattern, and then they are selected.

  Suggested installation location:
    ~/Library/Scripts/Applications/Finder/lselect.scpt

  The script can be invoked with the standard Mac OS X Script Menu,
  but I've found FastScripts (http://www.red-sweater.com/fastscripts/)
  to be a preferable alternative, primarily because of the ease with
  which reliable keyboard bindings can be assigned. I use Command-G.

  Issues:
    - If the last match is a directory and the current view type is
      Column, other matches will not end up selected (they appear
      to lose selection when the Finder come to rest on and reveals the
      contents of the last directory). I don't know how to prevent this.
    - Spaces must be escaped to match properly: "iTunes\ Music"
    - Performance is poor with hundreds of matches; see notes below

  Wishes:
    - Select sub-matches within current selection
    - Select matches on desktop instead of in "Desktop" window
    - Support patterns like "../" (subfolder patterns work, but not parent)
*)

tell application "Finder"

  (*
    Determine the present working directory as alias and POSIX path.
    If the insertion location is not a folder, use its parent. This is the
    case when a file is selected in Column view (otherwise, file selections
    do not seem to be treated as the insertion location).
  *)
  set pwdAlias to insertion location as alias
  if not (exists folder pwdAlias) then
    set pwdAlias to (container of pwdAlias) as alias
  end if
  set pwd to POSIX path of pwdAlias

  (*
    Ask the user what to select. Dialog time out is equivalent to cancellation.
    The default "Select Matches" option clears the current Finder selection,
    whereas "Add Matches" leaves it intact. Clearing the selection is not done
    in Column view if the displayed folder is the only thing selected.
  *)
  set dr to display dialog "Glob pattern:" default answer ¬
    "" buttons {"Cancel", "Add Matches", "Select Matches"} ¬
    default button 3 cancel button 1 with title pwd giving up after 60
  if button returned of dr is equal to "" then
    return
  else if button returned of dr is equal to "Select Matches" then
    try
      -- do not clear selection if the only thing selected is the focal folder
      if selection as alias is not equal to pwdAlias then select {}
    on error
      -- more than one thing already selected
      select {}
    end try
  end if

  (*
    Initialize list of selected files. Generally identical to selection returned
    by Finder, except the present working directory should not be included,
    which is initially selected in some Column view circumstances (see above).
    This is a little clumsy; selection state is vaguely defined in Column view.
  *)
  set selectables to selection
  try
    if selection as alias is equal to pwdAlias then set selectables to {}
  end try

  (*
    Get the glob pattern given by the user.
    We treat a blank pattern as cancellation (use * to select everything).
    Alternatively, omit this conditional to select the containing folder;
    this ought to be the default behavior once "../" issues are ironed out.
  *)
  set query to text returned of dr
  if query = "" then return

  (*
    Ask ls for a listing of files that match the given pattern.
    From the ls man page:
      -d Directories are listed as plain files (not searched recursively).
    If nothing matches the query, ls will return an error; just stop.
  *)
  try
    tell me to set matches to do shell script ("/bin/ls -d " &amp; quoted form of pwd &amp; query)
  on error
    return
  end try

  (*
    Parse each line of the response from ls as the path to a match.
    The visibility test is twofold: the "info for" test throws an error on
    Icon^M (the full name doesn't survive all translations and transmissions).
    This try-info-for-visibilty test is the main bottleneck;
    for faster handling of many matches (100s), replace this
    repeat body with "set end of selectables to matchpath as POSIX file"
  *)
  repeat with matchpath in paragraphs of matches
    set posixmatch to matchpath as POSIX file
    try
      set fileinfo to info for posixmatch without size
      if visible of fileinfo then set end of selectables to posixmatch
    end try
  end repeat

  (*
    Conclude by selecting the results.
    The "try" protects against cases we don't [yet] handle,
    such as certain "../" path traversals and anything else that may come up.
    If the last item of selectables is a directory and we're using
    Column view, other items may not end up selected.
  *)
  try
    select every item of selectables
  on error errMsg number errNum
    display alert "Could not make selection (" &amp; errNum &amp; "):" message errMsg as critical
    return
  end try
end tell</pre>
<p>Here is brief list of glob syntax you can try with lselect:</p>
<p>? Match any one character.<br />
* Match any sequence of zero or more characters.<br />
[abc]  Match any one character in abc. A range of characters may be specified as a-z, or 0-9.<br />
[^abc] Match any one character not in abc.<br />
{a,b,c} Matches any of the strings a, b, or c, which may consist of more than one character each.</p>
<p>lselect shows a good use of AppleScript, is well documented and it&#8217;s handy&#8230;all good things.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/applescript/finder-and-glob.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NetBeans 6.1, Ruby and AppleScript</title>
		<link>http://MacDeveloperTips.com/tools-utilities/netbeans-61-ruby-and-applescript.html</link>
		<comments>http://MacDeveloperTips.com/tools-utilities/netbeans-61-ruby-and-applescript.html#comments</comments>
		<pubDate>Wed, 02 Apr 2008 12:21:21 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Tools/Utilities]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[rb-appscript]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[screencast]]></category>

		<guid isPermaLink="false">http://MacDeveloperTips.com/tools-utilities/netbeans-61-ruby-and-applescript.html</guid>
		<description><![CDATA[Screencast to demonstrate using NetBeans, Ruby and rb-appscript together to control scriptable applications on Mac OS X.]]></description>
			<content:encoded><![CDATA[<p>What do NetBeans,  Ruby and AppleScript have in common? There&#8217;s a circular definition of sorts to explain&#8230;to start, all are tools available to developers working on Mac. Next, NetBeans has full support for creating, debugging and packaging Ruby code. And finally, Ruby (with the right tools in place) can be used within NetBeans to control scriptable applications on a Mac, something which is typically accomplished using AppleScript.</p>
<p><a href="http://www.netbeans.org/community/releases/61/" target="_blank"><img src="/wp-content/uploads/2008/04/netbeansBeta-1.gif" align="left" height="115" width="123" /></a>In this post I&#8217;ll describe more about how all these tools come together to provide an interesting approach for using Ruby as an alternative to AppleScript, and working with NetBeans as the development environment for writing and building those same applications.</p>
<p>The screencast that follows will walk you through all the steps to download the tools you&#8217;ll need, build from source a scripting bridge  (<a href="http://rb-appscript.rubyforge.org/" target="_blank">rb-appscript</a>) for Ruby to AppleScript, and finally, create several short examples to demonstrate how you can use Ruby, from within <a href="http://www.netbeans.org" target="_blank">NetBeans</a> to control <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptX/Concepts/scriptable_apps.html#//apple_ref/doc/uid/TP40001569-BABEBGCF" target="_blank">scriptable applications</a>.</p>
<p><a href="javascript:lynkVideoPop(719,'1207055473');" class="lynkvp"><img src="/wp-content/uploads/2008/04/netbeansBeta-2.png" /></a></p>
<p>For more information, follow these links:</p>
<ul>
<li><a href="http://netbeans.org" target="_blank">NetBeans</a></li>
<li><a href="http://appscript.sourceforge.net/rb-appscript/index.html" target="_blank">rb-appscript</a></li>
<li><a href="http://www.apple.com/applescript/" target="_blank">AppleScript</a></li>
</ul>
<p>If you are interested to learn more about using Ruby as an alternative to AppleScript, I&#8217;ve written as series of posts you can find here: <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html" target="_blank">Part 1</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html" target="_blank">Part 2</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-3.html" target="_blank">Part 3</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank">Part 4</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-5.html" target="_blank">Part 5</a>, <a href="http://macdevelopertips.com/tools-utilities/netbeans-61-ruby-and-applescript.html" target="_blank">Part 6</a></p>
<p>If you write an interesting application in NetBeans, using Ruby and rb-appscript, <a href="http://macdevelopertips.com/?page_id=91" target="_blank">drop me a note</a>, as I&#8217;d like to  post several good examples showing the interaction of all these tools.</p>
<p>As an aside, NetBeans is sponsoring a blogging contest for the 6.1 Beta. If you are interested in learning more, visit the <a href="http://www.netbeans.org/competition/blog-contest.html" target="_blank">NetBeans Blogging Contest</a>. And who knows, you could walk away with one of ten $500 American Express gift certificates!</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/tools-utilities/netbeans-61-ruby-and-applescript.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, an AppleScript Alternative &#8211; Part 4</title>
		<link>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-4.html</link>
		<comments>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-4.html#comments</comments>
		<pubDate>Sat, 15 Mar 2008 03:44:33 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[rb-appscript]]></category>

		<guid isPermaLink="false">http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html</guid>
		<description><![CDATA[The fourth in a series of posts on how to use Ruby (rb-appscript) as a replacement for AppleScript.]]></description>
			<content:encoded><![CDATA[<p>My intention in starting this series of posts on Ruby was to re-write two short AppleScript applications; one that turns down the volume at shutdown, and one that resets the volume upon bootup. We&#8217;ve covered enough information up to this point to have all we need to write the Ruby version (using rb-appscript).<br />
<span id="more-68"></span></p>
<p>The AppleScript example we are porting to Ruby is as follows:</p>
<pre>tell application "Finder"

display dialog "What would you like to do?"
   buttons {"Shutdown", "Restart", "Cancel"} default button "Cancel"
  with icon caution

if button returned is "Shutdown" then
  set volume 0
  shut down
else if button returned is "Restart" then
  set volume 0
  restart
end if

end tell</pre>
<p>Pretty simple application, display a dialog box with three options and based on the selected option, shut down the system, go to sleep or do nothing. The application to reset the volume at boot time is all of one line:</p>
<pre>set volume 5</pre>
<p>You can get the specifics of how I used these scripts by referring back to the <a href="http://macdevelopertips.com/applescript/lower-volume-and-shutdown.html">original post</a>.</p>
<p>The Ruby version of the first script looks as follows:</p>
<pre>require 'osax'
include OSAX

# Display dialog box and get user request
dialog = osax.display_dialog( "What would you like to do?",
  :buttons =&gt; ["Shutdown", "Restart", "Cancel "], :default_button =&gt; "Cancel ",
  :with_title =&gt; "System Shutdown...", :with_icon =&gt; :caution)

if dialog[:button_returned] == "Shutdown"
  puts "Shutdown"
  osax.set_volume(0)
  app("System Events").shut_down()
elsif dialog[:button_returned] == "Restart"
  puts "Restart"
  osax.set_volume(0)
  app("System Events").restart()
end</pre>
<p>The Ruby code to reset the volume is as simple as its AppleScript counterpart:</p>
<pre>require 'osax'
include OSAX

# Turn volume up (range is 0 to 7)
osax.set_volume(5)</pre>
<p>One caveat worth mentioning regarding the Ruby code: notice there is space in the references to &#8220;Cancel &#8221; &#8211; You can read why that is by following <a href="http://rubyforge.org/pipermail/rb-appscript-discuss/2008-January/000107.html" target="_blank">this thread</a> on the rb-appscript discussion/mailing list.</p>
<p><strong><font color="#000080"> Additional Resources:</font></strong></p>
<p>I&#8217;ll leave you with a few additional resources that should be helpful if you are new to rb-appscript and/or AppleScript:</p>
<ul>
<li><a href="http://appscript.sourceforge.net/rb-appscript/doc/index.html" target="_blank">Documentation on rb-appscript </a></li>
<li><a href="http://appscript.sourceforge.net/rb-appscript/doc/appscript-manual/03_quicktutorial.html" target="_blank">Tutorial on getting started with rb-appscript</a></li>
<li><a href="http://appscript.sourceforge.net/links.html" target="_blank">Additional links</a></li>
</ul>
<p>Additional posts in the series: <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html" target="_blank">Part 1</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html" target="_blank">Part 2</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-3.html" target="_blank">Part 3</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-5.html" target="_blank">Part 5</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-6.html" target="_blank">Part 6</a></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-4.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, an AppleScript Alternative &#8211; Part 3</title>
		<link>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-3.html</link>
		<comments>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-3.html#comments</comments>
		<pubDate>Fri, 14 Mar 2008 01:36:07 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[rb-appscript]]></category>

		<guid isPermaLink="false">http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-3.html</guid>
		<description><![CDATA[This post is 3rd in a series about using Ruby and rb-appscript as an alternative to AppleScript.]]></description>
			<content:encoded><![CDATA[<p>So enough of the introductions (see <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html" target="_blank">Part 1</a> and <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html" target="_blank">Part 2</a>), let&#8217;s get on with the code. To get started, let&#8217;s look at how to use scripting additions from within rb-appscript as the application that I have in mind will need to display a dialog box (which is located in Standard Additions). We can get a list of the additions currently available as well as a list of commands using the following Ruby code:<br />
<span id="more-57"></span></p>
<pre>
require 'osax'
include OSAX

# List available scripting additions
puts osax.scripting_additions

puts "~~~~~~~~~~"

# List all commands:
puts osax.commands</pre>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/ruby-cmd.png" height="411" width="302" /></p>
<p>Notice the third line from the top, StandardAdditions, this is the addition I&#8217;m after. Let&#8217;s take this one step further and view the rb-appscript variation of a dictionary. rb-appscript includes a tool, <a href="http://appscript.sourceforge.net/tools.html" target="_blank">ASDictionary</a>, which will export to text or HTML (I recommend the HTML with frames option) information on scriptable applications.</p>
<p>Not unlike the dictionary available in the Script Editor, ASDictionary will list the suites, classes and commands, however, the content is tailored for use with specifically with rb-appscript and Ruby. The screenshot below shows the StandardAdditions library information from with ASDictionary.</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/asdic.png" /></p>
<p>Now, contrast the above image with the image below, which is the Standard Additions dictionary shown yesterday (for AppleScript).</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/dict-dialog.png" height="333" width="462" /></p>
<p>When working with rb-appscript, you&#8217;ll notice how what would be accessed with AppleScipt as <font color="#800000"><em>display dialog</em></font> is requested using <em><font color="#800000">display_dialog(&#8230;)</font></em> in rb-appscript.</p>
<p>As a more complete example, the code below will show a dialog box using AppleScript:</p>
<pre>
display dialog "What would you like to do?" buttons {"Shutdown", "Restart", "Cancel"}
   default button "Shutdown" with icon caution</pre>
<p>Here is the same code written using Ruby:</p>
<pre>
osax.display_dialog( "What would you like to do?", :buttons =&gt; [ "Shutdown", "Restart", "Cancel " ],
  :default_button =&gt; "Shutdown", :with_icon =&gt; :caution)</pre>
<p>Regardless of the approach, the end result of the either code block above looks the same:</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/shutdown1.png" align="right" /></p>
<p>This is a good time to point you at the <a href="http://appscript.sourceforge.net/rb-appscript/doc/index.html" target="_blank">documentation for rb-appscript</a> as it covers in great detail all the specifics.</p>
<p>At this point I&#8217;ve covered enough to write something useful. Have a look at the following AppleScript code that I wrote about in a <a href="http://macdevelopertips.com/applescript/lower-volume-and-shutdown.html" target="_blank">previous</a> post.</p>
<pre>tell application "Finder"

display dialog "What would you like to do?"
   buttons {"Shutdown", "Restart", "Cancel"} default button "Shutdown"
  with icon caution

if button returned is "Shutdown" then
  set volume 0
  shut down
else if button returned is "Restart" then
  set volume 0
  restart
end if

end tell</pre>
<p>In an upcoming post I&#8217;ll show to write the same script using using Ruby and rb-appscript. I realize this is a pretty short example, however, if nothing else it should help pull all the pieces together into a working Ruby application that offers an alternative to AppleScript.</p>
<p>Additional posts in the series: <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html" target="_blank">Part 1</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html" target="_blank">Part 2</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank">Part 4</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-5.html" target="_blank">Part 5</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-6.html" target="_blank">Part 6</a><a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-3.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, an AppleScript Alternative &#8211; Part 2</title>
		<link>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-2.html</link>
		<comments>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-2.html#comments</comments>
		<pubDate>Thu, 13 Mar 2008 13:30:35 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[rb-appscript]]></category>

		<guid isPermaLink="false">http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html</guid>
		<description><![CDATA[This is Part 2 in a series on Ruby as an alternative to AppleScript. Introduced are dictionaries and Scripting Additions.]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://macdevelopertips.com/applescript/applescript-20-language-guide.html" target="_blank">Part 1 of this series</a> I walked through the steps to install <a href="http://appscript.sourceforge.net/rb-appscript/index.html" target="_blank">rb-appscript</a>, a scripting bridge that provides an alternative to AppleScript for controlling scriptable applications on a Mac.</p>
<p>My intention in exploring an alternative was to find an option for scripting beyond AppleScript itself and to rewrite an AppleScript program that I was currently using. The original AppleScript code was written to turn down the volume upon shutdown, to enable a silent bootup process. You can read the who, what and why in the <a href="http://macdevelopertips.com/applescript/lower-volume-and-shutdown.html" target="_blank">original post</a>.</p>
<p><span id="more-55"></span>However, I&#8217;m getting ahead of myself. Before we dig into the original code and how to write the same application in Ruby, let me take some time to explain a few other things important concepts within AppleScript.</p>
<p><font color="#003366"><strong>Dictionaries</strong></font></p>
<p>In order to work with a scriptable application (or aspects of the system that are scriptable), one first needs to know what is scritable and how to interact with the scriptable resource, an API of sorts. In AppleScript parlance this is know as the Apple event registry, also referred to as a dictionary.</p>
<p>Let&#8217;s look at a few examples &#8211; to open the dictionary for system related events, open the Script Editor, click on File / Open Dictionary, and browse to the &#8220;System Events.app&#8221; application. Scroll down to the Power Suite and you should see a listing  similar to the Figure below, which shows information on the commands: logout, restart, shutdown and sleep.</p>
<p>Information is organized into suites, which are shown in the leftmost window. In the middle column are available commands for the Power Suite. There is a great deal more information available within the dictionary, however, I&#8217;ll save the detailed description for another day.</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/dict-power.png" /></p>
<p><font color="#003366"><strong>Scripting Additions</strong></font></p>
<p>Scripting Additions are a mechanism for extending the functionality of AppleScript. There are any number of Scripting Additions available (google for scripting additions) from mathematical stuff to sending SMS messages. A good list of addtions can be found at <a href="http://osaxen.com" target="_blank">osaxen.com</a>.</p>
<p>A commonly used Scripting Addition, and one which is included by default, is Standard Additions. For example, to display a dialog box, you use the <font color="#800000"><em>User Interaction Suite</em></font> and the <font color="#800000"><em>display dialog</em></font> command as shown in the figure below. To bring up this dictionary from within the Script Editor, open a new dictionary and scroll to the Standard Additions entry.</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/dict-dialog.png" /></p>
<p>The information covered up to this point will lead us to the next post, which will make it clear why I chose the dictionaries I did in the examples above. And with the next post we&#8217;ll begin to dig into some Ruby code to make a few things happen with Apple Events. Same bat time, same bat channel&#8230;</p>
<p>Additional posts in the series: <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html" target="_blank">Part 1</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-3.html" target="_blank">Part 3</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank">Part 4</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-5.html" target="_blank">Part 5</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-6.html" target="_blank">Part 6</a><a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby, an AppleScript Alternative &#8211; Part 1</title>
		<link>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-1.html</link>
		<comments>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-1.html#comments</comments>
		<pubDate>Wed, 12 Mar 2008 22:53:07 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[rb-appscript]]></category>

		<guid isPermaLink="false">http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-1.html</guid>
		<description><![CDATA[An introduction and installation guide for rb-appscript to enable Ruby developers to query and control scriptable applications on a Mac, providing an alternative to AppleScript. ]]></description>
			<content:encoded><![CDATA[<p>AppleScript is a scripting language that provides the ability to control &#8220;scriptable&#8221; applications on a Mac. AppleScript provides an interface to the Apple Event messaging architecture, the means by which applications communicate with one another as well as the underlying OS.</p>
<p><span id="more-53"></span>Here is the description of AppleScript from the <a href="http://www.apple.com/applescript/index.html" target="_blank">Apple website</a>:</p>
<blockquote><p>AppleScript is an English-like language used to create script files that control the actions of the computer and the applications that run on it. Much more than just a macro-language, which simply repeats your recorded actions, AppleScript scripts can &#8220;think.&#8221;</p></blockquote>
<p>If you come from a programming background, it may take some time to get used to the &#8220;English-like&#8221; interface. Now this is not necessarily a bad thing as it makes AppleScript more approachable for those without any experience writing code.</p>
<p>In my search for an alternative to AppleScript, I came upon <a href="http://appscript.sourceforge.net/rb-appscript/index.html" target="_blank">rb-appscript</a>. Ruby appscript is an Apple event bridge that provides a framework for querying and controlling scriptable applications from Ruby. I first read about rb-appscript in <a href="http://www.oreilly.com/pub/a/mac/2007/02/27/replacing-applescript-with-ruby.html" target="_blank">this article</a> at O&#8217;Reilly, which provides an excellent introduction to the topic.</p>
<p>Let&#8217;s walk through how to install rb-appscript and wrap up with a short Ruby script to verify everything is working. For installation, there are two options &#8211; one is via RubyGems, the second is to build from source. I generally build from source as I have more control over the process. The steps for doing so are shown below (you&#8217;ll find the same basic steps listed in the O&#8217;Reilly article mentioned above):</p>
<ol>
<li>Download rb-appscript from <a href="http://rubyforge.org/projects/rb-appscript/" target="_blank">RubyForge</a> (I am running rb-appscript-0.5.1)</li>
<li>Extract the contents of the zip file</li>
<li>From the directory where the files were extracted, run these commands from a Terminal:</li>
<p>&gt; <font color="#800000"><em>ruby extconf.rb</em></font><br />
&gt; <font color="#800000"><em>make</em></font><br />
&gt; <font color="#800000"><em>sudo make install</em></font></p>
<li>Open a text file and enter the following code:</li>
<pre>
require "appscript"
include Appscript

app('TextEdit').activate</pre>
<li> Save the file as <font color="#800000"><em>test.rb</em></font> and from a Terminal run the script:<br />
&gt; <em><font color="#800000">ruby test.rb</font></em></li>
</ol>
<p>This short application does nothing more then use an application object to activate TextEdit, bringing it to the foreground (starting it if necessary). If all is well, you should see TextEdit as the top most application.</p>
<p>In the next tip I&#8217;ll dig a little deeper into working with rb-appscript, including a look at how to access Standard Additions such as dialog boxes.</p>
<p>Additional posts in the series: <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-2.html" target="_blank">Part 2</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-3.html" target="_blank">Part 3</a>, <a href="http://macdevelopertips.com/ruby/ruby-an-applescript-alternative-part-4.html" target="_blank">Part 4</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-5.html" target="_blank">Part 5</a>, <a href="http://macdevelopertips.com/ruby/ruby-as-applescript-alternative-part-6.html" target="_blank">Part 6</a></p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/ruby/ruby-an-applescript-alternative-part-1.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>AppleScript 2.0 Language Guide</title>
		<link>http://MacDeveloperTips.com/applescript/applescript-20-language-guide.html</link>
		<comments>http://MacDeveloperTips.com/applescript/applescript-20-language-guide.html#comments</comments>
		<pubDate>Tue, 11 Mar 2008 21:56:54 +0000</pubDate>
		<dc:creator>john</dc:creator>
				<category><![CDATA[AppleScript]]></category>

		<guid isPermaLink="false">http://macdevelopertips.com/applescript/applescript-20-language-guide.html</guid>
		<description><![CDATA[It&#8217;s been a long wait, however, as of today you can now get your hands on the latest update to the AppleScript Language Guide. The copy that I&#8217;ve been using for the past 6 months is dated 1999. Yikes! With the release of AppleScript 2.0 (accompanying Leopard) there is now support for Unicode, so you [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long wait, however, as of today you can now get your hands on the latest update to the <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/index.html" target="_blank">AppleScript Language Guide</a>. The copy that I&#8217;ve been using for the past 6 months is dated 1999. Yikes!    <img src="/wp-content/uploads/2008/03/icon-guides-s.jpg" align="right" /></p>
<p><span id="more-51"></span></p>
<p>With the release of AppleScript 2.0 (accompanying Leopard) there is now support for Unicode, so you can do things like this:</p>
<p><img src="http://macdevelopertips.com/wp-content/uploads/2008/03/apple-script1.png" height="75" width="356" /></p>
<p>The application object has updates as well, such that you can check whether an application is running, is it the front-most application, what version is running, etc.   There are also additions to scripting support across the system, for instance, network preferences are now scriptable. You can find all the release notes for 2.0 <a href="http://developer.apple.com/releasenotes/AppleScript/RN-AppleScript/index.html" target="_blank">here.</a></p>
<p><a href="http://developer.apple.com/releasenotes/AppleScript/RN-AppleScript/index.html" target="_blank"></a>If you are new to AppleScript, there is an <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW20" target="_blank">AppleScript Fundamentals</a> section that offers a good high-level overview of AppleScript from Objects to Scripting Additions to Commands, among other topics. On that same line of thought, you may also want to spend some time poking around the <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW20" target="_blank">debugging tips</a> which includes an updated section on handling of errors with <a href="http://developer.apple.com/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-128973" target="_blank">try statements</a> and <a href="http://developer.apple.com/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html#//apple_ref/doc/uid/TP40000983-CH6g-129657" target="_blank">error statements</a>.</p>
<p>You can download a <a href="http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScriptLanguageGuide.pdf" target="_blank">PDF version</a> of the file that is handy if you&#8217;d like to print a copy.</p>
]]></content:encoded>
			<wfw:commentRss>http://MacDeveloperTips.com/applescript/applescript-20-language-guide.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

