Author Topic: Exit loop  (Read 2643 times)

0 Members and 1 Guest are viewing this topic.

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Exit loop
« on: June 05, 2007, 07:20:40 AM »
I have a script that that Im playing with at the moment to find/reveal in the GUI a layer or layer set by name search. My questions are if repeat loop contains sub-routine can the sub-route exit the loop. Also if i want to repeat until something is true can the sub-routine return this aswell. This is more of an experiment into sub-routines than anthing else. I can get this to mine down to the 5th level of layer set but don't know how to call out of the repeat if the search is found.

001   tell application "Adobe Photoshop CS2"
002        activate
003        set docRef to the current document
004        tell docRef
005             set Q1 to display dialog "What is the name of the layer" & return & "or layer set you want to find?" & return ¬
006                  & "*Note this is NOT case sensitive…" default answer "" with icon note
007             set mySearch to (text returned of Q1) as string
008             set found to false
009             -- repeat until found is true
010             repeat with i from 1 to count of layers
011                  my findLIS(docRef, i, mySearch, found)
012                  -- set found to the result
013             end repeat
014             -- end repeat
015        end tell
016   end tell
017   --
018   on findLIS(docRef, i, mySearch, found)
019        tell application "Adobe Photoshop CS2"
020             tell docRef
021                  try
022                       if exists layer set mySearch of layer i then
023                            set current layer to the first layer of layer set mySearch of layer i
024                            set current layer to layer set mySearch of layer i
025                            set found to true
026                            exit repeat
027                            -- return true as text
028                       else if exists layer mySearch of layer i then
029                            set current layer to layer mySearch of layer i
030                            set found to true
031                            exit repeat
032                            -- return true as text
033                       else if exists layer set of layer i then
034                            repeat with j from 1 to (count of layer sets)
035                                 set SOS1 to name of layer set j of layer i as string
036                                 my findSOS1(docRef, i, mySearch, SOS1, found)
037                            end repeat
038                            return false as text
039                       end if
040                  end try
041             end tell
042        end tell
043   end findLIS

The additional sub-routines are pretty much the same thing just including each sucessive layer set level. Hope I kind of made sence.

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Exit loop
« Reply #1 on: June 05, 2007, 08:19:47 AM »
Hi Larsen,

You are very close...

Any sub routines will stop at the "return" command. Furthermore it will also return (duh!) the value following it:

Quote
001     on Sub_Routine(Param1, Param2)
002          if Logical_Expression is true then
003               return true
004          else
005               return false
006          end if
007     end Sub_Routine

Repeat loops works kinda the same way as they will stop at the "exit" command (no value can be returned though). Now to make use of this you could simple assign the result of your sub routine to a variable and act upon it:
Quote
001     repeat
002          set SubValue to my Sub_Routine(x, y)
003          if SubValue is true then
004               exit repeat
005          end if
006     end repeat

This code can even be simplified by using Applescript default If... Then... Else... behaviour where the expression is expected to be true to trigger the Then statement:
Quote
001     repeat
002          if my Sub_Routine(x, y) then
003               exit repeat
004          end if
005     end repeat

As you can see, you can ONLY exit the Repeat loop from the loop itself. Sub routines are liitle "worlds" by themselves and does not intereact with the calling script. That said, you could also have the sub routine set the value of a property (or global variable) that in tutn can be used to stop your loop:
Quote
001     property ExitRepeat : false

002     repeat while ExitRepeat is false
003          my Sub_Routine(x, y)
004     end repeat

005     on Sub_Routine(Param1, Param2)
006          if Logical_Expression is true then
007               set ExitRepeat to true
008          end if
009     end Sub_Routine

BTW, in your code you had "return false as text" which is fine, but you have to realize that AppleScript does not consider "false" and false to be equals. So if you want to convert your Boolean values to Strings you will later have to compare these values to Strings as well.
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline larsen67

  • Sr. Member
  • ****
  • Posts: 459
  • Karma: 10
  • Gender: Male
Re: Exit loop
« Reply #2 on: June 05, 2007, 09:07:51 AM »
Thanks for the explanation. Im sure theres the bones of a class in there. I will now see if I can re-work this without buggering up my sub-routines. Being close is just not good enough! but better than being miles out I suppose.

Offline Karlsmith

  • Visitor
  • Posts: 2
  • Karma: 0
Re: Exit loop
« Reply #3 on: January 24, 2010, 04:19:33 AM »
Nicely explained by admin :)