<?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>devtrends.com &#187; AppleScript</title>
	<atom:link href="http://www.devtrends.com/index.php/category/software-development/applescript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devtrends.com</link>
	<description>developing trends in information technology</description>
	<lastBuildDate>Tue, 06 Sep 2011 19:27:42 +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>Move Files with Folder Actions and AppleScript</title>
		<link>http://www.devtrends.com/index.php/move-files-with-folder-actions-and-applescript/</link>
		<comments>http://www.devtrends.com/index.php/move-files-with-folder-actions-and-applescript/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 05:09:17 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Folder Actions]]></category>
		<category><![CDATA[zFeatured]]></category>
		<category><![CDATA[POSIX]]></category>

		<guid isPermaLink="false">http://www.devtrends.com/?p=512</guid>
		<description><![CDATA[In relation to my article on Combining PDFs in Apple Automator, I needed to automate moving files from one location to another, with the final destination name of the file...]]></description>
			<content:encoded><![CDATA[<p>In relation to my article on <a href="http://www.devtrends.com/index.php/combine-pdfs-in-apple-automator/">Combining PDFs in Apple Automator</a>, I needed to automate moving files from one location to another, with the final destination name of the file being different than the original. At first I thought this would be 5 minutes in AppleScript, until I realized, as with all programming, that I would run into programming language complexities. The first one was moving to a mounted location, the second was formatting dates and the third was getting a single object in the array, added_items, to output the UNIX style path.</p>
<p>The script to accomplish this task ended up looking like this:</p>
<pre style="padding-left: 30px;"><strong>on</strong> adding folder items to this_folder after receiving added_items<strong>
  repeat</strong> <strong>with</strong> this_item <strong>in</strong> added_items<strong>
    set</strong> destpath <strong>to</strong> "/Volumes/[mount name]/[share]/" <strong>as</strong> string<strong>
    set</strong> datestring <strong>to</strong> ((year <strong>of</strong> (current date)) * 10000) + ((month <strong>of</strong> (current date) <strong>as</strong> integer) * 100) + (day <strong>of</strong> (current date)) <strong>as</strong> integer<strong>
    set</strong> destfilename <strong>to</strong> "thefile_" &amp; datestring &amp; ".pdf" <strong>as</strong> string<strong>
    set</strong> sourcepath <strong>to</strong> POSIX path <strong>of</strong> this_item<strong>
  end</strong> <strong>repeat
end</strong> adding folder items to</pre>
<p>If you are familiar with Visual Basic, the &#8220;repeat with this_item in added_items&#8221; would be similar to &#8220;for each this_item in added_items&#8221;. This creates a single object (this_item) from the array of objects (added_items). The destpath is a variable of the location that we will write the final file, which in this case is a mounted server.</p>
<p><strong>datestring</strong></p>
<p>The date format was not as easy to accomplish as I would have expected, especially considering that AppleScript is supposed to simple to use with its English language type structure. To create a date of 20100101, I had to use traditional math starting with the current year multiplied by 10000, which would end up:</p>
<pre style="padding-left: 30px;">2010 * 10000 = 20100000</pre>
<p>Next I needed to add the month in the appropriate location in the integer. This was accomplished by current month multiplied by 100, which would end up:</p>
<pre style="padding-left: 30px;">01 * 100 = 100</pre>
<p>Add the month to the year integer and you have:</p>
<pre style="padding-left: 30px;">20100000 + 100 = 20100100</pre>
<p>Finally, we need to add the day, which as you can imagine, needs no modification:</p>
<pre style="padding-left: 30px;">20100100 + 01 = 20100101</pre>
<p>The final number is stored in the variable datestring.</p>
<p><strong>sourcepath</strong></p>
<p>After about 30 minutes of frustration, working with other avenues to accomplish the same task, I finally found an AppleScript example that used &#8220;POSIX path of&#8221;. Being that I am a native Windows guy, POSIX is fairly foreign to me, however, it is key to making sure the shell command, &#8220;mv&#8221;, works properly.</p>
<p>If you were to use:</p>
<pre style="padding-left: 30px;">set sourcepath to name of this_item</pre>
<p>you  would get a path separated with colons, such as harddrive:Volumes:[mount name]:[share], which is clearly useless in a shell environment. So instead, we must request the POSIX path, which is the full path and file name of &#8220;this_item&#8221;:</p>
<pre style="padding-left: 30px;">set sourcepath to POSIX path of this_item</pre>
<p>That is it&#8230;assign this script to a folder using the Folder Actions functionality of the Mac operating system.</p>
<p>-Aaron Gilbert</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtrends.com/index.php/move-files-with-folder-actions-and-applescript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alert (Display Dialog) in AppleScript</title>
		<link>http://www.devtrends.com/index.php/alert-display-dialog-in-applescript/</link>
		<comments>http://www.devtrends.com/index.php/alert-display-dialog-in-applescript/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 04:40:12 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[display dialog]]></category>

		<guid isPermaLink="false">http://www.devtrends.com/?p=510</guid>
		<description><![CDATA[To display an alert or dialog prompt in AppleScript is easy, once you know the command string to use. For simple scripting, such as AppleScript or VBScript, I will frequently...]]></description>
			<content:encoded><![CDATA[<p>To display an alert or dialog prompt in AppleScript is easy, once you know the command string to use. For simple scripting, such as AppleScript or VBScript, I will frequently use alert dialogs to display the contents of variables. Hmm, any other reasons for a dialog prompt? Obviously, the true intent is to provide some type of user interface/input&#8230;</p>
<pre style="padding-left: 30px;">display dialog the [string variable]
  buttons {"Yes", "No"}
  default button 1
  with icon 1
  giving up after [(x) seconds]</pre>
<p>To customize this, replace [string variable] with a string in quotes or a string variable name. The buttons array will let you define the text of each button, if you wanted only an &#8220;Ok&#8221; use {&#8220;Ok&#8221;}. The default button 1 defines which button is automatically selected. Giving up after (x) seconds will close the dialog automatically if the user does not respond.</p>
<pre style="padding-left: 30px;">set my_variable to text returned of (display dialog the [string variable]
  buttons {"Yes", "No"}
  default button 1
  with icon 1)</pre>
<p>The above example allows you to use a &#8220;display dialog&#8221; as a user prompt, requiring some type of input that the remaining AppleScript can then process through logic, if, select, et cetera.</p>
<p>As you might have imagined, there are more options available for &#8220;display dialog&#8221;, including user input (text and multiple buttons). If you want more information on the &#8220;display dialog&#8221; method, check out this article on <a href="http://en.wikibooks.org/wiki/AppleScript_Programming/Advanced_Code_List/Display_Dialog" target="_blank">wikibooks</a>.</p>
<p>-Aaron Gilbert</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtrends.com/index.php/alert-display-dialog-in-applescript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine PDFs in Apple Automator</title>
		<link>http://www.devtrends.com/index.php/combine-pdfs-in-apple-automator/</link>
		<comments>http://www.devtrends.com/index.php/combine-pdfs-in-apple-automator/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 02:38:17 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[AppleScript]]></category>
		<category><![CDATA[Automator]]></category>
		<category><![CDATA[Folder Actions]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[zFeatured]]></category>
		<category><![CDATA[.scpt]]></category>

		<guid isPermaLink="false">http://www.devtrends.com/?p=489</guid>
		<description><![CDATA[There is a dream that I will become specialized in one area, with my aspirations pointed towards network engineering. However, until I get there I will be good at everything,...]]></description>
			<content:encoded><![CDATA[<p>There is a dream that I will become specialized in one area, with my aspirations pointed towards network engineering. However, until I get there I will be good at everything, a jack-of-all-trades that seemingly cannot understand the meaning of “no”. This type of quality creates immediate opportunity that pays well but slows growth in specialized areas. My father always told me that I make too much to be rich, which I did not understand until I realized that my current efforts, which provide well, have a negative effect on crossing over into the entrepreneur realm.</p>
<p>Enough of that…today I step out of my Microsoft box and into the world of Apple and create some Automator workflows to combine multiple PDFs, in alphabetical order, from one folder and output into another folder.</p>
<p><strong>Apple Automator!</strong></p>
<p>The requirements of this task was to combine single page PDF files automatically, with the only user intervention being manual placement of the PDFs into a folder. This was accomplished with a two step process: (1) create the workflow in Automator; and (2) trigger the workflow using Apple’s Folder Actions feature.</p>
<p>For those that just want to steal, download <a href="http://www.devtrends.com/downloads/combinepdfs.zip">here</a>.</p>
<p>For me, this was the first time I have ever used Automator, and frankly, I was quite impressed with the ease and intuitive design. The basic process involved was to find the PDF files, sort that list, combine the PDFs, move that file to another location, add a date to the output file name and then remove the original list of PDF files.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/01/fullworkflow.jpg"><img class="alignnone size-medium wp-image-490" title="fullworkflow" src="http://www.devtrends.com/wp-content/uploads/2010/01/fullworkflow-300x241.jpg" alt="fullworkflow" width="300" height="241" /></a></p>
<p>As shown in the screen shot above, this is easily accomplished using predefined Automator Actions, which includes the PDF actions. The first action is “Find Finder Items”, which allows us to specify a directory and other variables, such as file name specifics. In this case, I said all files that have a name that ends with .pdf. The second action is named “Sort Finder Items”, which takes the list as passed from “Find Finder Items”, notice the linking arrows between actions, and sorts them by name. The third will use the “Combine PDF Pages” action and will take the sort list of PDFs and make them into one PDF file.</p>
<p>The combined PDF is created in a temporary location with a random file name. The fourth action, “Move Finder Items” moves that file to a specific folder. Finally, the last in this group of actions is the “Rename Finder Items (Add Date or Time to Finder Item Names)”, which I have specified to add a date to the front of the file name.</p>
<p>The last two actions are in a different group for the reason that I do not want to work with the files/folders and provided by the last action in the previous group. Instead, similar to the start of the first group, the first action in this group is “Find Finder Items”, which allows us to specify a folder (same directory) and other variables, such as file name specifics. In this case, I said all files that have a name that ends with .pdf. The second and last action in this group moves those files to the Trash.</p>
<p><strong>Folder Actions</strong></p>
<p>Folder Actions employ AppleScripts that are triggered following a specific folder event. In this case and more common than not, the event is generally after files have been copied to the folder. As you may know, an AppleScript can accomplish a fair amount, including running Automator workflows and shell scripts. In this example, when we save the Automator workflow as a Plug-in, it will create two files, an application equivalent of the Automator workflow and an AppleScript that runs the Automator application.</p>
<p>To make an Automator workflow a folder action, you must save the Automator workflow as a “Plug-in”. At the Save As Plug-in dialog, you must choose ”Folder Actions” under “Plug-in for:” and then specify a folder to attach to. Name the plug-in accordingly and then click Save.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/01/saveasplugin.jpg"><img class="alignnone size-medium wp-image-493" title="saveasplugin" src="http://www.devtrends.com/wp-content/uploads/2010/01/saveasplugin-300x156.jpg" alt="saveasplugin" width="300" height="156" /></a></p>
<p>Now that you have added an Automator workflow, right click on the folder and click on “Attach a Folder Action”. Choose the appropriate AppleScript – usually the same name as the Automator workflow you just saved.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/01/whichscpt.jpg"><img class="alignnone size-medium wp-image-495" title="whichscpt" src="http://www.devtrends.com/wp-content/uploads/2010/01/whichscpt-300x208.jpg" alt="whichscpt" width="300" height="208" /></a></p>
<p>Assuming that Folder Actions are enabled and the script is set as a Folder Action to your folder, the Automator will run every time a file is copied into the folder.</p>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/01/folderactions.jpg"><img class="alignnone size-medium wp-image-494" title="folderactions" src="http://www.devtrends.com/wp-content/uploads/2010/01/folderactions-300x239.jpg" alt="folderactions" width="300" height="239" /></a></p>
<p><strong>Waiting until ALL Files are Copied</strong></p>
<p>If you copy more than one file at a time into the folder, you will likely run into an issue with this Automator/AppleScript combination where the script will begin before all files are completed copying. This is easily remedied by employing the following script example:</p>
<pre style="padding-left: 30px;">On adding folder items to this_folder after receiving added_items
  If folderReady(this_folder) then
    --((your Automator “tell” line goes here))
  End if
End adding folder items to

On folderReady(tFolder)
  Set myFolder to tFolder as alias
  Set firstSize to size of (info for myFolder)
  Delay 3
  Set newSize to size of (info for myFolder)

  Repeat while newSize ≠ firstSize
    Set firstSize to newSize
    Delay 3
    Set newSize to size of (info for myFolder)
  End repeat

  Return true
End folderReady</pre>
<p><a href="http://www.devtrends.com/wp-content/uploads/2010/01/combinepdfsscpt.jpg"><img class="alignnone size-medium wp-image-492" title="combinepdfsscpt" src="http://www.devtrends.com/wp-content/uploads/2010/01/combinepdfsscpt-300x245.jpg" alt="combinepdfsscpt" width="300" height="245" /></a></p>
<p>This script was taken from an example on <a href="http://macscripter.net/viewtopic.php?id=26564" target="_blank">MacScripter.Net</a>.</p>
<p>Aaron Gilbert</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devtrends.com/index.php/combine-pdfs-in-apple-automator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

