New to AppleScript and having a basic issue...

Hello all,
As stated in the subject, I am new to AppleScript and I am having an issue related to the following code:

set my_random_number to random number 100
set my_random_number to my_random_number as text
set my_folder_name to "Folder Number "& my_random_number 
tell application "Finder" to make new folder at desktop 
set my_folder to the result as alias 
tell application "Finder" to set name of my_folder to my_folder_name 
display dialog ("Delete Folder " & my_folder_name &"?") buttons {"Yes", "No"}
set dialog_result_record to the result
if button returned of dialog_result_record is "Yes" then tell application "Finder" to delete my_folder
end if

The issue is that the ‘end if’ terminating the script throws an error when I compile or run the code to test. Here is the error:

‘Expected end of line, etc. but found “if”.’

According to what I’ve read, this is the correct way to invoke an ‘end if’ so why is my small code segment blowing up? Any and all help would be appreciated!

My AppleScript version is AppleScript 1.10.7 and I’m using Script Editor Version 2.1.1 (81)

Thanks in advance…

Model: PowerBook 1.33gHz
AppleScript: AppleScript 1.10.7
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Amfetamyne,

you can make and if statement in two ways

  1. in this instance the end if is implied and all code must reside on one line

if condition then result

2 this is better if you need to do multiple thing if the condition is meet


if  condition then
result
end if

after thought

you could condense your code like this

set my_folder_name to "Folder Number " & (random number 100 as text)
tell application "Finder" to make new folder at desktop
set my_folder to the result as alias
tell application "Finder" to set name of my_folder to my_folder_name

if button returned of (display dialog ("Delete Folder " & my_folder_name & "?") buttons {"Yes", "No"}) then tell application "Finder" to delete my_folder

set newFolderName to "Folder Number " & (random number 100)
tell application "Finder" to set newFolder to make new folder at desktop with properties {name:newFolderName}

display dialog ("Delete "" & newFolderName & ""?") buttons {"Delete", "Don't Delete"} default button 2

if button returned of result is "Delete" then tell application "Finder" to delete newFolder

Edit: Just saw your post, mcgrailm; It doesn’t work for me.

Bruce,

you are correct and I like what you did with the first half

here is my correction combined with yours


set newFolderName to "Folder Number " & (random number 100)
tell application "Finder" to set newFolder to make new folder at desktop with properties {name:newFolderName}
if button returned of (display dialog ("Delete Folder " & newFolderName & "?") buttons {"Yes", "No"}) is "Yes" then tell application "Finder" to delete newFolder

now we have done this is three lines :cool:

Hello Posters,
Thanks for the QUICK responses! And the code-condensation is something that I am going to try right now.

So that I can learn from the ground up, though was there an issue with my code construction? Did I do something incorrect in my code? Or should I just chalk it up and move on? :slight_smile: Certainly not being ungrateful, but I want to make sure that I am grasping the correct concepts.

Thanks again!

Model: PowerBook 1.33gHz
AppleScript: AppleScript 1.10.7
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Wait, looks like I may have answered my own question…

Had I had a line-break (started a new line) like so for example:


if button returned of dialog_result_record is "Yes" then
	tell application "Finder" to delete my_folder
end if

I would have been fine? :cool:

Model: PowerBook 1.33gHz
AppleScript: AppleScript 1.10.7
Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Correct.