throughout this site (and others) you’ll find lots of applescripts which are direct responses to real-world workflows. but, chances are, you’ll need to do some cut-pasting, and additional code writing to create a script which does exactly what you want in your workflow.
this can be a bit intimidating and not a little frustrating as you try to get the bloody thing to work.
so here’s a post (and there may be more to follow) which gets back to basics and addresses a common beginner question :
When I’m cutting and pasting sections of a script, how many ‘end tells’ do I need to include?
new scripters will often be confronted with errors like these when trying to compile or run a script in development :
one of the main problems with this kind of error, as compared with many other types of errors, is that Applescript Editor may not highlight the exact line where the problem needs to be fixed.
the important thing to remember is that the number of ‘end tells’ will be exactly the same as the number of ‘tells’.
when writing code from scratch, it’s best practice to write the ‘tell’ and ‘end tell’ first — then go back and fill in the rest of the code :
tell application id "com.adobe.InDesign" -- rest of code here end tell
you can have tells within tells (or nested tells) — these get closed off in the reverse order that they were opened — but notice, still, the number of ‘end tells’ is the same as the number of ‘tells’ :
tell application id "com.adobe.InDesign" -- code here addresses the application tell active document -- code here addresses the active document tell layout window 1 -- code here addresses the layout window end tell -- code here addresses the active document end tell -- code here addresses the application end tell
other commands
this is also the case with other commands that need an ‘end’ statement. for example, if you have an ‘if’ command within a ‘tell’ command, the ‘if’ needs to end before the ‘tell’ ends :
tell application id "com.adobe.InDesign" if x = y then -- some more code end if end tell
when writing code you can also close off with just ‘end’ and the editor will work out which end goes with which command when the script is compiled. so you can write it out like this :
tell application id "com.adobe.InDesign" tell active document if x = y then repeat 5 times -- some more code end end end end
… and, when compiled, it will become this :
tell application id "com.adobe.InDesign" tell active document if x = y then repeat 5 times -- some more code end repeat end if end tell end tell
but you can see that even here — all the commands get closed down in the reverse order that they were opened. the ‘repeat’ is closed first, then the ‘if’, then the second ‘tell’ and lastly the first ‘tell’.
… and another thing
whenever possible, it’s best to avoid one application ‘telling’ another application :
tell application id "com.adobe.InDesign" set mgFolder to file path of active document tell application "Finder" set mgOLDFolder to (mgJobFolder & "OLD:" as string) as alias end tell end tell
… is better writen as :
tell application id "com.adobe.InDesign" set mgFolder to file path of active document end tell tell application "Finder" set mgOLDFolder to (mgJobFolder & "OLD:" as string) as alias end tell
… where we close the InDesign tell before opening the Finder tell
are there other scripting issues that keep cropping up for you?
Reblogged this on SutoCom Solutions.