InDesign Auto Slug Info // How to make failsafe?

I’ve cobbled together some code from a number of resources online. Then hacked into it to customise our requirements.

One thing I’m struggling with is making the code failsafe. i.e. Under perfect conditions the code runs exactly as intended. But if a layer is locked, or style already exists, then some funny results can occur.

Do you have any idea how to account for anomalies, to make this script a bit more hardy?

By the way, this Script auto adds a slug at the bottom of an InDesign document, then fills in relevant details; filename / date / user etc, and also fits our corporate logo.

Edited

[format](Some info obfuscated to protect the innocent)[/format]

• Check if a document is open before continuing otherwise you get an error
• Check the current document has been saved (has a file path) otherwise you get an error
• The text doesn’t have to have a style. Just specify the size, font, colour etc to avoid style conflicts
• I ran it with locked layers and it still worked :wink:

Okay, I’ve taken onboard your suggestions and added in dialog boxes for whether there is a document open, and if it has been saved yet.

That’s about as far as I can go with this I think.

If you have any further suggestions let me know!

property Horse_User : ""
if Horse_User is "" then
	tell application id "com.adobe.InDesign"
		activate
		set Horse_Dialog to make dialog
		tell Horse_Dialog
			tell (make dialog column)
				tell (make dialog row)
					make static text with properties {static label:"Insert Full Name:"}
				end tell
				tell (make dialog row)
					set Horse_TextField to make text editbox with properties {edit contents:"Joe Bloggs", min width:250}
				end tell
				tell (make dialog row)
					make static text with properties {static label:"(This information will be retained for future use)"}
				end tell
			end tell
		end tell
		set Horse_Result to show Horse_Dialog
		if Horse_Result is true then
			set Horse_User to edit contents of Horse_TextField as string
			destroy Horse_Dialog
		else
			error number -128
			destroy Horse_Dialog
		end if
	end tell
	Horse_RunScript()
else
	Horse_RunScript()
end if
on Horse_RunScript()
	with timeout of 20 seconds
		set Horse_Date to do shell script "date +%Y-%m-%d\" \"%H:%M:%S"
		tell application id "com.adobe.InDesign"
			if active document exists then
			else
				display dialog "Slug creation requires an active document." buttons {"Continue"} default button "Continue" with icon caution giving up after 45
				error number -128
				destroy dialog
			end if
			tell active document
				set Horse_Name to name
				try
					set Horse_Folder to file path
				on error
					display dialog "Document must be saved to continue" buttons {"Cancel", "Save"} default button "Save" with icon caution
					if the button returned of the result is "Save" then
						tell application id "com.adobe.InDesign"
							save active document
							set Horse_Folder to file path
							
						end tell
					end if
				end try
				set text item delimiters of AppleScript to "."
				set text item delimiters of AppleScript to ":"
				set Horse_Client to text item -4 of (Horse_Folder as string)
				set text item delimiters of AppleScript to ""
				try
					set Horse_Layer to layer "Info"
					set locked of layer "Info" to false
					delete every text frame of layer "Info"
					delete every rectangle of layer "Info"
				on error
					set Horse_Layer to make layer with properties {name:"Info"}
				end try
				try
					set Horse_Para to paragraph style "Info"
				on error
					set Horse_Para to make paragraph style with properties {name:"Info", justification:left align, point size:5, applied font:"Helvetica Neue (TT)	Regular", fill color:"Black"}
				end try
				set Horse_Bleed to document bleed bottom offset of document preferences
				set Horse_Info to Horse_Bleed + 11
				if slug bottom offset of document preferences is less than Horse_Info then
					set properties of document preferences to {document slug uniform size:false}
					set properties of document preferences to {slug bottom offset:Horse_Info}
				end if
				repeat with Horse_Spread from 1 to count spreads
					set active spread of layout window 1 to spread Horse_Spread
					tell active spread of layout window 1
						set Horse_Page to name of page 1
						set Horse_Width to item 4 of bounds of page 1
						set Horse_Width to Horse_Width as integer
						set Horse_Height to item 3 of bounds of page 1
						set Horse_Height to Horse_Height as integer
						set Horse_Dimensions to "(W)" & ((Horse_Width as string) & "mm x (H)" & Horse_Height as string) & "mm"
					end tell
					set Horse_Contents to "Filename: " & Horse_Name & " // Client: " & Horse_Client & " // Page Size: " & Horse_Dimensions & " // Horse: " & Horse_User & " // " & Horse_Date
					set Horse_Bounds1 to Horse_Height + Horse_Bleed
					set Horse_Bounds3 to Horse_Bounds1 + 11
					set Horse_FrameBounds to {Horse_Bounds1, "35", Horse_Bounds3, Horse_Width}
					set Horse_Info to make text frame in page Horse_Page with properties {geometric bounds:Horse_FrameBounds, fill color:swatch 1}
					set properties of text frame preferences of Horse_Info to {inset spacing:{0, 2, 0, 2}}
					set vertical justification of text frame preferences of Horse_Info to center align
					set contents of Horse_Info to Horse_Contents
					set applied paragraph style of parent story of Horse_Info to "Info"
					move Horse_Info to layer "Info"
					set Horse_LogoFilename to "Pathtohorseimage"
					set Horse_Logo to make rectangle in page Horse_Page with properties {geometric bounds:{Horse_Bounds1, "0", Horse_Bounds3, 35}, stroke color:swatch 1}
					place alias (Horse_LogoFilename) on Horse_Logo
					tell Horse_Logo
						fit given proportionally
					end tell
					move Horse_Logo to layer "Info"
				end repeat
				set locked of layer "Info" to true
				display dialog "Slug Complete" buttons {"Okay"} giving up after 10
			end tell
		end tell
	end timeout
end Horse_RunScript

Your first dialog’s purpose is to create a text edit box to collect a name; InDesign’s dialog creation functionality is not needed for that and actually bloats the script by 20 or so lines. Instead, use a standard dialog with “default answer.”

You added a text label for millimeters, without accounting for the units actually in use. This is likely to result in dimension errors.

Both are very good points! :slight_smile:

I don’t know how to account for them though. I’ll do some research…