Skip navigation

Why doesn't this script work?

318 Views 8 Replies Latest reply: Feb 1, 2013 2:25 PM by Badunit RSS
Boboonski Level 1 Level 1 (25 points)
Currently Being Moderated
Feb 1, 2013 1:45 PM

I've changed the context of the script for posting here, but the one I have is mostly the same. 

 

The problem is, it is saying that economy2 is not defined.  I don't really see why it wouldn't be.

 

set begin to display dialog "LOAD FILE?" with title "Testing" buttons {"Load", "Cancel"}

if button returned of begin is "Load" then

    set theFile to (choose file with prompt "Load doc" of type {"txt"})

    open for access theFile

    set fileContents to (read theFile)

    close access theFile

    set AppleScript's text item delimiters to {":"}

    set economy1 to text item 1 of fileContents

end if

if economy1 is -9 then set economy2 to 18 / 18 * 100

if economy1 is -8 then set economy2 to 17 / 18 * 100

if economy1 is -7 then set economy2 to 16 / 18 * 100

if economy1 is -6 then set economy2 to 15 / 18 * 100

if economy1 is -5 then set economy2 to 14 / 18 * 100

if economy1 is -4 then set economy2 to 13 / 18 * 100

if economy1 is -3 then set economy2 to 12 / 18 * 100

if economy1 is -2 then set economy2 to 11 / 18 * 100

if economy1 is -1 then set economy2 to 10 / 18 * 100

if economy1 is 0 then set economy2 to 9 / 18 * 100

if economy1 is 1 then set economy2 to 8 / 18 * 100

if economy1 is 2 then set economy2 to 7 / 18 * 100

if economy1 is 3 then set economy2 to 6 / 18 * 100

if economy1 is 4 then set economy2 to 5 / 18 * 100

if economy1 is 5 then set economy2 to 4 / 18 * 100

if economy1 is 6 then set economy2 to 3 / 18 * 100

if economy1 is 7 then set economy2 to 2 / 18 * 100

if economy1 is 8 then set economy2 to 1 / 18 * 100

if economy1 is 9 then set economy2 to 0.5 / 18 * 100

 

display dialog economy2

 

Thanks in advanced!

Boboonski

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Feb 1, 2013 1:51 PM (in response to Boboonski)

    first thing that pops up (assuming all the stuff in the if button/end runs without error) is that economy1 is not any of those values

     

    You should set economy2 to some value so that if it is not set by all the if's you'll catch that condition.

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Feb 1, 2013 2:11 PM (in response to Boboonski)

    Maybe your code isn't setting it right. Maybe the file is messed up.

     

    It's good programming practice in a case like this to set economy2 to something before you start or to have a default condition to catch the times that economy1 is not set to any of the expected values.

     

    It will cost you nothing to put a

     

    set economy2 to 0

     

    at the top of your code. Then if the display dialog prints out 0 you know what happened.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 1, 2013 2:16 PM (in response to Boboonski)

    You are reading text but comparing it to a number.  The solution is to either compare to text such as "-1" or coerce the text item read to a number, e.g. set economy1 to text item 1 of fileContents as number.  Don't forget to reset the text item delimiters.

  • Frank Caggiano Level 7 Level 7 (22,820 points)
    Currently Being Moderated
    Feb 1, 2013 2:23 PM (in response to Boboonski)

    See by following good programming practices you were able to debug your program.

     

    By setting economy2 to some value and have it print out that value rather then having the program error you can see that economy1 was not being set as you thought it was. Then it was just a matter of locating the problem.

     

  • Badunit Level 6 Level 6 (10,760 points)
    Currently Being Moderated
    Feb 1, 2013 2:25 PM (in response to Boboonski)

    As a debugging check, insert the line  "return economy1" just to see if it is what you expect. If it is not, it might also help you figure out why not.

     

    EDIT: looks like you already found the answer.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.