Wednesday, August 12, 2009

Display Dialog with Password


This script presents a text entry dialog where the text entered by the user is encrypted as bullets so that nosy people, for instance, cannot see what is being entered on your screen.

Seriously, though, the main purpose of this type of dialog is as a security measure to restrict access to critical files.


set thePass to "pass90805"
--Here, depending upon the application, you could have 'thePass' set to the value stored in a global field, In FileMaker it would be something like 'set thePass to data of cell "passwordStorage"'

considering case
repeat with x from 1 to 3
display dialog ("Enter the password for access to this file:") default answer "" with icon stop with hidden answer
if the text returned of the result is thePass then
exit repeat
end if
if x is 3 then return "You have attempted to access this file too many times. Your access is denied. Please try again."
end repeat
end considering


The considering case block tells AppleScript to require that the text entered match the particular case of the pre-determined password (upper or lower).

By the way, the part that gives the bullets instead of the actual text entered is
'with hidden answer'

The repeat block allows the user 3 attempts at entering the password for access and after that, terminates the script.

Add to Technorati Favorites



Contact me if you have any questions or comments at: hyperscripter@gmail.com or http://twitter.com/hyperscripter

Stumble Upon Toolbar

'Display Alert' Dialogs


These are a type of dialog, introduced in OS 10.4 (Tiger), which are similar to regular dialogs (in use as well as syntax), the noteable difference being that their purpose is to expand upon the standard 'display dialog'. They are used when it is important to impart further information on the state of the Finder or another application, where, for instance, some data loss could occur as a result of an incorrect action being taken.




try
display dialog ("Enter a number") default answer ("") buttons {"OK"} default button "OK"
set userEntry to text returned of result
return userEntry as number
on error
set alertString to "The text entered is not a number"
set messageString to ("" & userEntry & "is not a number. ") & "Run the script again and use only number keys."
display alert alertString message messageString buttons {"OK"} default button "OK" giving up after 20
end try



This is fairly straightforward, below the resulting dialog when the user happens to enter 'abc' (a non-numerical value):

displayalertdlog


Add to Technorati Favorites



Contact me if you have any questions or comments at: hyperscripter@gmail.com or http://twitter.com/hyperscripter

If you like this post, check out this one: Invoking actions with dialogs

Stumble Upon Toolbar

Wednesday, June 10, 2009

Creating a New Document with Microsoft Word 2004

g5gutsFor those of you who prefer to use Word for your text documents, this is a fairly straightforward script to create a new document with a default font and text and save it to your hard drive:

tell application "Microsoft Word"
activate
try
set newDoc to make new document
set name of font object of text object of newDoc to "Arial"
--Here you could have a 'text returned of' dialog to set the text font for the new document to a font of your choice. Of course, the font would have to exist on the target hard drive.

set documentName to text returned of (display dialog "Enter name for new document:" default answer "Untitled Document" with icon note buttons {"Cancel","Save"} default button {"Save"})
save as newDoc file name documentName
--By default, the document is created in the currently active folder.

set initialText to text returned of (display dialog "Enter initial text for new document:" default answer "" with icon note buttons {"Cancel","Insert"} default button {"Insert"})
if initialText<>"" then
set documentRange to create range active document start 0 end 0
insert text initialText at documentRange
end if
--If initialText has no value, then a text document is opened with no initial text.
on error
if documentName = "" then
display dialog "Document creation aborted!" with icon stop buttons {"OK"} default button {"OK"}
else
end if
end try
end tell

The else part of the conditional above instructs AppleScript to ignore the fact that no initial text was requested for the new document and creates the document anyway. The value of 0 for start and end tells Word that it is a document that does not yet have any text or if there is existing text, to place it before the existing text .



If you have any questions or comments, contact me at: hyperscripter@gmail.com or http://twitter.com/hyperscripter.

Add to Technorati Favorites

Stumble Upon Toolbar

Tuesday, June 2, 2009

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

imacrainbow

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 clause to deal with that (notice it does nothing at all) and to perform the usual statements otherwise.




As always, if you have any questions or comments, 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
If you like this site, check out this one: http://www.squidoo.com/applescripter

Stumble Upon Toolbar