Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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

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)

Posted on Dec 3, 2012 2:51 AM

Reply
Question marked as Best reply

Posted on Dec 3, 2012 3:56 AM

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.

6 replies

Dec 3, 2012 7:43 AM in response to adamcf

This is what I have right now. as you can see there are 3 variables that carry the needed info. U

ltimatley I'd like to have about 8 variables, all with different info.

I can do a repeat for each one until something is entered, but I cant do a back command for each entry.

That's the purpose for the "run 'script'" line in each section. I'd have to do a repeat inside of a repeat and then overlapping repeats.

It won't let me do overlapping repeats. what's the solution?


run scriptfn

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


runfn

end if


end script


-- Last Name

run scriptln

script ln

set last_name to ""

set lName to display dialog "last name" default answer "" buttons {"back", "cancel", "next"} default button "next"

if button returned of lName = "next" then

set last_name to text returned of lName

else if button returned of lName = "back" then


runfn

end if

if last_name = "" then

run ln

end if


end script


-- street number

run scriptsn

script sn

set s_num to ""

set sNum to display dialog "street number" default answer "" buttons {"back", "cancel", "next"} default button "next"

if button returned of sNum = "next" then

set s_num to text returned of sNum

else if button returned of sNum = "back" then

run ln

end if

if s_num = "" then

run sn

end if

end script



-- ready steady write!

display dialogfirst_name & last_name & s_num

Dec 3, 2012 8:19 AM in response to adamcf

run scriptfn

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


runfn

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.

Dec 3, 2012 9:27 AM in response to adamcf

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.


onrun-- example


setfirstNametogetInfo("first name")


setlastNametogetInfo("last name")


setstreetNumbertogetInfo("street number")


display dialogfirstName & space & lastName & space & streetNumberbuttons {"OK"} default button1

endrun


ongetInfo(whatever)


settheResultto""


repeatwhiletheResult = ""

settheResulttotext returnedof (display dialogwhateverdefault answer""buttons {"cancel", "next"} default button"next")


endrepeat


returntheResult

endgetInfo

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

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.