shell script

Latest

  • Alfred, Dropbox, Hazel, and Markdown? All in a day's work

    by 
    TJ Luoma
    TJ Luoma
    06.09.2014

    "How does a geek-pastor use a computer?" is a question that I assume a lot of people ask themselves, although they probably don't. But if they did, this would be one answer: I need to make a list of the readings and sermon titles for the church secretary and choir director, so one can make the weekly worship guide, and the other can plan the music. I put my schedule up as a web page, like this: The reading is linked to the text so that everyone involved can easily access the text (I'll need it for sermon preparation, and the choir director will need it to see what the text is about so she can try to match it to potential hymns/anthems). Which reminds me: I have an Alfred shortcut for looking up Bible passages from the NRSV: A month year of Sundays Today the church secretary reminded me that I needed to update the list, as I had gone through all of the previous weeks that I had scheduled (oops). I decided that it was a good time to make a list of all of the Sundays between now and the end of 2015; that way I could start to fill in the dates of special events, vacation, etc. So... how would a sane person make a list of Sundays? Maybe look at a calendar? Try to figure them out in your head? Not me. I wrote a shell script: Actually I made my loop a little more complex, in order to give me the Markdown formatting that I wanted for the list: That gave me a nice, clean "unordered list" in HTML. The first line under the date will be the Scripture Reading. The second line will be the sermon title. The third line (if any) will be any notes about that particular Sunday. Fortunately I didn't have to include all of that in the output because I could use CSS' "generated content" feature to automatically add it for me: Markdown Markdown is a simple way of writing plain text which can be automatically translated into HTML (the language of web pages). But I don't want to have to remember to manually update the HTML page every time I edit the plain text Markdown file. So I have Hazel do that for me: Translation: Any time the file worship.mmd in the folder worship.luo.ma is updated, run that script on it, which will automatically convert it from Markdown to HTML and save it as the file index.html. The ~/Sites/worship.luo.ma/ folder is linked to my Dropbox, so I can edit that file on any of my Macs (or on my iPad or iPhone), and every time I press "Save" the website version will update, usually in less than a minute (the amount of time it takes Dropbox to sync and Hazel to notice the file has changed). (Have I mentioned how much I love having a MacMiniColo server? Because I do. A lot.) You can see the result for yourself at http://worship.luo.ma, or view the Markdown file at http://worship.luo.ma/worship.mmd. Now... if only I could get Hazel to clean up my actual desktop.

  • Microsoft Office 2011: Unattended download and installation

    by 
    TJ Luoma
    TJ Luoma
    12.22.2013

    I own Microsoft Office 2011 and wanted to make the process of installing it easier, since I own several Macs and have this habit of reinstalling OS X frequently. So I wrote a shell script because that's what I do to solve these sorts of problems. The best part of the script is that it does not require you to have any of the necessary parts. You do not need your installation DVDs or to have downloaded the latest updates. Once you start the script, it will download everything that you need directly from Microsoft's servers and install all of them for you. The entire process can be left completely unattended after it is started. The length of time for the install process depends on your network speed. The total download size is 1.16 GB (aka 1.08 GiB or 1,157,294,708 bytes) as of today. Once the script is finished, the necessary files will be available in ~/Downloads/Office2011/ which you can then copy to any of your other Macs. The script will look for files in ~/Downloads/Office2011/ and if they already exist, it will not download them again. The script will install the following Microsoft Office for Mac 2011 with Service Pack 2 (14.2.0) Office2011-1439Update_EN-US.dmg Microsoft Error Reporting for Mac 2.2.9 Update Microsoft AutoUpdate for Mac 2.3.6 Update Once the script finishes, it will run "Microsoft AutoUpdate.app" to make sure that there are not additional updates necessary. How to use this script (The Easy Way) There is an installer available. This is an Automator app which will simply download and run the office2011.sh for you. Please note that because of Gatekeeper settings in OS X, you may have to right click the installer app and choose open in order to get it to launch. Once you confirm that you want to download and install Office, the rest will take place behind the scenes. You will be prompted to enter your administrator password because the installation cannot continue without it. Update 2013-12-29: I have updated the information at GitHub to include a walk-through of using the GUI Installer application. If you tried it before and it did not work, please try again. How to use this script (The Nerdy Way) A better way to use this script requires the Terminal.app which is found in /Applications/Utilities/. Using Terminal is like getting under the hood of your car: it shouldn't scare you, but you shouldn't just go randomly poking around either. Once in Terminal.app, you will need to: 1) download the installer script (this should all be one line): curl --remote-name "https://raw.github.com/tjluoma/office2011/master/office2011.sh" 2) make it executable chmod 700 office2011.sh 3) run the script. sudo ./office2011.sh Note that you will have to enter your administrator password to use sudo. After Installation Once the installation finishes, Microsoft's Auto Update app will be launched. This will make sure that there are no additional updates needed which have been released since this script was written. It is also a good time to set the auto-updater to run as often as you'd like it to run automatically. I recommend once a week. Disclaimer and Final Notes As far as I know, installing Office 2011 this way gives you the same result as if you had installed using OS X's Installer.app on the various pkg files directly. However, there's a chance that I could be wrong. Use at your own risk, etc. The only difference that I am aware of is that installing Microsoft Office using these scripts does not automatically add the apps to your OS X Dock. This is considered a feature, not a bug. Using this installer will not give you free access to Microsoft Office 2011. The first time you run it, you will be asked for your license code or Office 365 subscription information. If you do not have either of those you can use a trial version of Office, but I do not know what kinds of limitations it places on you. Discovering those is left as an exercise to the reader. Upon installation, you may want to read my article on how to Reduce visual clutter in Microsoft Word.

  • DevJuice: Embed shell commands in an .app wrapper and other dev command line tricks

    by 
    Erica Sadun
    Erica Sadun
    07.26.2013

    The other day, I was chatting with some Mac devs. One of them asked how to set up an App wrapper to run a custom shell script from the desktop. I use a script called appify created by Thomas Aylott and Mathias Bynens. It does little more than create the app bundle folder structure (top level.app/Contents/MacOS), copy the shell script, and then set its execute flag. As Aylott and Bynens discuss on a related blog post and its comments, other solutions like Wilfredo Sanchez's DropScript. (Sanchez also offers a sweet dmg building script at the same URL.) While I'm on the topic of my favorite command line solutions, I'd be remiss if I didn't mention lns, the best way to create symbolic links. It's perfect for drilling into Xcode folders. I always keep a link around to the top level of the latest SDK: lrwxr-xr-x 1 ericasadun staff 106 Jul 8 18:58 sdk@ -> /Applications/Xcode[REDACTED]-DP3.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS[REDACTED].sdk and a second link to bring me right to UIKit headers. lrwxr-xr-x 1 ericasadun staff 71 Jul 8 18:58 headers@ -> /Users/ericasadun/sdk/System/Library/Frameworks/UIKit.framework/Headers By staging these as two items, I never have to update my headers symbolic link. I just modify the "sdk" one as new betas roll out. Another great dev symbolic link to have on hand takes you right to the simulator folder, so you can preload photos, access files, and so forth: lrwxr-xr-x 1 ericasadun staff 62 Nov 18 2012 sim@ -> /Users/ericasadun/Library/Application Support/iPhone Simulator Got some favorite dev-centric command line utilities? Drop a note in the comments.

  • Terminally Geeky: How to tell if a script is being called from launchd

    by 
    TJ Luoma
    TJ Luoma
    02.25.2013

    Warning: command line geekiness ahead. When writing shell scripts, I often send interactive output to the user via echo to give feedback or information. But if the shell script is called on a schedule via OS X's behind-the-scenes process launcher launchd, rather than from a Terminal session, chances are that I won't ever see that message. Fortunately, it's relatively easy to figure out if a shell script has been called from launchd or not, simply by checking the $PPID variable. (Note: this works in zsh and may work in bash as well. If it doesn't work in your shell, this is a good time to upgrade to zsh.) But how can I make sure my messages are seen if a shell script has been called via launchd? For that I use the Swiss Army Knife of notification tools, Growl. Specifically, I use the growlnotify optional package, which allows me to send Growl notifications from shell scripts. For example, imagine that I wrote a script where I wanted to tell the user that a certain process had succeeded or failed. Normally I might just use echo 'SUCCESS!' or echo 'FAILED!' But now, instead of 'echo' I use a function called msg (short for 'message'). If the script was called from launchd then msg will use growlnotify, but if the script was called from the command line, it will just use echo. Here's how that works: This method is not foolproof. For example, if you call a shell script from launchd and that shell script calls another shell script, it might not realize that it was originally executed from launchd. In practice, I have not run into that problem, but it did seem worth mentioning.

  • Preparing high-res icon files with Automator

    by 
    Ben Waldie
    Ben Waldie
    12.31.2012

    This week's post is for all those Mac app developers out there. If you're not an app developer yet, it's a new year, and I'm sure becoming an app developer is at at the top of your resolution list. In any case, if you plan to develop apps, then you need to prepare icons for those apps. Apple provides very specific requirements for generating high resolution icons, which you can find listed on the Apple Developer Connection website. In the past, Xcode came with a tool named IconComposer, which could be used to generate icon files. This tool, however, didn't generate high res icon files, so Apple now discourages its use. Instead, Apple provides instructions for generating icons using an iconset, a folder of image files that can be converted to an icon file using either Xcode or the iconutil command line tool. There are, of course, other ways of creating icons. In this post, however, we'll focus on an iconset. To manually generate an iconset folder, you start by creating a square image to serve as your icon. Next, you generate versions of the image scaled to 16x16, 32x32, 128x128, 256x256, 512x512. These scaled versions are to be named as follows: icon_16x16.png icon_16x16@2x.png icon_32x32.png icon_32x32@2x.png icon_128x128.png icon_128x128@2x.png icon_256x256.png icon_256x256@2x.png icon_512x512.png icon_512x512@2x.png You place all of these images into a folder named FolderName.iconset. Then, you can process it with iconutil to generate an icns file. Sure, creating scaled images and processing them isn't too difficult, but it's a lot of repetitive work. If you create icons regularly, it can get tedious. You're probably thinking, surely, Automator can be used to streamline the process. It can. Here, we'll create an application workflow you can run anytime you want to convert an image to an icon file. Let's get started... Note: If you're an iOS developer, feel free to adjust the workflow below accordingly. Creating the Workflow Launch Automator and create a new Application workflow. Next, insert and configure the following actions. Note: This workflow is pretty long and repetitive. It's easy to get lost. If you get stuck, don't worry, you can download the complete workflow here, along with a sample icon image. 1. Ask for Finder Items - Set this action to ask for an image file at least 1024x1024 in size. 2. New Folder - Set this action to create a folder named MyIcon.iconset on your Desktop. 3. Set Value of Variable - From the popup menu in this action, choose New Variable and create a variable named iconset folder. 4. Get Folder Contents 5. Change Type of Images - Set this action to convert images to PNG format. 6. Scale Images - Set this action to scale the image to a size of 1024 pixels. 7. Rename Finder Items - Set this action to name a single item's basename only to icon_512x512@2x. This represents a 1024 image (i.e. a Retina display ready size) when the icon is created. Note that this action's title changes in the workflow to reflect the type of naming, in this case Name Single Item. 8. Duplicate Finder Items 9. Scale Images - Set this action to scale the image to a size of 512 pixels. 10. Rename Finder Items - Set this action to name a single item's basename only to icon_512x512. 11. Duplicate Finder Items 12. Rename Finder Items - Set this action to name a single item's basename only to icon_256x256@2x. 13. Duplicate Finder Items 14. Scale Images - Set this action to scale the image to a size of 256 pixels. 15. Rename Finder Items - Set this action to name a single item's basename only to icon_256x256. 16. Duplicate Finder Items 17. Rename Finder Items - Set this action to name a single item's basename only to icon_128x128@2x. 18. Duplicate Finder Items 19. Scale Images - Set this action to scale the image to a size of 128 pixels. 20. Rename Finder Items - Set this action to name a single item's basename only to icon_128x128. 21. Duplicate Finder Items 22. Scale Images - Set this action to scale the image to a size of 64 pixels. 23. Rename Finder Items - Set this action to name a single item's basename only to icon_32x32@2x. 24. Duplicate Finder Items 25. Scale Images - Set this action to scale the image to a size of 32 pixels. 26. Rename Finder Items - Set this action to name a single item's basename only to icon_32x32. 27. Duplicate Finder Items 28. Rename Finder Items - Set this action to name a single item's basename only to icon_16x16@2x. 29. Duplicate Finder Items 30. Scale Images - Set this action to scale the image to a size of 16 pixels. 31. Rename Finder Items - Set this action to name a single item's basename only to icon_16x16. 32. Get Value of Variable - Set this action to get the iconset folder variable you created way back at the beginning of the workflow. You also need to set this action to ignore its input, so it doesn't continue processing the 16x16px image from the previous action. Select Action > Ignore Image. 33. Run Shell Script - Set this action to pass input as arguments. Then, enter the following command: iconutil -c icns "$@" Preparing the Run the Workflow Before you begin running the workflow, you need an image you can convert to an icon. Note that iconutil requires this image to be square. So, make sure it is. If you really want to be sure it's square, you can insert a Crop Images action between steps 5 and 6, and configure it to crop to 1024x1024, scaling to the shortest side before copping. However, if your image contains transparency, this action will remove it. So, it's best to prepare your starting image as a square. Running the Workflow When you run the workflow, you're first asked to choose an image. Select the desired image and click Choose. The workflow runs, an iconset folder is created on your Desktop and passed to iconutil, which generates an icns file. You're ready to plug your icns file into your app. Now, any time you need to generate an icon, just create a square image and trigger your workflow. Happy New Year and Happy App Developing!

  • Daily Mac App and Friday Favorite: CodeRunner

    by 
    TJ Luoma
    TJ Luoma
    01.06.2012

    CodeRunner is a text editor for people who write code. It comes with built-in syntax highlighting for AppleScript, C, C++, Java, JavaScript (Node.js), Objective,C, Perl, PHP, Python, Ruby, or shell scripting. I have been using it for a few months, mostly for writing shell scripts, and love it. The color coding makes it easy to immediately tell when I am missing a quote or some other basic syntax flaw, which means making fewer mistakes. It automatically applies templates (which are editable) to new files, so whenever I start a new shell script, it automatically includes the header lines and some other settings that I always use. In fact, it defaults to using the same template as the last kind of file you saved, so if you tend to write in one language, it will automatically pick that language and template. Otherwise you can choose manually. One of my favorite aspects of it is a built-in terminal console, which lets you test the script without switching over to Terminal or iTerm. A recent update even made it possible to define arguments, compilations flags, or arguments before sending it to the built-in console. The console automatically appears when needed, and can be shown/hidden with a keyboard command. CodeRunner offers "completions" (for example: automatically adding a closing bracket when you open one), but it also lets you turn that off if you don't like it. There are even themes to change the color combinations. I tend to prefer a simple black-on-white, but there are several dark background/light type options as well. In Lion, CodeRunner supports Autosaving, Versions and Fullscreen mode. It also supports "tabs" (multiple documents in one window) if you want to use them, but doesn't require them. Generally I don't like tabs in any apps except web browsers, but it is handy to have the option to keep related files together when working on separate projects. CodeRunner isn't going to replace a complex program like BBEdit with integrated FTP and a multitude of configurable options, but it doesn't need to. I own, use, and love BBEdit, but I use CodeRunner exclusively for writing shell scripts now, and vastly prefer it for that purpose. If you want a lightweight tool for writing scripts, you should definitely check it out on its home page or download it from the Mac App Store for $10. (Also: if you use regular expressions -- especially if you have trouble with them -- be sure to also checkout Patterns, an app by the same developer which makes it much easier to see how they expand. I'll probably review that more fully another day but it's currently on sale for $3 instead of $5, so you might want to check it out soon.)

  • Revisiting the randomized signature AppleScript, now with API goodness

    by 
    Brett Terpstra
    Brett Terpstra
    02.10.2009

    When I shared an AppleScript last week which created randomized signatures using TextExpander, I promised to revisit it when the iheartquotes.com API started working again. It's working right now, but it seems to go up and down; check this link before you try the script and make sure you get a quote, not an error message. As before, when used with a TextExpander snippet set to the AppleScript type, this script will provide a random quote, this time pulling it from the Internet rather than our own list. The script is essentially the same, so I'm only going to explain the part which changed; visit the previous article for a rundown on the rest: set _date to do shell script ¬ "date +'Sent on %A, %b %d at %l:%M %p'|tr -s ' '" set _quote to "" try set _quote to do shell script "curl -s \\ 'http://www.iheartquotes.com/api/v1/random?source=macintosh&max_lines=4'" set {astid, AppleScript's text item delimiters} ¬ to {AppleScript's text item delimiters, return} set _quote to paragraphs 1 through -3 of _quote as string set AppleScript's text item delimiters to astid end try set _out to " -Brett " & _date & " ________________________________ Brett Terpstra | myemailaddress@mywebsite.com The Unofficial Apple Weblog | http://www.tuaw.com " & _quote return _out The curl call Here's the part which makes this more interesting than it was before: set _quote to do shell script "curl -s \\ 'http://www.iheartquotes.com/api/v1/random?source=macintosh&max_lines=4'" First off, that really should be all one line, but is split here for formatting purposes. You can remove the double backslash (\\) and merge the lines together if you like. What this does is use AppleScript's ability to run shell scripts (Unix commands) to make a curl call to retrieve a response from the iheartquotes API. Note the "try" statement surrounding that section; it will just return our regular signature without the quote if anything goes wrong. If it gets a response, it stores it in a variable (_quote) and then trims off the last line, like this: set {astid, AppleScript's text item delimiters} ¬ to {AppleScript's text item delimiters, return} set _quote to paragraphs 1 through -3 of _quote as string set AppleScript's text item delimiters to astid The first two lines (actually one line) are shorthand for setting "AppleScript's text item delimiters" -- the character that AppleScript uses to separate text items in a block of text -- to the return character and store the current text item delimiter in a variable so we can restore it with the last line. Using the paragraphs command, which automatically uses the return key to separate paragraphs, we turn the quote into a list of paragraphs, starting with the first and ending with the third from last. This is because the last two "paragraphs" are always an empty line and a permalink (even if you use the API's parameters to turn off the permalink). The last part of that line, "as string", is where the text item delimiters come into play, putting the list items back together with a return after each one, reconstructing the original formatting. Note: You could do the same thing by adding |sed -n '$!p' to the end of the do shell script line, right before the last double quote. I suppose you might as well, as long as you're dipping out to the shell anyway ... From there it's the same as before, just building a final output string from the pieces we've created. This method provides a much wider variety of randomized signatures than constructing your own list by making use of a small section of the wide array of data on the interwebs. The use of curl in an AppleScript can open a lot of possibilities, such as posting to Twitter in various ways. or shortening urls on the fly. Have fun.

  • Random Signatures with TextExpander and AppleScript

    by 
    Brett Terpstra
    Brett Terpstra
    02.02.2009

    TextExpander, a $29.95US utility which inserts snippets of text or images when you type a preset string of characters, has long been a TUAW favorite. I only recently learned that it can run AppleScripts within a snippet, which opened up a world of fun for me. Here's a quick script demonstrating how AppleScript can be used to randomize quotes within your email signature. Create a new TextExpander snippet and set the content type to "AppleScript." Copy and paste the code that follows into the snippet content box. I'll run through what it does in at the end. Read on for the script!

  • AppleScript: Integrating shell scripts

    by 
    Cory Bohon
    Cory Bohon
    06.23.2008

    We've talked about AppleScript how-tos before. AppleScript is fun and all, but what if you already know how to write shell scripts? Well, did you know that by integrating shells scripts into AppleScript, you can create simple applications that do useful things? It's true, and I am going to show you how. Continue reading to learn how to integrate shell scripts into AppleScripts.

  • Platypus: create Mac applications from Ruby, Perl, shell scripts, etc.

    by 
    Mat Lu
    Mat Lu
    05.08.2007

    MacResearch has posted a nice two part tutorial on using Platypus, a neat utility that allows you to "create native, flawlessly integrated Mac OS X applications from interpreted scripts." This include shell scripts, Perl, Ruby, Python, etc. With Platypus you can turn command-line only tools into full fledged, free-standing Mac applications that support drag and drop and even basic graphical feedback. Obviously this isn't going to make for a full GUI application, but it will allow you easily to create self-contained OS X applications that you can double-click to run. Especially for those in the science fields, and even for beginning (Ruby) script programmers like myself, Platypus makes it possible to get basic (and even not so basic) Mac app up and running very quickly.Platypus is a free download from Sveinbjorn Thordarson, and donations are requested.[Via MacResearch]Update: fixed headline

  • ScriptExport: iPhoto Plugin Allows Shell Script Manipulation

    by 
    Mat Lu
    Mat Lu
    03.16.2007

    Today is apparently iPhoto export plugin day as in addition to the Facebook iPhoto plugin and updated FlickrExport plugin mentioned earlier, John Gruber at Daring Fireball brings our attention to ScriptExport from Nathaniel Gray. ScriptExport "allows you to 'export' your iPhoto pictures and movies by processing them with shell script." A script can can be done either at the start of the export, on a per item basis, or at the end of the process. He has some example scripts in the documentation. This is probably only for the command line ninjas among us (i.e. not me), but it looks like it could be very useful if you're the type that would rather type mkdir than hit Cmd + Shift + N.ScriptExport is a free download.[Via Daring Fireball]