If you use the Terminal for any amount of work, sooner or later you will probably come across the need to use SSH. SSH is a transfer protocol used to send data securely over the internet. It stands for Secure Shell. SSH has the ability to use keys, which will allow one computer to access another computer using really long encrypted password. Keys are considered more secure than passwords because they are much longer in length and can not be easily broken via brute force methods, like most passwords can.
Mac OS X is built on UNIX, and has SSH installed on all versions of Mac OS X by default. No extra software is necessary to use SSH on Mac OS X. But to generate an SSH key on Mac OS X takes a few steps.
Step 1: Open Terminal
First, open the Terminal app on your computer. The Terminal app resides in /Applications/Utilities. The easiest way to launch it is to open Spotlight with Command-Spacebar, and then start typing Terminal. You should see the app as the top selection. Click enter to launch it.
The Terminal should open using your home directory as the default directory, but in case it doesn’t, do this:
Step 2: Check for existing keys
Now, inside your home directory, let’s see if you have any SSH keys already created:
ls -al ~/.ssh
If this is a brand new installation of Mac OS X, the .ssh directory might not even exist. (Pro Tip: Directories that have a period in front of them are hidden by default, meaning, you won’t see them listed in the Finder.)
If you have any keys already created, most likely they would be files listed with a “.pub” extension. Some default key file names are:
If you don’t have any keys already created, go to the next step to create one.
Step 3: Generate a new SSH key
In the terminal, enter this command:
ssh-keygen -t rsa -C "firstname.lastname@example.org"
Substitute your email address for the one listed above.
Hit enter, and you should see the following message returned:
# Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
Press enter to create a key using the default settings.
Next, you will be asked to enter a passphrase. A passphrase is essentially a password that will be used every time the key itself is used. It’s another layer of security. You can hit enter and bypass using a passphrase, but you are effectively limiting how secure your connection will be. Keys can be stolen, and without a passphrase, someone could use your keys without any consequence. If you forgo using the passphrase, be warned.
Enter passphrase (empty for no passphrase): [Type a passphrase] # Enter same passphrase again: [Type passphrase again]
Once you enter your passphrase (or click enter to bypass entering a passphrase) you should see a confirmation that your key has been created, which will look something like this:
Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db email@example.com
Step 4: Move key to remote server
Now that you have created a key, let’s move it to a remote server so we can connect to it.
First you will need to know the username of the account on the remote machine you will be connecting to, and you will need to know the remote machines address (either a fully qualified domain name, or an IP address).
Now, in the terminal, you will issue this command:
cat ~/.ssh/id_rsa.pub | ssh firstname.lastname@example.org "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
If you are on a Linux system, you can try the following shortened command that does the same thing.
To test that everything worked correctly, try to initiate an SSH session from your machine to the remote machine via the terminal. If it works, you should be prompted for your passphrase (unless you opted not to enter one) and once entered correctly, be looking at the prompt for a shell on the remote machine.
Category: OS X,Tutorial,UNIX,Web Development
One of the cooler, often overlooked features of OS X Mavericks is it’s ability to scan a signature using your iSight camera, and the ability to use that signature to digitally sign a PDF document. This tutorial will walk you through setting this up. To get started, you’ll need a couple of things. (more…)
Category: Apple,Mavericks,OS X,Tutorial
Installing MySQL on OS X can be as easy or as complex as you want it to be. On the complex side, since OS X is UNIX, you could install from source and build your own package. Or your could use the Homebrew package manager to install completely from the command line.
The easiest way to get MySQL installed on OS X is to use the packages built by MySQL. The packages are offered up in tarball or in DMG. Getting the DMG is going to give you the most ‘Mac’ like install. For the purpose of simplicity, this is the method we will use in this example. (more…)
Category: PHP,Tutorial,UNIX,Web Development
About the only thing I miss about hosting my sites on a Linux webhost running Cpanel is the ability to auto update WordPress sites with one click. Granted, my experience with WordPress auto update is a mixed bag. When it works, it is all kinds of awesome. When it doesn’t, it leaves your site inaccessible while you figure out exactly what went wrong and how to fix it.
Since I’ve moved my webhosting to my own colocated Mac mini, I’ve missed the ease of auto updating my WordPress sistes. Downloading the latest WordPress update is such a pain, especially when you have to update several sites each time an update is released. Thankfully, with a little command line code, you can perform this action quite quickly and easily.
First start by SSHing in to your Mac OS X Server. Once in, you will use the ‘curl’ command to download the latest version of WordPress. Thankfully, WordPress keeps it’s latest version at the same URL regardless of version number, so once you make note of this URL, it shouldn’t change.
curl -O http://wordpress.org.latest.zip
Once the download is complete, unzip the archive.
Then change your directory to the wordpress folder.
Now, the final piece is to copy the files in the WordPress folder to the location of your WordPress install. When you do this, make sure you use the ‘-pr’ modifier for the copy command so the copy maintains permissions (-p) and is recursive (-r).
cp -pr * /Library/Webserver/Sites/your-website-name/.
That’s it. You’re done. One of the nice things about the *NIX copy command is that it won’t obliterate directories on the destination if they aren’t present in the source directory. So when you copy the wp-content to the new location, your themes and plugins are all left intact.
In our next tip, we’ll show you how to combine all of these steps in to one script and further automate it.
In previous versions of Mac OS X, Apple included PHP, but usually an older version that what was available at the time of the operating systems release. In Snow Leopard, Apple has included the just released PHP 5.3. Compared with other Mac OS X releases, Apple has made getting PHP and MySQL up and running much easier than before. (more…)
Category: Tutorial,Web Development
Q: I am constantly fat fingering “Command – Escape”, which invokes Front Row. It’s irritating. Any way I can disable Front Row?
A: The best solution is to just disable the keyboard shortcut for Front Row. To do this, open System Preferences, go to Keyboard & Mouse and click on the “Keyboard Shortcuts” tab.
The very first option is “Hide and Show Front Row” . Uncheck the “on” check box, and you’re finished.
Having problems with GrowlMail not working in the latest version of Growl? You’re not alone. There seems to be a bug that has yet to be documented in the release notes. If after installing the GrowlMail plugin you still don’t have the GrowlMail prefpane in Mail.app, run these commands in the terminal:
defaults write com.apple.mail EnableBundles 1
defaults write com.apple.mail BundleCompatibilityVersion 2
Restart mail, and voila – GrowlMail should now work.
Last week, I went through a tutorial that outlined how to create a very simple AppleScript Studio application. I went through the basics of using Project Builder and Interface Builder, how to set up a preliminary project and interface, and how to get text to display in a dialog and in a text field, all through the language of AppleScript. This week, I’m going to get a little more detailed into one of the most important topics of AppleScript Studio: referring to objects. In going through this tutorial, you’ll learn how to refer to and manipulate various objects like buttons, text fields, and other standard interface objects so that you can create your application with an interface that is best tailored for the features that you want to implement.
1. If you haven’t already done so, create a new project, and open up the “MainMenu.nib” file. (Remember that to do so, you must first have the “Files” tab selected. Then, click the disclosure triangle next to the Resources folder that is under the Groups & Files list on the left side of the main Project Builder window. Inside that folder, you should find the “MainMenu.nib” file that we want to open; simply double-click it, and Interface Builder will launch and open the file.)
Remember that, by default, the interface is set up to have one window. In the MainMenu.nib window (not the MainMenu.nib – MainMenu window), there should be a little mini-icon of an Aqua window, and should be named simply “Window”.
2. To change the name, simply double-click the word “Window”. For now, change it to “Main Window”.
The MainMenu.nib window
Keeping track of a window’s title in Interface Builder is confusing; this is mainly because, when using AppleScript Studio, a window has four different names. The first name is the one in the MainMenu.nib window — this will be called the “display name”. The second name is the actual title of the window which appears in the window’s title bar when you run the application — this will be called the “window title”. The third name is the window’s “auto save name”; more about this later. The fourth and last name is the name that you use to refer to the window when creating your AppleScript code — this will be called the “AppleScript name”. We just set the display name; the latter three names you have to set in the window’s Info window.
3. Open up the info window, by selecting the “Main Window” icon in the MainMenu.nib window. Then press Command-Shift-I, or select Show Info from the Tools menu.
4. When the NSWindow Info window comes up, select “Attributes” from the pop-up menu at the top of the window, or press Command-1.
Here we can set the window title: see the text field next to where it says “Window Title:”? That’s the window title, which appears in the title bar of the window. For simplicity, let’s set this title to have the same name as the display name.
5. Change the window title to “Main Window”.
Right under the Window Title: text field is the Auto Save Name: text field. This name is very important, because you can gain a critical feature simply by filling in this field. If you do, the location of the window on the screen will automatically be saved to preferences when someone quits your application; you don’t have to write a single line of code — all you have to do is fill in this field.
6. Set the auto save name to be the same as the window title and display name: “Main Window”.
The MainMenu.nib window and the NSWindow Info window showing three different names assigned to a single window
7. Now, in the popup menu at the top of the NSWindow Info window, select “AppleScript”, or press Command-6.
You should now see a single text field right below the popup menu that is simply named “Name:”. This is the AppleScript name of the window.
8. Set the AppleScript name of the window to “Main Window”. You’ve now set all four names of your main window.
The MainMenu.nib window and the NSWindow Info window showing two different names assigned to a single window
Let’s set up an interface with a few buttons, a few progress indicators, and a slider.
9. In the interface objects palette (which shows you what you can place in your interface), click on the “Views” button — if there are no button labels, control-click somewhere in the toolbar, and select Icon & Text to show the labels.
The interface objects palette, showing the Views section
10. Drag a button, a switch, a radio button set, a system font text, and a blank text field from the interface object palette to your main interface window (if you haven’t already done so, double-click the icon named “Main Window” in the MainMenu.nib window to show your application’s main window, which is where you are going to place your interface objects). Set it up as in the picture below — remember that you simply have to click once on an object and then click and drag the red handles to resize an object.
The completed interface
Setting up the interface to be exactly like the above picture is not necessary. Just make sure you have the same interface objects, so you can follow along when working on your AppleScript project. Also, remember that the blue guide lines that show up help you to make your interface more visually appealing and more consistent with other Mac OS X applications.
All of the interface objects we just placed can be customized. Simply press Command-Shift-I or select Show Info from the Tools menu (make sure the Attributes popup item is selected). Here you can set whether a button is enabled or disabled, small or normal size, the equivalent keyboard command, and the title of the object.
For the purposes of this tutorial, we’re just going to set the name of each object. You can quickly set the name of each interface object by first opening the Info window. Then, one by one, select the interface objects, and set their name — to do so, you’ll change the “Title:” field to the desired name. As you click the different objects, the Info window will change to show you the attributes of the currently selected interface object. The radio button set is different: click once to select the set, and then double-click on the radio button whose name you want to change. Now you can set the name of the radio button — single-click the other radio button to change its name.
11. Make your interface look like the picture below.
The customized interface
We’ve set the title of all the interface objects, but we haven’t set their AppleScript names. This is essential when building an AppleScript Studio application — without the AppleScript name, you have to refer to an object by its index number, which isn’t terribly convenient.
12. To set up the AppleScript name of each object, first select “AppleScript” in the popup menu of the Info window, or simply press Command-6. Now, as you did before, select each interface object, in turn, and change it’s AppleScript name — set the AppleScript name to be the same as the title of the interface object, so you won’t get confused as to how to refer to an object.
Now we want to set it up so your interface objects actually do something. We want your application to do something when you click the “Press me!” button, when you check or uncheck the “Check” checkbox, when you select a different radio button, when you edit the text field “Text Field”, or when you move your mouse over the label “Label”. This is a crucial step when setting up AppleScript Studio applications.
13. Select the “Press me!” button by clicking on it once.
If you haven’t closed the Info window, you should see the AppleScript name of the button, and a list of checkboxes under the heading “Event Handlers”. This is where we select to which events that the interface object responds.
14. In the Event Handlers list, click the disclosure triangle next to the checkbox named “Action”. Then check the checkbox named “clicked”.
This tells the button to respond to being clicked. However, it doesn’t tell it in which script your code resides. You can have numerous different scripts in a single AppleScript Studio project — how does the button know which script to look into in order to do what it’s supposed to do? Simple: you just have to check the correct script in the “Scripts” list at the button of the Info window.
15. Check the checkbox next to your one and only script in the “Scripts” list in the NSButton Info window. The script should be named the name of your project, except with “.script” at the end of the name.
The button is ready to execute code!
16. For the checkbox named “Check”, check the checkbox for the “clicked” action (under the Action section), and select your script.
17. For the text box named “Text Field”, check the checkbox for the “end editing” action (under the Editing section), and select your script.
18. For the text field named “Label”, check the checkboxes for the “mouse entered” and “mouse exited” actions (under the Mouse section), and select your script.
19. For the radio buttons, single-click on the set of radio buttons, double-click to select a single radio button, and then check the checkbox for the “clicked” action (under the Action section). Remember to also select your script. Single-click on the other radio button, and set up its AppleScript name and designated script as well.
20. Now, reselect one of the interface objects you just set up (like the Press me! button). Over in the info window, make sure you’ve got AppleScript selected in the popup menu, and click the Edit Script button down at the bottom of the window. This will open up Project Builder, and will automatically set up the required on-end blocks in your script for each of the actions that we selected.
You should now have 4 on-end blocks: “on clicked theObject”/”end clicked”, “on mouse exited theObject event theEvent”/”end mouse exited”, “on mouse entered theObject event theEvent”/”end mouse entered”, and “on end editing theObject”/”end end editing”. If you don’t, go back to Interface Builder, select each interface object, and make sure that you actually checked the box next to your script in the Script section of the Info window. Remember, make sure that AppleScript is selected in the popup menu.
Now we’re ready to start writing some code! First, let’s make a dialog show up when we press the “Press me!” button.
21. In your script in Project Builder, put the following code in between the “on clicked theObject” and “end clicked” lines:
display dialog "Ouch!"
22. Now, save the script, and build your application. Click the “Press me!” button, and watch your dialog show up! Save any and all files before building (Project Builder should automatically ask you).
But, wait! Click on the checkbox, or on one of the radio buttons. Uh, oh: the dialog shows up when you click those things, too! We don’t want that. Let’s fix that. Go back to your code in Project Builder — we need to make sure that certain code only applies to our specific button.
23. Change the code we just wrote from:
display dialog "Ouch!"
if (name of theObject is "Press me!") then
display dialog "Ouch!"
If you haven’t coded in AppleScript before, the syntax is pretty straightforward, since it’s human-readable. You should be able to easily follow along and customize the code that’s presented in these tutorials.
24. Build and run the project now. Notice how the dialog now only shows up when you press the “Press me!” button. This is the desired behavior.
Now let’s set up some code for the other buttons. We want the checkbox to change the label that we also placed in the interface.
25. Go back to Project Builder, and add this code right after the end if line that we just put inside the “on clicked theObject”/”end clicked” block (also called the “on/end clicked” block):
if (name of theObject is "Check") then
if (state of theObject is 1) then
set contents of text field "Label" of window "Main Window" to "Check is on!"
set contents of text field "Label" of window "Main Window" to "Check is off."
As a quick aside, note how newly typed code looks differently from code that has been typed before you last built the project? That’s because it’s been checked for correct syntax. This always happens before the project is built, just like a compiler compiles C, C++, or Objective-C code before running your app. If you want to make sure your syntax is correct without having to initiate a build of your app, simply press the enter key on your keyboard’s keypad, press Command-K, or select Check Syntax of Current File from the Build menu.
Code that hasn’t had its syntax checked is, by default, orange, and in the Courier font. Syntax-checked code is multi-colored and uses the Geneva font.
Back to the checkbox. Note how we first checked that the name of the object was “Check”. But then inside, we accessed the state of the checkbox, now that we confirmed that it is indeed the checkbox. For checkboxes, a state of “1” means that it is checked, and a state of “0” means that it is not. There are a few other properties that we can access or set. Let’s do this for the radio buttons.
26. Add this code inside the on/end clicked block that we were just editing:
if (name of theObject is "Item 1") then
set enabled of button "Check" of window "Main Window" to false
else if (name of theObject is "Item 2") then
set enabled of button "Check" of window "Main Window" to true
Note how it doesn’t even matter that the radio buttons are part of a matrix — we can still refer to them by name when an action is performed on them.
27. Inside the on/end mouse entered block, add this code:
if (name of theObject is "Label") then
set contents of text field "Text Field" of window "Main Window" to "Hi!"
28. Add the above code inside the on/end mouse exited block as well, except changing the “Hi!” to a “Bye!”
Note again how we refer to interface objects. Interface objects exist in a hierarchy of more general interface objects. When we want to change the string in a text field, we first start with the contents of it (contents), which is contained in the text field itself (of text field “Text Field”), which exists inside the window itself (of window “Main Window”). An interface object doesn’t necessarily just live inside the main window: it could live inside a tab view, which could be in a drawer. So when you write AppleScript code, you need to know the precise hierarchy of where the object exists; as you get more advanced, you’ll find some rather annoying oddities. For example, for a tab view, a button existing inside of it would be referred to like so: button “Press Me!” of view of tab view “Tab View” of window “Main Window” — note the extra “view”. But I’m getting ahead of myself; you’ll learn things like these in due time.
29. The last thing we’re going to do is perform an action after finishing editing the text field. Let’s do something wacky this time.. we’ll complete hide the text field named “Label”. This is just another simple property of an interface object like “state” and “name”. Add this code inside the on/end editing code block:
if (visible of text field "Label" of window "Main Window" is true) then
set visible of text field "Label" of window "Main Window" to false
set visible of text field "Label" of window "Main Window" to true
30. Build and run your program. Test all the stuff that we just did: check and uncheck the checkbox, click on each radio button, move your mouse over the “Label” text field (which probably has a different title, now), and type something in the other text field. Note that to actually “finish editing”, you need to press the enter or return key on your keyboard. If we had another text field, you could also “finish editing” by clicking in that other text field.
That’s it for this time! Hopefully you’ve learned a little more about how to manipulate and refer to different interface objects that you can use in your AppleScript Studio application. But we’re just scratching the surface at this point. There’s a lot more to AppleScript Studio!
So this is the end of the second tutorial. How am I doing? Am I going too fast, too slow — am I thoroughly explaining exactly how to do things in Interface Builder and Project Builder? If you have any questions, or suggestions for topics of future tutorials, I’d certainly welcome them. Please, do let me know what you think of my tutorials, so that I can make better ones in the future.
So, you want to learn about AppleScript Studio? Well, you’ve come to the right place. AppleScript Studio is one of the exciting new technologies that Apple has integrated into its free developer tools in recent years, and it’s one of the easiest ways to create professional-looking programs very fast, even if you have limited programming knowledge.
AppleScript Studio is basically the hybrid of two awesome Apple technologies that merge to create a fantastic project. On the one hand, we have AppleScript. AppleScript is a scripting language that is at the heart of Mac OS X, and allows one to control a vast array of different programs to be able to accomplish different things. The two main benefits of AppleScript are that it has an easy-to-learn syntax, and that it can perform an amazing variety of automated tasks by harnessing the power of the many different features of many different programs. AppleScript has always been limited however, because the AppleScript interface is limited to some very confining objects; mainly dialogs and lists. For such a powerful scripting language, AppleScript always languished because of this one major drawback.
The interface limit of AppleScript applets before AppleScript Studio
Now no more. AppleScript Studio allows AppleScript to harness a vast array of different interface objects; in fact, AppleScript Studio applications can now use any native OS X interface object that regular Mac OS X applications can use! This includes buttons, lists, tabs, popup menus, radio buttons, and more. By integrating AppleScript into Project Builder and Interface Builder, a novice programmer can use simply AppleScript to control the advanced interface of his Mac OS X application while retaining the awesome cross-application capabilities of AppleScript.
Of course, AppleScript Studio may not be the best programming language of choice with which to make your Mac OS X applications. AppleScript Studio is still limited by what commands AppleScript can call in any application. And AppleScript is not nearly as fast as ubiquitous programming languages like C and C++. However, for the novice programmer who already knows the syntax of AppleScript and wants to create more advanced applications with an intuitive interface, AppleScript Studio may be the answer. Additionally, learning AppleScript Studio allows a programmer to step up more easily to creating Mac OS X applications in C or C++, since it uses the same exact programming tools to create programs: Project Builder and Interface Builder.
This tutorial goes through the very basics of creating a simple AppleScript Studio project. It assumes no knowledge of Project Builder or Interface Builder, and it also assumes no knowledge of AppleScript. If you want to create AppleScript Studio applications, knowledge of AppleScript would be very helpful, but by no means necessary, since AppleScript is a fairly easy programming language to learn.
Before we start, make sure you have downloaded the latest Developer Tools from Apple. Simply go to http://connect.apple.com/, sign up for a free Apple Developer Connection account, and download the Mac OS X developer tools. (To do so, log in, click “Download Software”, click the “Mac OS X” subheading, and then download the December 2002 Mac OS X Developer Tools — it should be roughly 300 MB in size. Don’t worry about the Dec 2002 gcc Updater. If you have a dialup connection, you should be able to find the developer tools on one of the CDs that came with your Mac or in your Mac OS X retail box. The latest version of the developer tools is recommended, so make sure it isn’t an earlier version of the developer tools on your CD.)
So let’s get started!
1. Open Project Builder. Select New Project… from the File menu or press Command-Shift-N
The new project Assistant window
2. Choose AppleScript Application from the list. You can also choose AppleScript Document-based Application if you wish, but this is only a worthwhile choice if you’re going to make a program that creates and edits documents (like a word processing program or an HTML source editing program). For now, let’s stick with the vanilla AppleScript Application.
3. Click Next, choose a name and a destination, and then click Finish.
Now you’re ready to start writing your application! First, let’s get you acquainted with Project Builder. On the left, you should have a listing that has a heading of Groups & Files. If not, make sure the Files tab is selected — there are 5 sideways tabs going down the right side of the left pane: Files, Classes, Bookmarks, Targets, and Breakpoints.
The main Project Builder window, with the Files tab selected
Click the disclosure triangles for the 5 main groups in the file listing — they should be (in order from top to bottom) Scripts, Resources, Other Sources, Frameworks, and Products. Make sure that the very top group, which is the project itself, is also revealed.
The files inside the Scripts folder is where you’re going to write your actual code. Note that, by default, you’ll have one script in this folder. You can either opt to place all your code in this one big script, or you can create new script files and have some of the code be in separate scripts. Either will work, although I’d probably suggest separate scripts, so that you can manage your code more easily. For this project, we won’t need any more than one script file.
The files inside the Resources folder are more files that you’ll probably modify to customize your AppleScript Studio application. MainMenu.nib is going to be your application’s interface (more on this, later). InfoPlist.strings is a file that contains a few pieces of information, like the version number, the copyright notice, and the name of the application.
The file named AppleScriptKit.asdictionary in the Resources folder deserves special attention. If you click it, an AppleScript Studio reference file will appear in the right side of the Project Builder window. This is where you’ll be able to look through to find the appropriate references and commands that will refer to objects in your application’s interface. If you don’t know how to refer to something, click on this entry in the list and browse the dictionary for the right syntax. This can be incredibly handy.
The other two folders, Frameworks, and Products, are probably things that you won’t touch. “Frameworks” lists the frameworks that your AppleScript Studio application will use. If you’re going to use other frameworks, you’re going to need to reference them in this folder before you can use them. For the most part, though, you won’t need to do this. “Products” just shows a hierarchy of your completed application when you finally build it.
Now bring your attention to the Project Builder toolbar. For now, you just need to concern yourself with the first three buttons on the left (assuming you’re using the default toolbar). The first is Build, which looks like a hammer. When you’ve partially implemented a feature and want to test it out, you’re going to first need to build your program. This packages it into an application package that will run when double-clicked. The Clean button wipes any existing built executable that you’ve created before, so that you can test out your application from a clean slate. Build and Run is similar to Build, except for the fact that it immediately launches your built application after Project Builder has finished building it. This is very handy for when you actually want to test out something you just wrote.
The default Project Builder toolbar, with the three important buttons circled
Now you’re familiar enough with Project Builder that we can actually start building an application. To actually start making an application, you’ll first need to create an interface with Interface Builder.
4. Open Interface Builder by double-clicking on the MainMenu.nib file that’s directly inside the Resources. Interface Builder should automatically start up and display the MainMenu.nib file.
When Interface Builder starts up, 4 windows will show up. One is a floating palette called Cocoa-Views. One is named MainMenu.nib, that contains 4 tabs — Instances, Classes, Images, and Sounds. The last two windows are named MainMenu.nib – MainMenu, and Window.
The four default windows that show up when you open Interface Builder from Project Builder
The Cocoa-Views palette shows all the different interface elements you can place in your AppleScript Studio application. These are exactly the same elements you can place in a standard Cocoa application written in Objective-C, C, or C++; this is one of the big advantages of AppleScript Studio over regular AppleScript applets — you can use all the interface elements available to normal applications, instead of being relegated to 3-button dialogs and lists, which is all that AppleScript applets can use. Click the icons at the top of the palette to cycle through the different kinds of elements that you can place into your application.
The MainMenu.nib window shows an abstract version of your application’s interface. In the Instances tab, it has icons for the menubar (that appears at the top of the screen when you switch into your application), any windows you have, and File’s Owner and First Responder, which are abstract concepts that you probably won’t need to deal with. The Classes tab is pretty much useless to AppleScript Studio developers, since these are Objective-C Cocoa classes. The other two tabs, Images and Sounds, show you what images and sounds you have available at your disposal to use in your application.
The MainMenu.nib – MainMenu window is a floating version of the menubar that will be used when your application starts up. You can use the Cocoa-Views palette to add items to this list if needed, and you can also double-click any menu item to rename it (triple-click a menu title to edit those).
The last window, with an amazingly original name of Window, is where you’re actually going to start placing your interface elements. This is only a default window — you can create as many windows as you like; you’re certainly not limited to this one.
So let’s create an interface and show how to hook up your interface elements to actual AppleScript code that will power your application! Before we start, let’s make things a little easier by changing the name of that window named Window.
5. In the MainMenu.nib window, select the little icon that looks like a window, and press Command-Shift-I, or select Show Info from the Tools menu.
6. Make sure Attributes is selected in the Info window popup menu at the top, and then change the window title name to “Tutorial App Window”.
The Info window with the corrected window name
7. While in the Info window, choose AppleScript from the popup menu (or press Command-6). In the Name field, type “Tutorial App Window”. The name in the AppleScript info panel is what we use to refer to the window, NOT the title.
8. In the Cocoa-Views palette, click the Views toolbar button (which looks like a button above a text field), to show the buttons and text fields you can use in your application.
9. To start, drag a button and a text field to the Tutorial App Window window. To make sure we’re on the same page, use the button that’s labeled with the title Button, and the blank text field at the top right of the window (not the two attached text fields that are named Field1 and Field2).
10. Drag the text field near the top left corner of the Tutorial App Window window. You should notice that guide lines appear when you’re near the edge of the window, and again when you’re right up against the edge. Line up the top left corner of your text field to correspond with the “near-edge” guide lines.
The “near-edge” guidelines appear when you drag an interface object relatively close to the edge of the window
11. Now, click the text field once. 8 red dots should appear around the text field; you can use these to resize the text field. Click and hold on the right-most dot that is not on the corner of the text field. While still holding down the mouse button, drag to your right: the text field should resize. Keep dragging until you hit the “near-edge” guide line on the right side of the window.
12. Now, position the button underneath your stretched text field. When you position it, note that you’ll see the same “near-edge” guide line appear for the left side of the window, but you’ll also see a guide line when the button nears the bottom edge of the text field. This helps ensure that your application’s interface looks good by keeping a specific distance between interface elements.
13. As before, stretch the button to the right side of the window, lining it up with the “near-edge” guide line.
The button has now been placed in the window and stretched
14. Now, grab the Tutorial App Window window’s resize handle, and drag up towards the button. If you’ve set things up right, you’ll hit the minimum height for the window, and it won’t let you shrink it down vertically any more.
The completed window
You’ve got a preliminary interface set up! Now, we’ll set up the hooks to your future AppleScript code.
15. Click on the button once. Then, press Command-Shift-I, or select Show Info from the Tools menu. This will bring up a floating panel that will allow you to customize the appearance and actions of your button.
For now, we’ll ignore the Attributes section, and instead concentrate on making your button actually DO something.
16. At the top of the Info window (which should be named NSButton Info if you’ve selected the button), select AppleScript from the popup menu. You can also just press Command-6 to jump to the correct info window panel.
Here is where you define to what actions your button will respond. In the Event Handlers section, you can click on the disclosure triangles to reveal all the possibile actions. For now, we’ll just use a simple click.
17. Click the disclosure triangle for Action. A single checkbox should be revealed, named “clicked”. Check it.
18. Now, in the Script section of the Info window, select your one and only script. It should have the same name as your project, except with “.script” appended to the end.
The Info window when focused on the button’s AppleScript properties
19. We have to give our text field a name for AppleScript to refer to it, too. So, click on the text field. The Info window should automatically change to have a title of “NSTextField Info” instead of “NSButton Info”, and the “clicked” action should no longer be selected. In the Name field (like we did before for the window), type “Text Field 1″.
20. Choose Save from the File menu to save our newly completed interface.
21. Click on the button again. The Info window title should change back to “NSButton Info”. Now click the Edit Script button.
Project Builder should be activated, and your script will automatically be selected and shown in the main view. Additionally, Project Builder should automatically insert the surrounding on/end block that will allow you to write the code that will execute when your button is clicked. It should show up like this:
on clicked theObject
(*Add your script here.*)
We’re going to edit this script to just display a simple dialog, and a message in the text field.
22. Replace the following line of code:
(*Add your script here.*)
display dialog “Hello world!”
set contents of text field “Text Field 1″ of window “Tutorial App Window” to “Hello world!”
Note that the first line of this code is identical to how you would display a dialog in a regular AppleScript applet. That’s another benefit of AppleScript Studio: you don’t need to learn any new syntax at all! The second line tells the text field to display “Hello world!”, by referring to its position in the interface hierarchy. We want to change the contents of the text field, which is part of the text field itself, which is part of the surrounding window.
We’re now ready to build your application.
23. Press the Build and Run button in the Project Builder toolbar.
24. Click Save All if the dialog comes up (since we haven’t saved your script yet). NOTE: If Project Builder appears to be doing nothing, switch back to Interface Builder — there may be some residual changes that need to be saved before Project Builder will build your project.
Once your project is built, it should start up, complete with an icon showing in the Dock (albeit a plain application icon). Your main window should show up, complete with your blank text field and button. Now, click the button; your dialog should show up, and after dismissing the dialog, the text field should be filled in with the phrase “Hello world!”. Congratulations, you’ve completed your first AppleScript Studio application!
That’s the end of this preliminary AppleScript Studio tutorial. Although we didn’t create much of an application, you’ve learned a lot about getting around Project Builder and Interface Builder, and a bit about referring to objects with AppleScript. This knowledge should allow you to start creating more advanced projects, with the help of the AppleScript Studio documentation (don’t forget the AppleScriptKit.asdictionary file in the Resources folder!).
This is Simone Manganelli, signing off from OSXFactor.com . Keep your eyes peeled for future AppleScript Studio tutorials.