Skip navigation

Applescript "go to" command or pull variables from script?

1656 Views 6 Replies Latest reply: Dec 3, 2012 10:23 AM by Frank Caggiano RSS
adamcf Calculating status...
Currently Being Moderated
Dec 3, 2012 2:51 AM

Is there a "go to line" command in Applescript?  I want to be able to create a dialog box to jump back to the previous box if a mistake was made.  I don't want to do a repeat because that will cause multipule chained repeats and it won't work.  I tried creating seperate scripts and that works fine for jumping around within the program.  But the variables get lost when a new script is ran.  is there a way to bring variables from other scripts into one script?

MacBook Pro, OS X Mountain Lion (10.8.2)
  • thomas_r. Level 7 Level 7 (26,985 points)

    There is no "goto" command in AppleScript, or any other decent programming language these days. Gotos are evil! You need to use loops that will repeat until the proper conditions are met.

  • red_menace Level 6 Level 6 (14,275 points)

    Depending on how you are running the other scripts (e.g. run script from Standard Additions), parameters can be passed and results returned.  It sounds lke your script/application needs to be reorganized, though.

  • thomas_r. Level 7 Level 7 (26,985 points)

    run script fn

    script fn

              set first_name to ""

     

              set fName to display dialog "first name" default answer "" buttons {"cancel", "next"} default button "next"

              set first_name to text returned of fName

              if first_name = "" then

      run fn

              end if

     

    end script

     

    This is the wrong approach here. You need to use a loop. Try this instead:

     

    set first_name to ""

    repeat while first_name = ""

              set fName to display dialog "first name" default answer "" buttons {"cancel", "next"} default button "next"

              set first_name to text returned of fName

    end repeat

     

    Of course, you will also need some code to handle the case where the user clicks the cancel button.

  • red_menace Level 6 Level 6 (14,275 points)

    Looks like you need to read up on handlers (subroutines).  Since you are doing more or less the same thing to get your values, you can use a handler to do the asking/repeating part and call it for each variable.  In addition to shortening the script, you can just reuse the handler as many times as needed to get your information.

     

    on run -- example

      set firstName to getInfo("first name")

      set lastName to getInfo("last name")

      set streetNumber to getInfo("street number")

      display dialog firstName & space & lastName & space & streetNumber buttons {"OK"} default button 1

    end run

     

    on getInfo(whatever)

      set theResult to ""

      repeat while theResult = ""

        set theResult to text returned of (display dialog whatever default answer "" buttons {"cancel", "next"} default button "next")

      end repeat

      return theResult

    end getInfo


  • Frank Caggiano Level 7 Level 7 (22,830 points)

    Gotos are evil!

    There are no evil programming statements only evil programmers

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.