Conditional Logic - Code Errors ("Expected of Line, etc.")

I am having compiling errors show up for code I am learning from the book “AppleScript Programming for the absolute beginner”, previous scripts work fine in this book. This is for the “Typing Test Game”

The error focuses on the word “else” in the code below. This is a Conditional Logic code issue I am having. I even tried to see if it would format correctly in the native code editor, didn’t work and had same issue.

Here is the code:

– Process the player’s input
if FirstSentence = “Perhaps today is a good to die.” then
set PlayerScore to PlayerScore + 1
display dialog (“Correct! Click on OK for your next typing challenge.” buttons {“OK”})
else
display dialog (“Incorrect. Click on OK for your next typing challenge.” buttons {“OK”})
end if

Hi, Jonathan, I think the parens in your script were confusing the compiler.

Also, when you post a script sample precede it with three ticks"`". That allows your script to be displayed as an appleScript and doesn’t substitute quotes with typographers quotes. Plus you get that nifty button to open the script in Script Debugger.

Ed

set firstSentence to ""
set playerScore to 0

if firstSentence = "Perhaps today is a good to die." then
   set playerScore to playerScore + 1
   display dialog "Correct! Click on OK for your next typing challenge." buttons {"OK"}
else
   display dialog "Incorrect. Click on OK for your next typing challenge." buttons {"OK"}
end if

Thanks for replying quick. I did try not using parens and that didn’t help either. However I did not see "set FirstSentence to “” " in the code in the book, didn’t even show it. I will see if your changes will help.

Also, thanks for telling me about the three ticks preceding the code for this forum. I won’t forget that. I will reply if your additions/tips help. If not, I will edit this post with an “Edit:” to share my result.

Edit: No Parens and still getting the error “Expected end of line, etc. but found “else”. - 2,741”. Also, I am using the current MacOS version.

Here is the updated code. I still get the same error I mentioned earlier. Not even “if else” is working for this. I am literally stumped at how to fix this. If anyone knows how to fix this please let me know.

    set PlayerScore to 0
    set firstSentence to ""

    -- Display the first sentence
    set firstSentence to text returned of (display dialog ¬
    	"Perhaps today is a good day to die." default answer "" buttons {"OK"} ¬
    	giving up after 20 ¬
    	with title "Type the following sentence and click on OK")

    -- Process the player's input
    if firstSentence = "Perhaps today is a good day to die." then ¬
    	set PlayerScore to PlayerScore + 1
    	(display dialog "Correct! Click on OK for your next typing challenge." buttons {"OK"})
    if else
    	(display dialog "Incorrect. Click on OK for your next tying challenge." buttons {"OK"})
    end

This line is missing a ¬ at end.

is wrong. it should be else only.

set PlayerScore to 0
set firstSentence to ""

-- Display the first sentence
set firstSentence to text returned of (display dialog "Perhaps today is a good day to die." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if firstSentence = "Perhaps today is a good day to die." then
	set PlayerScore to PlayerScore + 1
	display dialog "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog "Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

Thank you for the corrections to the code! No formatting issues and no errors any where. That solved the issues.

This is bugging the crap out of me. The last solution worked, now I keep getting errors in other places and the error message is annoyingly cryptic. This time I get “expected end of line, but found “”” This error shows up in line 4 of firstSentence “Process the player’s input” below.

Here is the full code so far:

    use AppleScript version "2.4" -- Yosemite (10.10) or later
    use scripting additions

    --
    -- Script Name: AppleScript Typing Test.scpt
    -- Version: 1.0
    -- Author: Jerry Lee Ford, Jr.
    -- Date: August 2007
    -- 
    -- Description: This AppleScript tests the player's typing skills by 
    -- presented a series of five sentences and challenging the player to
    -- type each sentence without making any typing mistakes
    -- in the time allotted.

    set PlayerScore to 0
    set firstSentence to ""
    set secondSentence to ""
    set thirdSentence to ""
    set fourthSentence to ""
    set fifthSentence to ""

    -- Display the first sentence
    set firstSentence to text returned of (display dialog ¬
    	"Perhaps today is a good day to die." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
    	giving up after 20 with title "Type the following sentence and click on OK")

    -- Process the player's input
    if firstSentence = "Perhaps today is a good day to die." then
    	set PlayerScore to PlayerScore + 1¬
    	display dialog ¬
    		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    else
    	display dialog ¬
    		"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    end if

    -- Display the second sentence
    set secondSentence to text returned of (display dialog ¬
    	"The force is strong with you, but you are not a Jedi yet." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
    	giving up after 20 with title "Type the following sentence and click on OK")

    -- Process the player's input
    if firstSentence = "The force is strong with you, but you are not a Jedi yet." then
    	set PlayerScore to PlayerScore + 1¬
    	display dialog ¬
    		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    else
    	display dialog ¬
    		"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    end if

    -- Display the third sentence
    set thirdSentence to text returned of (display dialog "If you lost and did not cheat, then you did not try hard enough." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
    	giving up after 20 with title "Type the following sentence and click on OK")

    -- Process the player's input
    if thirdSentence = "If you lost and did not cheat, then you did not try hard enough." then
    	set PlayerScore to PlayerScore + 1¬
    	display dialog ¬
    		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    else
    	display dialog ¬
    		"Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    end if

    -- Display the fourth sentence
    set fourthSentence to text returned of (display dialog "Ask not what your country can do for you; ask what you can " & "do for your country." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
    	giving up after 20 with title "Type the following sentence and click on Ok")

    -- Process the player's input
    if fourthSentence = "Ask not what your country can do for you; ask what you can" & "do for your country" then
    	set PlayerScore to PlayerScore + 1¬
    	display dialog ¬
    		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    else
    	display dialog ¬
    		"Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    end if

    -- Display the fifth sentence
    set fifthSentence to text returned of (display dialog "The time may one day come when man's courage fails him," & "but that day is not today." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2¬
    	giving up after 20 with title "Type the following sentence and click on OK")

    -- Process the player's input
    if fifthSentence = "The time may one day come when man's courage fails him, " & "but that day is not today." then¬
    	set PlayerScore to PlayerScore + 1¬
    		display dialog ¬
    			"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    	else
    		display dialog ¬
    			"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
    end if
    	display dialog ¬

The problem is the “¬” character at the end of the first quoted line here.

“¬” is a soft break paragraph, that you can use to make your script more readable, but if the AppleScript compiler sees a line end with “¬” it ignores that and the new line/paragraph character that follows so it’s interpreting those lines of your script as if it were:

set PlayerScore to PlayerScore +  1 display dialog 

And that won’t compile.

Here’s new version. They only thing I changed was removing the “¬” character when it should have been the end of a line. (In one case if followed “then” in an if/then statement.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--
-- Script Name: AppleScript Typing Test.scpt
-- Version: 1.0
-- Author: Jerry Lee Ford, Jr.
-- Date: August 2007
-- 
-- Description: This AppleScript tests the player's typing skills by 
-- presented a series of five sentences and challenging the player to
-- type each sentence without making any typing mistakes
-- in the time allotted.

set PlayerScore to 0
set firstSentence to ""
set secondSentence to ""
set thirdSentence to ""
set fourthSentence to ""
set fifthSentence to ""

-- Display the first sentence
set firstSentence to text returned of (display dialog ¬
   "Perhaps today is a good day to die." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
   giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if firstSentence = "Perhaps today is a good day to die." then
   set PlayerScore to PlayerScore + 1
   display dialog "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
   display dialog ¬
      "Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the second sentence
set secondSentence to text returned of (display dialog ¬
   "The force is strong with you, but you are not a Jedi yet." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
   giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if firstSentence = "The force is strong with you, but you are not a Jedi yet." then
   set PlayerScore to PlayerScore + 1
   display dialog ¬
      "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
   display dialog ¬
      "Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the third sentence
set thirdSentence to text returned of (display dialog "If you lost and did not cheat, then you did not try hard enough." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
   giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if thirdSentence = "If you lost and did not cheat, then you did not try hard enough." then
   set PlayerScore to PlayerScore + 1
   display dialog ¬
      "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
   display dialog ¬
      "Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the fourth sentence
set fourthSentence to text returned of (display dialog "Ask not what your country can do for you; ask what you can " & "do for your country." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
   giving up after 20 with title "Type the following sentence and click on Ok")

-- Process the player's input
if fourthSentence = "Ask not what your country can do for you; ask what you can" & "do for your country" then
   set PlayerScore to PlayerScore + 1
   display dialog ¬
      "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
   display dialog ¬
      "Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the fifth sentence
set fifthSentence to text returned of (display dialog "The time may one day come when man's courage fails him," & "but that day is not today." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
   giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if fifthSentence = "The time may one day come when man's courage fails him, " & "but that day is not today." then
   set PlayerScore to PlayerScore + 1
   display dialog ¬
      "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
   display dialog ¬
      "Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

Here is the full code for the problem, that way it is a permanent reference for any one else working on conditional logic. Thanks @estockly for your multiple times helping with this code problem. I was able to complete the rest of the code with no issues/no temporary successes.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--
-- Script Name: AppleScript Typing Test Game.scpt
-- Version: 1.0
-- Author: Jerry Lee Ford, Jr.
-- Date: August 2007
-- 
-- Description: This AppleScript tests the player's typing skills by 
-- presented a series of five sentences and challenging the player to
-- type each sentence without making any typing mistakes
-- in the time allotted.

set PlayerScore to 0
set firstSentence to ""
set secondSentence to ""
set thirdSentence to ""
set fourthSentence to ""
set fifthSentence to ""

-- Display the first sentence
set firstSentence to text returned of (display dialog ¬
	"Perhaps today is a good day to die." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
	giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if firstSentence = "Perhaps today is a good day to die." then
	set PlayerScore to PlayerScore + 1
	display dialog "Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog ¬
		"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the second sentence
set secondSentence to text returned of (display dialog ¬
	"The force is strong with you, but you are not a Jedi yet." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
	giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if secondSentence = "The force is strong with you, but you are not a Jedi yet." then
	set PlayerScore to PlayerScore + 1
	display dialog ¬
		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog ¬
		"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the third sentence
set thirdSentence to text returned of (display dialog "If you lost and did not cheat, then you did not try hard enough." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
	giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if thirdSentence = "If you lost and did not cheat, then you did not try hard enough." then
	set PlayerScore to PlayerScore + 1
	display dialog ¬
		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog ¬
		"Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the fourth sentence
set fourthSentence to text returned of (display dialog "Ask not what your country can do for you; ask what you can " & "do for your country." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
	giving up after 20 with title "Type the following sentence and click on Ok")

-- Process the player's input
if fourthSentence = "Ask not what your country can do for you; ask what you can" & "do for your country" then
	set PlayerScore to PlayerScore + 1
	display dialog ¬
		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog ¬
		"Incorrect. Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Display the fifth sentence
set fifthSentence to text returned of (display dialog "The time may one day come when man's courage fails him," & "but that day is not today." default answer "" buttons {"Cancel", "OK"} cancel button 1 default button 2 ¬
	giving up after 20 with title "Type the following sentence and click on OK")

-- Process the player's input
if fifthSentence = "The time may one day come when man's courage fails him, " & "but that day is not today." then
	set PlayerScore to PlayerScore + 1
	display dialog ¬
		"Correct! Click on OK for your next typing challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
else
	display dialog ¬
		"Incorrect. Click on OK for your next tying challenge." buttons {"Cancel", "OK"} cancel button 1 default button 2
end if

-- Make a little noise to let the player know the test is over
beep

-- Check the player's score and display the result
if PlayerScore ≥ 3 then -- The player has passed
	display dialog ¬
		"Congratulations. You have passed the typing test!" & "You correctly typed" & PlayerScore & "sentences." buttons {"OK"}
else -- The player has failed
	display dialog ¬
		"Sorry. You have not passed the typing test." & "You mistyped " & 5 - PlayerScore & "sentences." & "Restart the game to try again." buttons {"OK"} with title "Test Results"
end if