>
Apple-AppleScript-Script-Editor-Logo

Basic AppleScript Dialog:

display dialog "Your text here" with icon stop buttons {"OK"} default button {"OK"} giving up after 5

'giving up after 5' will automatically close the script after the indicated interval of time has elapsed.

HTML Anchor Code

Here is a sample of how to write code to link one part of your blog page to another:

<a name = "By Email">[optional text]</a> --place this where you want the link to go <a href = "#By Email">By Email</a> --this is the actual link

more HTML

HTML Code to Link to Other Web Pages

Here is a sample of code to link to another page. this is similar to the anchor code, except that the destination code is the url of the destination site:

<a href= "www.webSite.
com"> Website Name</a>

--just replace "www.webSite.
com"
with the actual url destination site and replace 'Website Name' with the prompt that the user will see.

My Links

Apple-IIc-Apple-Screen

AppleScript Note:

It might be interesting to those of you who are AppleScript enthusiasts that the HyperCard (HyperTalk) project was the prototype back in the '80's of what became the system-wide Applescript language (akin to JavaScript) that is in use today.
Apple-Mac-512-Screen
3d-iMac-Large

Trapping for List Dialog Errors

With 'List Dialog' type dialogs, since errors cannot be intercepted in an 'on error' handler, there is no 'normal' way to trap for 'Cancel' which, of course, would result in some sort of undesirable error dialog such as 'User cancelled. Error number -128'. Here is an example of one simple way I have found to trap for this type of error: set x to (choose from list {"Joe","Amy",
"Bill"} with prompt "Choose a record:")
if x is false then
else
set targetItem to (x as text)
show every record whose cell "Name" contains x
end if
When the user clicks on 'Cancel', the variable x is assigned the boolean value false. So all you have to do is set up a conditional to deal with that and to perform the usual statements otherwise.

Learning AppleScript

AppleScript-123-Book

Create Multiple Folders with Terminal

If you are more of a techy kind of person and are comfortable with using Terminal, here is a script you can use to easily create multiple folders:

First, for a single folder, type in: mkdir "Folder 1" --or whatever you want to name your folder, this creates a new directory, which, in effect is a new folder. To place multiple items in the 'Documents' folder: cd/Users/Administrator/Documents mkdir "Folder 1" "Folder 2" "Folder 3" To quickly create multiple folders, create a text file with the desired folder names (as many as you want) and name it something like 'folderList.txt'. Next type this in Terminal: cat folderList.txt |xargs mkdir Or you could create folders with the same prefix by entering: mkdir "Invoices " {"Corporate", "Individual", "Pro-Bono"}

Digg! Digg This!!

Mac LC III (1994), the first Mac that I ever owned, the CD-Rom and Zip drive were added much later.

Mac LC III (1994), the first Mac that I ever owned, the CD-Rom and Zip drive were added much later.

Basic HTML

Here is an example of a very simple HTML document:

<html>
<head>
<title>Basic HTML Document</title>
</head>
<body>
Your text goes here
</body>
</html>

more HTML

JavaScript

cappuccino JavaScript

Somewhat similar to AppleScript, JavaScript is used mainly in web pages to add programming and interface functionality. While AppleScript can be used for this, the standard is JavaScript as it can be used on any of the common computer platforms (Macintosh, Windows, Linux etc.)

Like AppleScript, It is fairly ‘english-like’ in syntax. The syntax is a little more difficult to pick up than AppleScript, but, if you have learned a little bit about AppleScript, it should not be difficult to learn the basics.

Simple Interaction | Confirm Box | Prompt Box | JavaScript to Open URL in New Window | Dealing with Error Messages | Using the Switch Statement | The For Loop and For…In Loop



JavaScript and Simple Interaction with the User

Alert Boxes (dialogs) are similar to AppleScript dialogs in that they communicate the state of something to the user. While AppleScript dialogs can be used in web pages as well as on the desktop, JavaScript dialogs are used almost exclusively for the internet.

In this, the first post on this topic, I will use the various types of JavaScript ‘Boxes’ to illustrate some of the basics of the JavaScript syntax.

Although you can call for an external JavaScript, they are most commonly placed within either the <head> tag or the <body> tag of an HTML page:

<head>
<script type="text/javascript">
function userAlert()
{
alert("Welcome to my web page");
}
</script>
</head>



When you place a function, in this case an alert dialog, within the head tag, it ‘preloads’ the function when the page is displayed so that when it is needed there is no delay. You must begin with the statement ‘<script type=”text/javascript”>’, which tells your computer to expect something with the JavaScript syntax.

Next, the name of the function (case sensitive, ie: userAlert() and useralert() are not the same and could be two totally different functions).

Then the statements, each line ending with ‘;’ and an entire block of statements enclosed by ‘{}’ Note that in a single statement block such as the one above the so-called ‘curly brackets are optional’.

In the body section of the page:

<body>
<input type="button" onclick="userAlert()" value="Display alert" />
</body>

It looks like this:

Click the button above to see the dialog

input type=”button” says to display a button on the page and value=”Display alert” says that the button will bear the name “Display alert”.

Lastly, onclick=”userAlert()” tells JavaScript that when the button is clicked, to execute what is in the preloaded function ‘userAlert’.

YouTube Preview Image

120x20 thumb black

If you have questions or would like to suggest a post on another JavaScript issue, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.


Digg!

JavaScript Confirm Box Dialog

Apple IIThe confirm box, has a syntax that is similar to that of alerts. It is often used when you want ask the user to make a decision on something. It has an “OK” and a “Cancel” button.

If the user clicks “OK”, the value returned is true. If the user clicks “Cancel”, false is returned. These boolean values can be used for further script execution. Once again, they are usually placed either within the <head> tag or the <body> tag as is the case here. It is not required, but is good style as it makes the page flow efficiently and gives it a professional look :

<head>
<script type="text/javascript">
function userConfirm()
{
var response=confirm("Press a button");
if (response==true)
{
document.write("OK button!");
}
else
{
document.write("Cancel button!");
}
}
</script>
</head>

Note here that in ‘var response=confirm(”Press a button”);’ above, we use an equal sign to assign a value to the variable ‘response’. Note in the next line that ‘if (response==true)’ uses double equal signs to indicate equals and also unlike AppleScript the word ‘then’ is omitted.

‘document.write(”OK button!”);’ tells the function to write “You pressed OK!” to the web page (or blog). The text string must be enclosed by quotes and parenthesis.



<body>
<input type="button" onclick="userConfirm()" value="Display confirm dialog" />
</body>

This is very similar to the alert syntax and should be easy to understand.

The button above would show this dialog:

confirmAlert 300x93

Stumbleupon

If you have questions or would like to suggest a post, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

What is this??


JavaScript Prompt Box

AppleIIc afterSchoolAd 300x208These are used when you want the user to enter text into a dialog box. They are like AppleScript dialogs with ‘default answer…’ They display “OK” and “Cancel” buttons and a text entry box. “OK” returns the text that was entered. “Cancel” returns the value null. The null value is somewhat like the empty value in AppleScript:



<head>
<script type="text/javascript">
function screenNamePrompt()
{
var screenName=prompt("Enter your screen name","MacUser");
if (screenName!=null && screenName!="")
{
alert("Your screen name is " + screenName + " !");
}
else
alert("Don't you have a screen name?");
}
</script>
</head>

The Statement ‘var screenName=prompt(”Enter your screen name:”,”MacUser”);’ tells JavaScript that you are requesting a dialog with the text prompt: ‘Enter your screen name:’ and the default entry text ‘MacUser’

There are a few things to note here. First, the line ‘if (screenName!=null && screenName!=”")’:

The symbol ‘!’ means not or not equal to. So, in this case, if the user clicks the cancel button or enters no text at all, the first conditional statement is executed: ‘alert(”Your screen name is ” + screenName + ” !”);’

The next thing concerns what is referred to as concatenation or parsing (putting two or more strings of text together as one).

The previous statement: ‘alert(”Your screen name is ” + screenName + ” !”);’ uses the plus sign to put strings together as one. In AppleScript, we would use the ampersand ‘&’

<body>
<input type="button" onclick="screenNamePrompt()" value="Prompt Screen Name" />
</body>

Click on this button to see prompt dialog:

If you have questions or would like to suggest a post, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

bluepastelMacDesigned 300x95

120x20 su blue

JavaScript to Open URL in New Window

appledecalsSometimes you want to confirm an action before it is carried out. This script displays a confirmation box and, if confirmed, opens the specified URL:

<script type="text/javascript">
function userConfirm()
{
var response=confirm("Do you want to go to the Home page?");
if (response==true)
{
window.open("http://www.yourURL.com/")
}
else
{
alert("User Cancelled!");
}
}
</script>

<a href="http://www.yourURL.com/" onclick="userConfirm()"><img src="http://www.yourURL.com/wp-content/uploads/2009/06/yourImg.jpg" alt="Image Name" title="Image Name" width="115" height="80" class="alignright size-full wp-image-520" /></a>

Click on the graphic below to see how this works:

Image Name

If you have questions or would like to suggest a post, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

Dealing with Error Messages with Try…Catch

a apple.jpeg 300x229Sometimes you want to give the user some feedback based on information that they have entered. If they have entered something that does not fit with the information requested, you want to be able to tell them so that they will understand what course of action to take.

The syntax of the ‘Try…Catch’ pair of JavaScript will look fairly familiar to you if you are familiar with ‘try…on error’ used in AppleScript. It has the same purpose.

So, let’s examine this script and see how it works:

<script type="text/javascript">
function ageMessage()
{
var ageInteger=prompt("Please enter your age:","16");
try
{
if(ageInteger<0)
{
throw "errorCase1";
}
else if(ageInteger<16)
{
throw "errorCase2";
}
else if(ageInteger>16)
{
throw "errorCase3";
}
else if(isNaN(ageInteger))
{
throw "errorCase4";
}
}
catch(error)
{
if(error=="errorCase1")
{
alert("This is someone who has not even been born yet!");
}
if(error=="errorCase2")
{
alert("You must be at least 16 years old to apply for a driver's license in this state! Sorry.");
}
if(error=="errorCase3")
{
alert("Please come in and register with us to apply or revalidate your driver's license.");
}
if(error=="errorCase4")
{
alert("Invalid entry! You must enter an age here!");
}
}
}
</script>
<input type="button" value="Reply Prompt" onclick="ageMessage()" />

Add to Technorati Favorites

The function ‘ageMessage()’ requests an age for a Driver’s License bureau’s records division to determine if the user is of legal age to apply or revalidate a current driver’s license. The default is 16 (the legal age in most US states to acquire a license)

var ageInteger=prompt(”Please enter your age:”,”16″); The prompt that sets the variable ‘ageInteger’ that will be evaluated.

The ‘try’ segment evaluates the variable var ‘ageInteger’

If the variable value meets a particular value, then it is assigned a ‘throw’ or case value that is sent on to the ‘catch’ (error) subroutine, which will respond with some feedback.

Stumbleupon

Try this out to see how this can be put to use in one of your pages:


If you have questions or would like to suggest a post on another JavaScript issue, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

Using the Switch Statement

javascript loading 300x2711

The Switch Statement is a sort of Conditional, it tests for instances of something, usually a variable value.

Here the function displayAlert assigns a value to the variable OSName by way of a prompt box:

<html>
<body>
<script language=javascript>

function displayAlert()
{
var OSName = prompt(”Enter the name of an operating system:”,”");

switch (OSName)
{
case “Macintosh”:
alert (”The best operating system is Macintosh!” );
break;

case “Windows”:
alert (”Windows is almost as good as Macintosh!” );
break;

case “MS DOS”:
alert (”Windows evolved from MS DOS!” );
break;

case “Unix”:
alert (”Unix enhances Mac OS and makes it more compatible with Windows.” );
break;

case “Linux”:
alert (”Linux is a relatively new OS!” );
break;

default:
alert (”Don’t know that operating system!”);
break;
}
alert (”Execution continues here.”);
}
</script>

Here the function displayAlert is called by clicking on an image in the main body of the page:

<a href=”http://www.yourDomain.com/” onclick=”displayAlert()”><img src=”http://www.yourDomain.com/yourImage.jpg”/></a>

</body>
</html>

Or you could create a button, ‘Switch Activate’ in this case, to do the job:

<button type=”button” name=”Switch Activate” onclick=”displayAlert()”></button>

If you have questions or would like to suggest a post on another JavaScript issue, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

The For Loop and For…In Loop

Apple iMac Heaven Screen 300x191

Sometimes you need to execute code repeatedly for a given number of times. The For Loop and For…In Loop structures accomplish this. These are similar to AppleScript’s Repeat and Repeat In structures.

<html>
<body>
<script language=javascript>

var degreesFahrenheit = new Array (212, 32, -459.15);
var degreesCelsius = new Array ();
var repeatCount;

for (repeatCount = 0; repeatCount <= 2; repeatCount++)
{
degreeCelsius [repeatCount] = 5/9 * (degreeFahrenheit [repeatCount] – 32)
}

for (repeatCount = 2; repeatCount >= 0; repeatCount–)
{
document.write (”Value ” + repeatCount + ” was ” + degreeFahrenheit [repeatCount] + ” degrees Fahrenheit”);
document.write (” which is ” + degreeCelsius [repeatCount] + ” degrees Celsius<br>”);
}

</script>

</body>
</html>

This statement sets the parameters for the loop. The parameters are separated by a semicolon:

for (repeatCount = 0; repeatCount <= 2; repeatCount++)

The first two parameters, repeatCount = 0; repeatCount <= 2;, set the loop count to start at 0 and end at 2.

The last parameter, repeatCount++, increments the loop by 1 each time it loops through. As you might have surmised, repeatCount– would increment the loop by 1 each time from from a higher initial value such as 3.

The For…In loop is used mainly with arrays. It makes it possible to loop through arrays without needing to know ahead of time how many elements are contained in the array.

Example:

var textItemArray = new Array(”Books”, “Magazines”,”Newspapers”);
var theTextItem
for (theTextItem in textItemArray)
{
alert(textItemArray [theTextItem])
}

This loops through the elements in textItemArray and displays an alert containing each.

If you have questions or would like to suggest a post on another JavaScript issue, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter or to subscribe, click the By Email link at the top of the page.

Favorite this site:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz
  • MySpace
  • YahooMyWeb
  • HackerNews
  • RSS
  • Twitter
  • E-mail this story to a friend!
  • Internetmedia
  • Webnews.de
  • Yahoo! Bookmarks
Apple-ID-Badge

Apple-Computer-Sticker-Old
Create Multiple Folders with Terminal

If you are more of a techy kind of person and are comfortable with using Terminal, here is a script you can use to easily create multiple folders:

First, for a single folder, type in: mkdir "Folder 1" --or whatever you want to name your folder, this creates a new directory, which, in effect is a new folder. To place multiple items in the 'Documents' folder: cd/Users/Administrator/Documents mkdir "Folder 1" "Folder 2" "Folder 3" To quickly create multiple folders, create a text file with the desired folder names (as many as you want) and name it something like 'folderList.txt'. Next type this in Terminal: cat folderList.txt |xargs mkdir Or you could create folders with the same prefix by entering: mkdir "Invoices " {"Corporate", "Individual", "Pro-Bono"}

Add http://www.scriptsforapple.com to Technorati Favorites

Apple-iMac-Rainbow

Digg! Digg This!!

An AppleScript to Verify a Date

Run this in the Script Editor:

set dateRecord to (current date)
set defaultDate to (date string of dateRecord)
try
set apptDate to text returned of (display dialog "Enter appointment date:" default answer defaultDate buttons {"Set"} default button {"Set"})
set datetext to apptDate as text
date apptDate --if an invalid date is entered, the next dialog is aborted and it triggers the error alert below.
display dialog datetext & " is a valid date." with icon note buttons {"OK"} default button {"OK"}
on error
set alertText to "An error has occurred!"
set messageText to quote & datetext & quote & " is an invalid date."
display alert alertText message messageText as warning buttons {"OK"} default button "OK" giving up after 15
return
end try

Airport-Extreme-Hardware