Help Creating Script

What am I missing…

tell application "Adobe InDesign CS5"
   activate
   if not (exists document 1) then
       try
           with timeout of 9999 seconds
               display dialog "No document open!" buttons "Cancel" default button "Cancel" giving up after 4
           end timeout
       on error
           return
       end try
   end if
   tell active document
       if not (exists layer "Third from bottom") then make new layer with properties {name:"Third from bottom", layer color:red}
       try
           move layer "Third from bottom" to end of last layer
       end try
       if not (exists layer "Second from bottom") then make new layer with properties {name:"Second from bottom", layer color:red}
       try
           move layer "Second from bottom" to end of last layer
       end try
       if not (exists layer "The Bottom") then make new layer with properties {name:"The Bottom", layer color:orange}
       try
           move layer "The Bottom" to end of last layer
       end try
       if not (exists layer "Another Layer") then make new layer with properties {name:"Another Layer", layer color:dark green}
       if not (exists layer "One More") then make new layer with properties {name:"One More", layer color:dark green}
   end tell
end tell

Thanks all!

gary

Model: Mac mini, Intel Core 2 Duo, 2.53 GHz
AppleScript: AppleScript Editor, AppleScript 2.1, Version 2.3
Browser: Safari 534.59.10
Operating System: Mac OS X (10.6)

Did you edit your script? I’m sure I didn’t see any thens.

Hi zac,

Might be a bug. I can’t test it but you might have a conflict with keywords. Try putting these things ‘end of last layer’ in vertical bars “|”. i.e “|end of last layer|”. Just a long shot.

Edited: or something along that line. :slight_smile:

Edited: or maybe just the “|end|”.

gl,
kel

It could be the try stayement also. It ends with ‘end try’. Darn, I can’t test it. Anyways, good luck. Your try block might be conflicting.

I may be way off so in that case then disregard.

gl,
kel

what you can do is place beeps in your code to see where you’re at. Mave this section out of the tell block:

 tell active document
       if not (exists layer "Third from bottom") then make new layer with properties {name:"Third from bottom", layer color:red}
       try
           move layer "Third from bottom" to end of last layer
       end try
       if not (exists layer "Second from bottom") then make new layer with properties {name:"Second from bottom", layer color:red}
       try
           move layer "Second from bottom" to end of last layer
       end try
       if not (exists layer "The Bottom") then make new layer with properties {name:"The Bottom", layer color:orange}
       try
           move layer "The Bottom" to end of last layer
       end try
       if not (exists layer "Another Layer") then make new layer with properties {name:"Another Layer", layer color:dark green}
       if not (exists layer "One More") then make new layer with properties {name:"One More", layer color:dark green}
   end tell

Place a beep before this. But you need to use another tell block for this section.

gl,
kel

See, what you want to do is break it down into sections to isolate where the error is coming from.

Also, you can use display dialog to give yourself feedback.

Edited: sorry, I got carried away on debugging your script. That’s just some of the things I use to debug AppleScript.

Edited: well placed beeps can tell you a lot. One ore more of them.

gl,
kel

Hi. You’re doing some extraneous operations.

tell application "Adobe InDesign CS3"
	activate
	if not (exists document 1) then
		display dialog "No document open!" buttons "Cancel" default button "Cancel" giving up after 4
		error number -128 --this prevents any null document from moving on to the next segment
	end if
	
	tell document 1
		if not (exists layer "The Bottom") then make layer with properties {name:"The Bottom", layer color:orange} at end
		if not (exists layer "Second from bottom") then make layer at end with properties {name:"Second from bottom", layer color:red} --whichever object position is fine
	end tell
end tell

Didn’t “end tell” work. i deleted two sections to simplify.

tell application "Adobe InDesign CS5"
   activate
   if not (exists document 1) then
       try
           with timeout of 9999 seconds
               display dialog "No document open!" buttons "Cancel" default button "Cancel" giving up after 4
           end timeout
       on error
           return
       end try
   end if
   tell active document
       if not (exists layer "Third from bottom") then make new layer with properties {name:"Third from bottom", layer color:red}
       try
           move layer "Third from bottom" to end of last layer
    end try
   end tell
end tell

Where do I place “beep” in your code?

gary

(Sorry…I had a stroke 9 months ago…)

Hi Gary,

What I meant is that the first step might be to eliminate the first part. Something like this:

tell application "Adobe InDesign CS5"
   activate
   if not (exists document 1) then
       try
           with timeout of 9999 seconds
               display dialog "No document open!" buttons "Cancel" default button "Cancel" giving up after 4
           end timeout
       on error
           return
       end try
   end if
end tell
beep 1 -- added this
-- added this tell
tell application "Adobe InDesign CS5"
   tell active document
       if not (exists layer "Third from bottom") then make new layer with properties {name:"Third from bottom", layer color:red}
       try
           move layer "Third from bottom" to end of last layer
    end try
   end tell
end tell

If you hear a beep, then you know that most likely the error is not coming from the first part.

It takes a little trying, but with beeps and display dialog you can find out a lot with a little thought.

gl,
kel

To me that’s the easiest way to debug when you have a fairly big program and you’re not sure where the error is coming from. You go step by step.

Then, you might want to look at the values of your variables if you still can’t figure it out. With the AppleScript in just the Script Editor, you can use display dialog to show you the values of the variables.

The thing is, you need feedback from your script when you’re writing it and later you can get rid of all that feed back.

Edited: and btw, when you get your script to work. you can put it all back together again if you want to. You’ll have learned a lot by then probably.

gl,
kel

I will keep working on it.,

I’m sorry to hear about your health issue. The code sample you posted mostly worked as-is, although it erred with no document open from its attempt to make layers, so I made that and a few other good-practice changes. A timeout to display a dialog that will destroy itself in a few seconds is unnecessary. The move command and multiple try blocks are also not needed”just indicate where the layer should be made.