shellscript

Latest

  • One easy step to writing better bug reports

    by 
    TJ Luoma
    TJ Luoma
    08.03.2013

    As someone who uses a lot of different software and tends to find a lot of 'edge cases' I often find myself emailing software developers or filling out support tickets to either ask a question or report a problem. I know it would be helpful to include information about my Mac (both the hardware and the operating system) as well as precise information about the version of the software that I'm using. But, to be honest, I'm often too lazy to include it. Instead I tell them that I'm using the "latest version" of Mac OS X and the "latest version" of their software. But am I? Not too long ago I realized I was running 10.8.2 on a Mac that I thought was up to date. Or what if the developer comes across my email again in a month and can't remember what version was current then? They might think "Oh, well I think I fixed that since then" but if they have the exact version number for their app, they could find out much more easily. Which is so say: sometimes developers can be lazy too. So I came up with an easy way to include the information without requiring much work. How it looks With my script or Keyboard Maestro macro, I can automatically grab the version information for a specific app, as well as relevant information about my hardware and operating system. Here's an example of what the output would look like: (Note: I'm just using Fantastical as an example of an app, not suggesting that it's buggy. In fact, I don't think I've ever run into a bug with it. If that doesn't show up properly for you, checkout this direct link instead.) How it works bugreporthelper.sh is a shell script that you can use in Terminal. Just type bugreporthelper.sh and it will ask you if you need information about a specific app. If you say yes, it will include version information about the app. Either way it will output relevant information about your hardware and operating system. Bug-Report-Helper.kmmacros is a Keyboard Maestro macro which will do the same thing without requiring that you be in Terminal. It will prompt you for the name of the App (or PreferencePane) and look for version information. It will also insert your relevant hardware and operating system information. Installation bugreporthelper.sh - install anywhere in your $PATH and make executable (chmod 755 bugreporthelper.sh) Bug-Report-Helper.kmmacros - import into Keyboard Maestro (be sure to customize the keyboard shortcut and any other trigger you might want to add.) Notes The Keyboard Maestro macro tries to determine whether or not it can 'paste' the output of the command into the current application. If it can, it will do so and then restore your previous clipboard (whatever was on there before this script ran). If it can't, it will display a notification alerting you that it couldn't paste it, and keep the info on the pasteboard for you to manually paste wherever you want it. The method for determining whether or not it can paste depends on a menu item 'Paste' being enabled. Which is to say that it is not foolproof. Let me know if it behaves particularly badly in a particular app. bugreporthelper.sh will ask whether or not you want the information added to the clipboard. It will also save the information in a temp file (either in $TMPDIR or /tmp).

  • 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!

  • How to backup your Mac App Store apps

    by 
    TJ Luoma
    TJ Luoma
    01.23.2012

    The excellent ReadNow application, which allows you to easily read articles from your Instapaper and Read It Later articles on your Mac, has been pulled from the Mac App Store "...because of an infringement letter." Developer Michael Schneider added "[i]n my current situation I'm not allowed to provide any further information." The good news is that if you have already purchased the app, you can still download it from the "Purchases" tab in the App Store.app even though the iTunes page for ReadNow now leads to the message, "Your request could not be completed." However, as I waited for the Mac App Store to load on my iMac this morning, I found myself wondering "What would I do if I couldn't re-download it from the Mac App Store?" Fortunately I have the app on my MacBook Air, so I could go to /Applications/ReadNow.app and then select File » Compress "Read Now" (or control+click the app and choose "Compress" from the menu). Terminal option If you wanted to make a copy of ReadNow (or any app) on the command line, I believe that your best option is using ditto like this: sudo ditto -v --keepParent -kc ReadNow.app ReadNow.zip (I don't claim to be a ditto expert, but I can tell you that worked for me. The -k option tells ditto to make zip archives. The --rsrc --extattr and --qtn options are all the default, so they don't need to be explicitly specified.) I then copied the 'ReadNow.zip' file to my Dropbox so I would have it, just in case Apple removes the option to download it. This should work for all Mac App Store apps (Xcode and "Install Lion" excepted), but may not work for other apps which use an installer and may install other files besides the ones in /Applications/. Show me all of my Mac App Store apps Speaking of the command line, if you want to see a list of all the Mac App Store apps that you have installed on your computer, you can run this command in Terminal.app:find /Applications \-path '*Contents/_MASReceipt/receipt' \-maxdepth 4 -print |\sed 's#.app/Contents/_MASReceipt/receipt#.app#g; s#/Applications/##' Note: this won't show you Xcode, because Xcode is an unusual case. It is an installer which installs Xcode and then the installer is removed. An ounce of prevention There has been no indication that Apple will remove ReadNow from your list of Purchased apps, I just wanted to have a backup 'just in case.' Hopefully the "infringement letter" issue will be settled soon and it will be available again. However, if the iOS App Store is any indication, eventually jettisoned apps will become unavailable for re-download. Also, the upcoming sandboxing requirements may lead to some existing applications being removed. As always, it's good to have your own backups rather than relying on being able to re-download anything from "the cloud." I wrote a small shell script which will look in /Applications/ for any applications which have the Mac App Store receipt, and create a .zip file for each of them. (While I have tested it myself and it works for me, YMMV, use only at your own risk, etc.) To use it, download the script to your Desktop (or wherever) and then run: chmod 755 ~/Desktop/backupmas.sh And then run it via ~/Desktop/backupmas.sh (Obviously if you saved it somewhere else, use that path instead of ~/Desktop/.) It may ask for your administrator password. If so, that's the one you use to log in to your computer, not your Mac App Store password.

  • 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]