Applescript Studio - How to...

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Applescript Studio

Applescript Studio is a group of technologies that all work together to help you write your own software.

All are on you Panther or Tiger install CD or DVD. just copy over the developers tools folder.

Once you have loaded the developer tools, you can start building...

Launch, Xcode and select new project from the file menu (the screenshots are Tiger), then scroll down and for this example select "AppleScript Application". Fig 1.

Type in the project name as Fig 2, use whatever name you like ! Then press finish.

Don't worry we haven't really finished, we're just starting !

You should now have a project window, like Fig 3, (this is in Tiger).

I know....whoooaaaa....what's all this !!!

Don't worry it's not as bad as it looks...

...more tomorrow !
 

Attachments

  • Fig1.jpg
    Fig1.jpg
    64.1 KB · Views: 1,003
  • Fig2.jpg
    Fig2.jpg
    58.7 KB · Views: 957
  • Fig3.jpg
    Fig3.jpg
    69.4 KB · Views: 992

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Three "files/folders" in the project window are important for this Test App.

The 'Scripts Folder' see Fig4, this folder will contain the "Applescripts" that we are going to write for this Application !

If you click on this folder and then double click on the Script you will see something like Fig5 this will be, where we enter our Applescript.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The next folder we are interested in, is the 'Resources Folder'. Within here, as you can see as in Fig6 it contains a file called 'MainMenu.nib'. This nib file will contain our graphical elements such as the menus and windows for our little App.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The third 'file' is the 'Application File' itself, in this example it's the one at the end in red in Fig7, "Ric's Test App.app". This file doesn't exist yet, not until we 'build' the App a bit later on.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The above are the basic building blocks that we are going to use to build our 'Applescript Studio App'. I am trying to keep this easy to follow and by doing so I am missing out some of the more technical aspects, but maybe we'll do a more complicated run through later !!!

more tomorrow...the fun bit, the interface builder !
 

Attachments

  • Fig5.jpg
    Fig5.jpg
    27.4 KB · Views: 1,007
  • Fig6.jpg
    Fig6.jpg
    60.9 KB · Views: 1,040
  • Fig7.jpg
    Fig7.jpg
    69.1 KB · Views: 855

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Next: The Interface Builder.

Reopen your project from before, and double click the 'MainMenu.nib', Fig8.

The Interface builder App will open, three 'Windows' should open up, shuffle them around so that you can see what you are doing, and open the inspector window from the 'Tools' menu.
 

Attachments

  • Fig8.jpg
    Fig8.jpg
    70.2 KB · Views: 906
  • Fig9.jpg
    Fig9.jpg
    69.1 KB · Views: 933
  • Fig10.jpg
    Fig10.jpg
    39.4 KB · Views: 960

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Have a look at the web browser that you are currently using, the 'Application itself' is a window...if you see what I mean. Every Application that you use is made up of Windows and Menus. This is what, in the simplest way, we have in front of us, in Interface builder.

We have a 'Main Window' and a 'Main Menu'.

WIMP....Windows ~ Icons ~ Menus ~ Pointer. Aqua is our GUI, Graphical User Interface of choice.

The three windows that have opened up should look like Fig11,12,13.
 

Attachments

  • Fig11.jpg
    Fig11.jpg
    19.9 KB · Views: 817
  • Fig12.jpg
    Fig12.jpg
    18.3 KB · Views: 845
  • Fig13.jpg
    Fig13.jpg
    9.4 KB · Views: 934

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

The 'Window' can be resized to whatever size you want, for this example we are going to make a small floating window.

If the 'Cocoa-xxxxx' palette isn't already open, go to the Tools menu > Palettes >Show Palettes.

This palette contains all the 'bits' that we will need to make our App. Have a click through the icons across the top to see the various palettes, and what each one contains. we are interested in the one with the buttons on, second from the left.

The first 'palettte' that we are interested in is the 'Cocoa-Controls' palette. Fig14.

Drag a button across to the 'Window', Fig15.

Make sure the button is selected, and change the button name in the Inspector Window Fig16, to 'Run Daily'

Now make three more buttons, 'Run Weekly', 'Run Monthly' and 'Run All'. As Fig17.

Notice I have now made the 'Main Window' much smaller, and how I have arranged the buttons.
 

Attachments

  • Fig14.jpg
    Fig14.jpg
    33.4 KB · Views: 888
  • Fig15.jpg
    Fig15.jpg
    25.6 KB · Views: 871
  • Fig16.jpg
    Fig16.jpg
    74.3 KB · Views: 846
  • Fig17.jpg
    Fig17.jpg
    15.1 KB · Views: 870

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

As you dragged the buttons around, did you notice the guides ! They are very good for helping you align objects, so that everything looks nice.

It's starting to look a little more like an Application now.

Now we are going to add some text, and name the 'Main Window'. So that we end up with something like Fig18.
 

Attachments

  • Fig18.jpg
    Fig18.jpg
    30.7 KB · Views: 986

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

To add the text, in this instance we are adding 'uneditable' text.

Now we are using the third from the left 'palette' Cocoa Text, drag across the 'Label Font Text' as Fig19.

Now edit that text box with the text from Fig18 (previous post). You will need to create three text boxes to create it exactly as Fig18. One for the main text, one for the 'This is just..' and one for the web address.

You can colour the text as you would in any word processor, just select the text and change the colour via the 'Colors' palette > Tools Menu. You can also resize, align etc.

Now to change the 'Main Window' name, just click the window and make sure nothing within it is selected, now change it via the Inspector. Fig20.

When we get a bit more confident we will have a look at a few other 'thing' we can do ... whilst the Inpector is still on the 'Main Window' change the 'Has Texture' to ticked ! Fig21.

Now our little Application should look like Fig22.
 

Attachments

  • Fig19.jpg
    Fig19.jpg
    36.7 KB · Views: 971
  • Fig20.jpg
    Fig20.jpg
    72.4 KB · Views: 856
  • Fig21.jpg
    Fig21.jpg
    54.7 KB · Views: 860
  • Fig22.jpg
    Fig22.jpg
    34 KB · Views: 904
Joined
Feb 12, 2005
Messages
267
Reaction score
8
Re: Applescript Studio

Hi

I had a crack at making a currency converter by looking at what you did for the temperature script and came up with this.

Ps. it can't convert numbers with Decimal points in such as £15.80 so maybe you can have a look and see where I've gone wrong :p
 

Attachments

  • UK Pound conversion..app.sit
    17.2 KB · Views: 834

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Very good !
Code:
set text_returned to text_returned as real

Try this ! Your file was 'seeing the text entered' as an 'integer' and getting rid of the data after the decimal point

Lots of different way's of doing it, just need to allow AS to see the 'data' entered as a fraction/decimal.

Hope this helps.

Try pressing the control key and clicking in an open Script editor window and have a look at some of the scripts, they are very good for learning. Fig 3.
 

Attachments

  • original.jpg
    original.jpg
    37.3 KB · Views: 878
  • amended.jpg
    amended.jpg
    37.7 KB · Views: 918
  • Original Applescript.zip
    1.9 KB · Views: 688
  • Amended Applescript.zip
    2 KB · Views: 661
  • Fig3.jpg
    Fig3.jpg
    46.7 KB · Views: 896

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

One for you to work on when we have finished the tutorial, more tomorrow. Wifey calls...
 

Attachments

  • d.jpg
    d.jpg
    28.3 KB · Views: 974

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

That's done in 'interface builder' you would then add your relevant bits of Applescript and voilla your own little App.

It is not a working version just an example to show you...when I finish writing this tutorial you will be able to build it yourself, easily, honest !

Will write more tutorial, once the anklebiter has gone to bed !

Playing choo choos, now !!! Gotta love brio smart track !!!
 

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

I'll finish tutorial tomorrow, just downloading Xcode 2.1 from Apple 700mb download !

Have just received the VTC Training videos on CD for AppleScript Studio, I'll let you know if they are any good once I've got round to watching them !

Have just received Xcode 2 book and a new AppleScript book to...reviews to follow both look very good !
 

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

So we have now 'built' our interface, just the menus and coding to do.

Just to refresh this is where we should be, your interface should look something like fig 23 below:
 

Attachments

  • fig 23.jpg
    fig 23.jpg
    82.1 KB · Views: 774
  • fig 23b.jpg
    fig 23b.jpg
    52.1 KB · Views: 728
  • fig 23c.jpg
    fig 23c.jpg
    62.5 KB · Views: 729

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

First we need to open Xcode and double click on our runDaily.scpt, then add the following code into it and save. (fig 24)


on «event coVScliI» theObject
tell window of theObject
tell application "Finder"
say "hello Ric"
end tell
end tell
end «event coVScliI»

Now do a 'run and build' and test the Run Daily Button, does it say hello Ric, if it does we are in business. This is just showing you that our button works and is now linked into the program...just check you have the sound turned up !

Project in its current state is posted below.

We are now happy that we are connecting the dots as it were...

The button works !

We have only used the 'say hello Ric' so that we can instantly tell that the button is connected up and fully functional !

We will now strip out the 'speech code' and enter the real script...
 

Attachments

  • s Test App.zip
    132.3 KB · Views: 559
  • fig 24.jpg
    fig 24.jpg
    88.7 KB · Views: 766
Joined
Feb 12, 2005
Messages
267
Reaction score
8
Re: Applescript Studio

Hi

I would like to carry on with my converter and having copied the GUI you made (for practice) and have an idea that I would like to use in it.

Seeing as the conversion rates are ever changing, I would like my AS to automatically update itself from a website such as xe.com

What I mean is that AS selects the specific text from the website (the USD conversion) and rewrites the old rate with the new one such every week.

As I've never done this before I've no idea how it can be done but was just wondering if it was possible as I think it's a good way of practicing.
 

Attachments

  • Picture 1.jpg
    Picture 1.jpg
    10.8 KB · Views: 698

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

No problem ! I'll finish up this "Maintenance App" tonight then we'll get started on your Temp Converter App !

Regards

Ric
 

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Now back to the code,

strip out the speach text, and change the daily script to be:

Code:
on «event coVScliI» theObject
	tell window of theObject
		tell application "Terminal"
			do script "sudo periodic daily"
		end tell
	end tell
end «event coVScliI»

then save build and run it...

...now you can see, that when the Run Daily button is clicked, the Application 'Terminal is launched and the code...sudo periodic daily is entered...

at this point it will ask for your administrators password, this is because we have to 'run' perodic as a Super User or Root User...

I have 'purposely' made this a bit messy so that you can see what's happening...

in real life we would not enter the script above...

we would use:

Code:
on «event coVScliI» theObject
	tell window of theObject
		
			do shell script "sudo periodic daily"

		
	end tell
end «event coVScliI»

or we could use

Code:
on «event coVScliI» theObject
	tell window of theObject
		
			do shell script "periodic daily" with administrator privileges

		
	end tell
end «event coVScliI»

Try one after the other, and build and run.

I am going to use the second script as I prefer the way that interacts with the user.

Before running the new script's open the "Activity Monitor" in the Utiliites folder from within the Apps folder, so that you can see that something is happening when you click the button.
 

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

So now we need to enter in the code for the Run Weekly, Run Monthly and for the Run all Script. So back to Xcode and modify the AppleScripts.

They are as follows:

Run Weekly:

Code:
on «event coVScliI» theObject
	tell window of theObject
		
		do shell script "periodic weekly" with administrator privileges
		
		
	end tell
end «event coVScliI»

Run Monthly:

Code:
on «event coVScliI» theObject
	tell window of theObject
		
		do shell script "periodic monthly" with administrator privileges
		
		
	end tell
end «event coVScliI»

Run All:

Code:
on «event coVScliI» theObject
	tell window of theObject
		
		do shell script "periodic daily weekly monthly" with administrator privileges
		
		
	end tell
end «event coVScliI»


nice and easy !!
 

Ric

Joined
May 14, 2004
Messages
4,245
Reaction score
5
Re: Applescript Studio

Now build and run... and test all the buttons, if you watch the Activity Monitor, you will be able to see it working !

Latest build...project below.
 

Attachments

  • s Test App.zip
    133.7 KB · Views: 568

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top