{"id":8698,"date":"2014-08-16T23:51:36","date_gmt":"2014-08-16T23:51:36","guid":{"rendered":"https:\/\/www.bartbusschots.ie\/s\/?p=8698"},"modified":"2014-08-16T23:51:36","modified_gmt":"2014-08-16T23:51:36","slug":"xkpasswd-2-service-with-automator","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2014\/08\/16\/xkpasswd-2-service-with-automator\/","title":{"rendered":"Creating an XKPasswd 2 OS X Service with Automator"},"content":{"rendered":"<p>It&#8217;s going to take me a few months to re-write the www.xkpasswd.net site so it uses the new XKPasswd 2 perl module. In the mean-time, thanks to the magic of Automator an OSX Services, Mac users can integrate XKPasswd 2 right into their OS with out very much effort at all.<\/p>\n<p>This blog post is intended as a follow-on post to my earlier <a href=\"https:\/\/www.bartbusschots.ie\/s\/2014\/08\/16\/xkpassws-2-absolute-beginners-guide\/\" target=\"_blank\">XKPasswd 2 beginners guide<\/a>. This post assumes you have followed the installation instructions in the beginner&#8217;s guide to install the XKPasswd 2 module, that you followed along with that post and created a script that generates passwords in a format of your choice, and that you have tested that script to be sure it works. In this post I&#8217;ll be using the final example script from the beginner&#8217;s guide as my pre-written script.<\/p>\n<p><!--more--><\/p>\n<p>Start by opening the <code>Automator<\/code> app located in <code>\/Applications\/Utilities<\/code> and creating a new document. When you create a new document Automator will give you a choice of document type, choose <code>Service<\/code>.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/create-a-new-automator-document-of-type-service.png\" alt=\"create-a-new-automator-document-of-type-service\" width=\"480\" height=\"464\" class=\"alignnone size-full wp-image-8722\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/create-a-new-automator-document-of-type-service.png 480w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/create-a-new-automator-document-of-type-service-300x290.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/p>\n<p>This will open a blank Automator workflow. There are three areas of interest in this window &#8211; the left pane allows us to search for actions to drag-and-drop into the workflow (1 in the screenshot below), the right of the screen is the Workflow area (2 in the screenshot), and above the Workflow area are the Service settings (3 in the screenshot).<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorWorkflowWindow.png\" alt=\"AutomatorWorkflowWindow\" width=\"494\" height=\"419\" class=\"alignnone size-full wp-image-8723\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorWorkflowWindow.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorWorkflowWindow-300x254.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p>Pay special attention to the search box at the top of the left pane. This is where we will search for the Actions we need to assemble our Workflow. Next to this search box are two toggle buttons, one labeled <code>Actions<\/code>, the other <code>Variables<\/code>. Make sure you have the one for actions selected as shown below:<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorActionsSearch.png\" alt=\"AutomatorActionsSearch\" width=\"393\" height=\"138\" class=\"alignnone size-full wp-image-8724\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorActionsSearch.png 393w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorActionsSearch-300x105.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/p>\n<p>Before we add our first action to our workflow we need to alter the Service settings above the workflow area. We need to set the <code>Service Receives<\/code> drop down to <code>no input<\/code> and leave the <code>in<\/code> drop down as <code>any application<\/code>.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorServiceSettings.png\" alt=\"AutomatorServiceSettings\" width=\"486\" height=\"69\" class=\"alignnone size-full wp-image-8725\"  style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorServiceSettings.png 486w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AutomatorServiceSettings-300x42.png 300w\" sizes=\"auto, (max-width: 486px) 100vw, 486px\" \/><\/p>\n<p>The first action we want to search for is <code>Run Shell Script<\/code>. When we find it we want to drag and drop it into the workflow area.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddRunShellScriptAction.png\" alt=\"AddRunShellScriptAction\" width=\"494\" height=\"418\" class=\"alignnone size-full wp-image-8726\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddRunShellScriptAction.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddRunShellScriptAction-300x253.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p>We now need to configure this action to run Perl code by changing the <code>Shell<\/code> drop down at the top of the action to <code>\/usr\/bin\/perl<\/code>:<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/ConfigureRunShellScriptAction.png\" alt=\"ConfigureRunShellScriptAction\" width=\"261\" height=\"203\" class=\"alignnone size-full wp-image-8727\" style=\"border:0px;\" \/><\/p>\n<p>Next, replace the default code in the body of the <code>Run Shell Script<\/code> action with your XKPasswd password generation script, minus the shebang line at the top of the script. I&#8217;ll be using the following code:<\/p>\n<pre class=\"brush: perl; title: ; notranslate\" title=\"\">\r\n# import best-practice standard perl modules\r\nuse strict;\r\nuse warnings;\r\n\r\n# import the XKPassed module\r\nuse lib &#039;\/usr\/local\/xkpasswd.pm\/&#039;;\r\nuse XKPasswd;\r\n\r\n# create an XKPasswd password generator object with out custom config\r\nmy $config_overrides = {\r\n    num_words =&gt; 4,\r\n    padding_characters_before =&gt; 2,\r\n    padding_characters_after =&gt; 2,\r\n    case_transform =&gt; &#039;ALTERNATE&#039;,\r\n    separator_alphabet =&gt; &#x5B;&#039;-&#039;, &#039;.&#039;, &#039;:&#039;]\r\n};\r\nmy $password_generator = XKPasswd-&gt;new(&#039;\/usr\/local\/xkpasswd.pm\/sample_dict.txt&#039;, &#039;APPLEID&#039;, $config_overrides);\r\n\r\n# use our generator to create and random password and print it\r\nprint $password_generator-&gt;password().&quot;\\n&quot;;\r\n<\/pre>\n<p>Once you have copied and pasted in your code you can test it by expanding the Results section  at the bottom of the <code>Run Shell Script<\/code> action (1 in the screenshot below), and pressing the <code>Run<\/code> button (2 in the screenshot) at the top-right of the window. You should seen your randomly generated password appear in the results section (3 in the screenshot).<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/TestRunAction.png\" alt=\"TestRunAction\" width=\"494\" height=\"389\" class=\"alignnone size-full wp-image-8728\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/TestRunAction.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/TestRunAction-300x236.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p>To make this workflow useful we now need to save the password somewhere. The most useful place I could think of was the clipboard, so next we search for and then drag and drop a <code>Copy to Clipboard<\/code> action. Be sure to add this action below the <code>Run Shell Script<\/code> action. Your workflow should now look something like:<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/FinishedBasicWorkflow.png\" alt=\"FinishedBasicWorkflow\" width=\"494\" height=\"319\" class=\"alignnone size-full wp-image-8729\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/FinishedBasicWorkflow.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/FinishedBasicWorkflow-300x193.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p>The workflow is now finished and ready to use. Save it and give it a name that makes sense to you. I called mine <code>XKPasswd (Example)<\/code>. You will now find this Service available in the <code>Services<\/code> sub-menu in any app that supports Services (just about every OS X app). The Services menu item is always located in an app&#8217;s main menu &#8211; the one with the same name as the app in bold right next to the Apple logo at the top-left of the screen.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/UsingTheService.png\" alt=\"UsingTheService\" width=\"494\" height=\"310\" class=\"alignnone size-full wp-image-8730\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/UsingTheService.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/UsingTheService-300x188.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<h2>Optional Enhancements<\/h2>\n<p>If you have <a href=\"http:\/\/growl.info\" target=\"_blank\">Growl<\/a> installed you can add in a <code>Show Growl Notification<\/code> action at the end of the workflow and give it a sensible title. If you leave the content area blank then the output of the workflow will be used as the body of the message &#8211; in this case, the automatically generated password.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/OptionalGrowlNotification.png\" alt=\"OptionalGrowlNotification\" width=\"494\" height=\"481\" class=\"alignnone size-full wp-image-8731\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/OptionalGrowlNotification.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/OptionalGrowlNotification-300x292.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n<p>Now, each time you run the service you&#8217;ll see a popup like the one below when it is finished running and the password is waiting for you in the clipboard.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/GrowlNotification.png\" alt=\"GrowlNotification\" width=\"346\" height=\"156\" class=\"alignnone size-full wp-image-8732\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/GrowlNotification.png 346w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/GrowlNotification-300x135.png 300w\" sizes=\"auto, (max-width: 346px) 100vw, 346px\" \/><\/p>\n<p>Finally, you can also assign a key-stroke to the Service in the <code>Keyboard<\/code> applet in the OS X System Preferences. You&#8217;ll find the relevant interface for adding a shortcut of your choice in the <code>Shortcuts<\/code> tab. To find the Service you&#8217;ll need to select <code>Services<\/code> in the left pane and scroll down to the <code>General<\/code> section in the right pane.<\/p>\n<p style=\"text-align:center;\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddShortcut.png\" alt=\"AddShortcut\" width=\"494\" height=\"444\" class=\"alignnone size-full wp-image-8733\" style=\"border:0px;\" srcset=\"https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddShortcut.png 494w, https:\/\/www.bartbusschots.ie\/s\/wp-content\/uploads\/2014\/08\/AddShortcut-300x269.png 300w\" sizes=\"auto, (max-width: 494px) 100vw, 494px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s going to take me a few months to re-write the www.xkpasswd.net site so it uses the new XKPasswd 2 perl module. In the mean-time, thanks to the magic of Automator an OSX Services, Mac users can integrate XKPasswd 2 right into their OS with out very much effort at all. This blog post is [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[12,446],"tags":[269,410,26,267,456,458],"series":[],"class_list":["post-8698","post","type-post","status-publish","format-standard","hentry","category-computers-tech","category-sysadmin","tag-automator","tag-hsxkpasswd","tag-os-x","tag-perl","tag-services","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-2gi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/8698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=8698"}],"version-history":[{"count":2,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/8698\/revisions"}],"predecessor-version":[{"id":8734,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/8698\/revisions\/8734"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=8698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=8698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=8698"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=8698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}