What is AppleScript ?

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
AppleScript is a powerful computer language that is built into every Mac, way back from the System 7 days.

Now that Tiger comes with "Automator" It seems to have sparked in interest in the power of Automation again...

Whilst Automator is only available in Tiger, AppleScript is available in all the modern day Mac OS's.

Try and have a think if there is anything that you do day in day out, on your computer...the chances are that it could and should be automated by using AppleScript.

If you have anything that you can think off then post it and we'll see if we can 'Automate' it one way or another for you !

Whilst we waiting for a few ideas, I'll start writing a bit of a tutorial for anyone that's interested in learning a bit of AppleScript.

Don't worry it's probably the easiest/English like programming language there is !

There is a also a Applescript Studio Tutorial being written as well, this is really more for people who have a rudimentary knowledge of AppleScript !

Tutorial starts below : Screengrabs are from Tiger.
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
First off fire up "Script Editor" it's in the AppleScript folder in the Applications folder...
 

Attachments

  • Script Editor001.jpg
    Script Editor001.jpg
    50.4 KB · Views: 493

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Record---Stop---Run---Compile---Bundle Contents

Well 'record'...

if it worked how we would like it to, we would not be here trying to learn AppleScript.

Some Applications are recordable...that means fire up a 'recordable App' and then open Script Editor and press record, now go back to your App and do what you want and Script Editor will record it. Once your done you can go back to Script Editor and save your Script.
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
The Finder is 'partially' recordable, try this below !
 

Attachments

  • Script Editor002.jpg
    Script Editor002.jpg
    30.1 KB · Views: 487

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
As you can see the 'actions' that you took in the Finder were recorded by Script editor.

If only everything was 'recordable'...

...but unfortunately it's not. The best we can hope for is that the nice programmers that have written the software that we want to use, have included an AppleScript Dictionary.

If they have it makes life a bit easier.

The 'Dictionarys' are a good starting point...

Back to Script Editor and file menu open Dictionary...

... this opens a dialogue box tha allows you to select an Application and look at it's AppleScript Dictionary. If it's not in the list, it normally means that it doesn't have an AppleScript Dictionary.

Open the 'Mail Dictionary' it should look like something below...
 

Attachments

  • Script Editor003.jpg
    Script Editor003.jpg
    41 KB · Views: 493
  • Script Editor004.jpg
    Script Editor004.jpg
    46.7 KB · Views: 504

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Have a look through the Mail classes and commands...

..these are all the elements that the programmers have been good enough to program in for us...

All of these elements can be used in an AppleScript.

ie.

type this into Script Editor then press compile and then run...


Code:
tell application "Mail"
activate
end tell

As you can see, relatively plain English.

tell application "Mail"------->Which application should run these commands !

in this instance "Mail" would do anything written within the 'Tell Block'

activate If the program is not open then launch it, if it is open bring it to the front.

end tell Finishes this part of the script, allowing you to move onto something else !

I know not very exciting !

But you have to start somewhere, as least we didn't do

Code:
beep 50
 

Attachments

  • Script Editor006.jpg
    Script Editor006.jpg
    27.4 KB · Views: 490
  • Script Editor007.jpg
    Script Editor007.jpg
    24.2 KB · Views: 466

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
A bit of fun...

read very carefully before proceeding, make sure you understand everything before 'doing'...


First create an AppleScript;

Code:
tell application "System Events"
	shut down
end tell

and save it on your desktop as "shutdown script"...

Now open Mail and go to the preferences and create a new rule as the pic below, but using your own email address !

Then select the shutdown script as in the picture and save the rule.

Make sure there are no other apps open except mail. Now send an email to yourself.

Wait a few minutes and then do a get Mail...

Your Mac should shut down !!!

Now this is the Important bit !! Sometimes when you reboot, and open Mail, it downloads the same email message again, and Shuts down your Mac...if this happens then just reboot and edit the AppleScript on the desktop to:
Code:
tell application "System Events"
	beep 2
end tell

then open up Mail, then delete that email !!!!

All back to normal !!!

Can you start to see, what 'can' be done with AppleScript...
 

Attachments

  • Mail001.jpg
    Mail001.jpg
    49.3 KB · Views: 494
  • Mail002.jpg
    Mail002.jpg
    47.2 KB · Views: 523

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Back to our Mail Dictionary !

Whilst I'm sleeping !!

Have a search in spotlight for a Folder called Mail Scripts, then open it up and have a read through them...

I like the Crazy Message Text.scrpt !

But for now, we are going to be disecting (and learning whats what ) the "Create New Message.scrpt"

The code is below in case anyone hasn't got it...

Code:
(*
Create New Message

Copyright © 2002 Apple Computer, Inc.

You may incorporate this Apple sample code into your program(s) without
restriction.  This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours.  You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes.  If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)

(*
This script walks through the creation of a new message, including setting
sender, signature, subject, to: recipient, body, and attachment. This script
demonstrates some of the new Applescript functionality in Mail and improved
terminology.
*)

-- Repeat this loop until the text entered has been changed from the default example text.
repeat
	set theResult to display dialog "To whom would you like to send this message?" default answer "Example: Jane Doe"
	set theName to text returned of theResult
	if (theName does not start with "Example:") then
		exit repeat
	end if
end repeat

-- Repeat this loop until the text entered has been changed from the default example text.
-- Email address validation could be done at this point.
repeat
	set theResult to display dialog "What is their email address?" default answer "Example: [email protected]"
	set theAddress to text returned of theResult
	if (theAddress does not start with "Example:") then
		exit repeat
	end if
end repeat

-- Prompt for message subject
set theResult to display dialog "What would you like the subject of the message to be?" default answer "I'm sending this via AppleScript!"
set theSubject to text returned of theResult

-- Prompt for whether an attachment is desired. If so, prompt for the location of the file.
set theResult to display dialog "Would you like to attach some files to this message?" buttons {"Yes", "No"} default button 1
set wantsAttachment to button returned of theResult
if wantsAttachment is equal to "Yes" then
	set theAttachment to choose file
end if

-- Prompt for message body
set theResult to display dialog "What would you like to say in the body of the message?" default answer ""
set theBody to text returned of theResult

-- Display a list of all the user's defined signatures. Skip if no signatures are defined.
tell application "Mail" to set everySignature to name of every signature
set theSignature to ""
if (count of everySignature) is greater than 0 then
	set everySignature to {"None"} & everySignature
	set theResult to choose from list everySignature with prompt ¬
		"Select a signature to use with this message:" default items {"None"} without multiple selections allowed
	if theResult is not equal to false then
		tell application "Mail" to set theSignature to signature (item 1 of theResult)
	end if
end if

-- Go through each account and constuct a list of possible addresses
-- to use as a return address for this message.
tell application "Mail"
	set listOfSenders to {}
	set everyAccount to every account
	repeat with eachAccount in everyAccount
		set everyEmailAddress to email addresses of eachAccount
		if (everyEmailAddress is not equal to missing value) then
			repeat with eachEmailAddress in everyEmailAddress
				set listOfSenders to listOfSenders & {(full name of eachAccount & " <" & eachEmailAddress & ">") as string}
			end repeat
		end if
	end repeat
end tell

-- Prompt the user to select which account to send this message from.
set theResult to choose from list listOfSenders with prompt ¬
	"Which account would you like to send this message from?" without multiple selections allowed
if theResult is not equal to false then
	set theSender to item 1 of theResult
	tell application "Mail"
		-- Properties can be specified in a record when creating the message or
		-- afterwards by setting individual property values.
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true
			set sender to theSender
			make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
			tell content
				if (wantsAttachment is equal to "Yes") then
					-- Position must be specified for attachments
					make new attachment with properties {file name:theAttachment} at after the last paragraph
				end if
			end tell
			if (theSignature is not equal to "") then
				set message signature to theSignature
			end if
		end tell
		-- Bring the new compose window to the foreground, in all its glory
		activate
	end tell
end if

This ones a little longer than three lines, but don't worry, we'll take it apart bit by bit so that you understand it !
 

Attachments

  • Finder001.jpg
    Finder001.jpg
    57.7 KB · Views: 478

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