<?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; Automator</title>
	<atom:link href="http://www.devtrends.com/index.php/category/software-development/automator/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>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>

