End Script

I know everyone hears this but I am a newbie. I am trying to build an applescript application for work to shortcut several repetitive tasks.

But before I jump to much into the actual interface I am trying to get applescripts to work correctly. The one I am working on right now is used to create a project folder with inlcluded prenamed subfolders. The name of the project folder would be determend and entered through a dialog box by the user. What I need to have happen is have the script stop if the user tries to use any word that could be viewed as vulgar. I think this would be done by an if then statement but i can’t seem to grasp 'em. Any pointers would be greatly appreciated.

Thanks

Model: PowerBook G4
Browser: Safari 412
Operating System: Mac OS X (10.4)

Of you just want the script to end then use the ‘return’ statement which will exit the current handler (or the script if you’re in the run handler):

set newFolderName to text returned of (display dialog "Enter new folder name:" default answer "new folder")
if newFolderName contains "badword" then return -- this cancels the current handler
tell application "Finder"
   make new folder at desktop with properties {name: newFolderName}
end tell

There are several variations on this theme, though, including looping until a valid folder name is entered:

set gotGoodName to false
repeat until gotGoodName is true
   set newFolderName to text returned of (display dialog "Enter new folder name:" default answer "new folder")
   if newFolderName contains "badword" then
      display dialog "Invalid folder name specified" buttons {"Try again"} default button 1
   else
      set gotGoodName to true
   end if
end repeat
-- by the time you get here you know you have a name that passes your test
tell application "Finder" 
   make new folder at desktop with properties {name: newFolderName}
end tell

This will repeatedly ask the user for a folder name until a valid name is entered.

I’d use something like this:

property invalid_words : "explative explative explative" --update thse to the actual words you want to forbid; spaces optional
property subfolder_names : {"a", "b", "c"}

set project_name to my get_validated_input("Enter a name for this project:", "untitled project")
if project_name = false then return
set desktop_path to (path to desktop) as Unicode text
repeat with i from 1 to (count subfolder_names)
	do shell script "mkdir -p " & quoted form of POSIX path of (desktop_path & project_name & ":" & item i of subfolder_names)
end repeat

on get_validated_input(the_prompt, default_answer)
	set {the_error, the_icon} to {"", 1}
	repeat
		try
			activate
			set the_result to (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)'s text returned
			repeat with i from 1 to (count the_result's words)
				if the_result's word i is in invalid_words then error 1 number 1
			end repeat
			return the_result
		on error e number n
			if n = -128 then return false --user cancelled
			set {the_error, the_icon} to {"Your response was not valid. Please try again.", 2}
		end try
	end repeat
end get_validated_input

Jon

Just something I noticed:

Camelot’s uses “contains”

John8’s uses “is in”

If I got this right, Camelot’s will flag “abadword”, whereas John8’s will only flag “badword” by itself. Correct?

-N

No, they’re interchangable.

set y to {1,2,3}
set x to 2

if x is in y then …

or:

if y contains x then …

or:

if x is contained by y then …

See the AppleScriptLanguageGuide.pdf for different forms fo the ‘contained by’ operator.

gl,

Kel,
Double check that:

John8’s will not flag “abadword”. It will flag “a badword” or “badword”

Camelot’s will flag “abadword”

Not so sure they’re interchangeable…

-N

They are interchangeable. Our scripts do different things. Mine iterates through the words in the response to see if any of them are contained by the forbidden list while Camelot’s looks for a single forbidden word within the response. Both have their shortcomings and a combination of the two would work best (i.e., iterate through the forbidden words to see if any are contained in the response):

property invalid_words : "explative explative explative" --update thse to the actual words you want to forbid; spaces optional
property subfolder_names : {"a", "b", "c"}

set project_name to my get_validated_input("Enter a name for this project:", "untitled project")
if project_name = false then return
set desktop_path to (path to desktop) as Unicode text
repeat with i from 1 to (count subfolder_names)
	do shell script "mkdir -p " & quoted form of POSIX path of (desktop_path & project_name & ":" & item i of subfolder_names)
end repeat

on get_validated_input(the_prompt, default_answer)
	set {the_error, the_icon} to {"", 1}
	repeat
		try
			activate
			set the_result to (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)'s text returned
			repeat with i from 1 to (count the_result's invalid_words)
				if word i of invalid_words is in the_result then error 1 number 1
			end repeat
			return the_result
		on error e number n
			if n = -128 then return false --user cancelled
			set {the_error, the_icon} to {"Your response was not valid. Please try again.", 2}
		end try
	end repeat
end get_validated_input

Jon

The last of these returns “Your response is not valid. Please try again.” no matter what I enter with invalid words set to “BW1 BW2 BW3” but I can’t see why.

I didn’t look that closely John8, thanks for clarification.

-N

NovaScotian, you’re right, the script I posted was rubbish because it was done too hastily. The repeat loop should said “count words of invalid_words” not “count the_result’s invalid_words”. Here’s a revised script:

property invalid_words : "explative explative explative" --update thse to the actual words you want to forbid; spaces optional
property subfolder_names : {"a", "b", "c"}

set project_name to my get_validated_input("Enter a name for this project:", "untitled project")
if project_name = false then return
set desktop_path to (path to desktop) as Unicode text
repeat with i from 1 to (count subfolder_names)
	do shell script "mkdir -p " & quoted form of POSIX path of (desktop_path & project_name & ":" & item i of subfolder_names)
end repeat

on get_validated_input(the_prompt, default_answer)
	set {the_error, the_icon} to {"", 1}
	repeat
		try
			activate
			set the_result to (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)'s text returned
			repeat with i from 1 to (count words of invalid_words)
				if word i of invalid_words is in the_result then error 1 number 1
			end repeat
			return the_result
		on error e number n
			if n = -128 then return false --user cancelled
			set {the_error, the_icon} to {"Your response was not valid. Please try again.", 2}
		end try
	end repeat
end get_validated_input

Jon

Good Stuff - thanks. I’d like to use a variation of this script that replaces “expletive expletive, etc.” with the names of files already in a folder to inform the user that a file with the proposed name is already in the folder (rather than have the Finder tell me that). A few tweaks will get me there.

That’s easy enough. This script will both check for invalid words and for existing folder names:

property invalid_words : "explative explative explative" --update thse to the actual words you want to forbid; spaces optional
property subfolder_names : {"a", "b", "c"}
property target_folder : ""
property existing_folders : {}

set target_folder to (path to desktop) as Unicode text
try
	tell application "Finder" to set existing_folders to name of folders of folder target_folder
on error
	set existing_folders to {}
end try
set project_name to my get_validated_input("Enter a name for this project:", "untitled project")
if project_name = false then return
repeat with i from 1 to (count subfolder_names)
	do shell script "mkdir -p " & quoted form of POSIX path of (target_folder & project_name & ":" & item i of subfolder_names)
end repeat

on get_validated_input(the_prompt, default_answer)
	set {the_error, the_icon} to {"", 1}
	repeat
		try
			activate
			set the_result to (display dialog the_error & the_prompt default answer default_answer buttons {"Cancel", "OK"} default button 2 with icon the_icon)'s text returned
			if the_result is in existing_folders then error "name already exists" number 1
			repeat with i from 1 to (count words of invalid_words)
				if word i of invalid_words is in the_result then error "invalid word found" number 2
			end repeat
			return the_result
		on error e number n
			if n = -128 then
				return false --user cancelled
			else if n = 1 then
				set the_error to "The name "" & the_result & "" already exists. Please try again." & return & return
			else if n = 2 then
				set the_error to "Your response included an invalid word. Please try again." & return & return
			end if
			set the_icon to 2
		end try
	end repeat
end get_validated_input

Jon