Xcode / Adding User Scripts
Subtitles of the Movie
In this video we will be covering how to add User Scripts to Xcode. This is a powerful feature in Xcode that allows you to do all kinds of cool things using any scripting language the shell can handle. This means you can write your User Scripts in Ruby, Python, Perl, AppleScript, Bash - you get the idea. The User Script feature gives you the ability to add functionality to Xcode. Now I will not be covering the depth of the User Script functionality. This video is mainly to show you how to add Scripts to Xcode and also give us access to a couple of scripts I use all the time and that we will be using throughout this Tutorial. We're going to add two User Scripts to Xcode. The first one will create a pragma mark with a comment. The second will tidy up our Declarations in our Header files. A pragma mark is a way to organize methods in the method pop-up list. This makes organizing and locating your code much easier. The method pop-up list can get out of hand very quickly once your file has a decent amount of code in it. This is where the pragma mark comes in. The way I have the User Script configured there will be a small divider line next to each section and the section name just below it. I am using the word section here to denote a group of like methods. Here's what the Script will produce when invoked. The comment above the pragma is to easily find each section in the file. The -next to the word pragma mark is what makes the divider line and finally, the words MAIN METHODS are what is displayed below the divider line in the pop-up list. The second User Script is the Declaration Tidier. This is a convenient script I wrote to help me keep my code more structured. It is not necessary, but I like my code to be neat. The Declaration Tidier is a Ruby Script that formats a Header file's variable declarations. It is out of the scope of this tutorial. to explain the Script itself, so we will just use Copy and Paste. You can find a copy of both of these Scripts in the Exercise Files in the 0306 Folder. I've launched Xcode and for this task I'm just going to open up the MyCompanyName project that we created earlier. I'm going to click Open and when that comes up I'm going to minimize it for right now and I'm going to go up here to the top right-hand corner where the Scripts are and come down to Edit User Scripts. When this window pops open there are several Scripts that come provided by Apple that are installed with the Default Installation of Xcode. What I want to focus on, though, is adding our own Scripts to this, so I'm going to go to the bottom left, click the Plus sign, choose New Submenu, I'm going to double-click that and just call it MyScripts. Now I'm going to Control-drag out of this and put it down to the bottom and I'll go ahead and toggle that closed. I'll open up MyScripts, I'll select it and now I'm going to choose New Shell Script. And this is going to be the pragma creator, so I'll just call it pragma mark. I'm going to make this window just a little bit wider. I'm going to highlight the code that's in here and I'm going to paste what I have on the Clipboard. Now here again, this is a Ruby Script that I'm running. It's pretty simple if you know Ruby; if not, you can write this in any language that the Shell supports, as I stated earlier and there are a couple of options here that you'll want to check. We want to make sure that the Input is set to Selection. Now that's for this particular Script because when we type something into our file we're going to highlight it and then execute this Script so I want the selection to be chosen and then fed into this Script. So I'm going to use Selection, the Directory we can leave that alone and then down at the bottom for the Output we want it to Replace Selection. And then Errors, personally I like to show them in a Display in Alert and we can also assign a handy keyboard shortcut. So I'll double-click here and for me, I use Command-Shift and P for pragma mark. So we can go ahead and close this window. I'm going to bring back up the Project and in our NewName implementation I'm going to come down here, Enter and I'm going to call this initialize methods. And I can put this in all lowercase, all uppercase - it doesn't matter. The Script itself will turn this into all uppercase. So I'm going to highlight everything, and I do that using Shift-Command-left arrow and then I'm going to do Shift-Command-E and it should execute the Script for us. And now we have a nicely formed pragma mark. If we come just to the top of the Editor window and select our pop-up list we'll see now that INITIALIZE METHODS is the first one in the list. Our next Script is a bit longer, but we go through the same routine to set it up. So let's go ahead and go to the Scripts Menu at the top, choose Edit User Scripts. If you select pragma mark before you select New Shell Script at the bottom it'll put it right under that inside the same folder. And I'll just name this tidy. I'm going to highlight the code that's over here. I've already got the Script on my Clipboard, I'm just going to paste it in. I'm going to do the same thing. We're going to take Selection as the Input, the Directory is not necessary to change and Replace Selection and if there's any errors we want them to Display in Alert. And for this I'm going to put Command-Control-Shift and W. So, choose a shortcut that seems right for you, put it in there. Now that we have our second User Script in let's go ahead and close this window and you can also get to the User Scripts by coming over to the Scripts and now you can see there's a section called My Scripts and every Script that you put inside there will show up over here on the right-hand side. And then you can also see the shortcuts that you've provided for them. So now we have two User Scripts set up in our Scripts Menu. We'll be using these a lot in this tutorial. and now I think it's time for us to learn some Objective-C.
Tutorial Information
| Course: | Objective-C and Xcode Essential Training |
| Author: | Craig Williams |
| SKU: | 34088 |
| ISBN: | 1-936334-00-3 |
| Release Date: | 2010-02-22 |
| Duration: | 6 hrs / 68 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 