InDesign undo modes in applescript

I may be missing something here, but it seems to me that in applescript it is not possible to pass a handler to the ‘do script’ command, which is the only way to be able to control how InDesign handles undoing your script, it will only accept a string. Is this correct?

Working on the assumption that it is, I’m trying out using the code below to encapsulate scripts so that they call themselves. I’ve done it because I’m getting tired of making whole scripts into strings and escaping all the quotes within, only to have to un-escape them all it if I want to edit the script and compile it for testing.

[b]
property runNow : false – change this to true to debug
property undoName : “Test undo name”

if runNow then
– do stuff
tell application “Adobe InDesign CS5”
– do stuff
end tell

-- important return statement.
-- stack overflow without
return

end if

---- workings
set scriptAlias to (path to me)
set scriptPath to (scriptAlias as string)

set theScript to load script scriptAlias

if not theScript’s runNow then
theScript’s toggleRun()
end if

if theScript’s runNow then
store script theScript in (path to me) with replacing
tell application “Adobe InDesign CS5”
do script scriptPath undo mode entire script undo name undoName
end tell
end if

on toggleRun()
if runNow then
set my runNow to false
else
set my runNow to true
end if
end toggleRun
---- workings
[/b]

It’s just occurred to me that I could also use a script bundle with the main script calling another script from within it’s bundle, which is possibly a more elegant solution.

Having given it some more thought this morning, the method used above is rubbish.

A much easier way is like this:


-- Undo mechanism start

property undo_name : "MyScript"
property load_script : true

if load_script then
	set my_path to path to me as string
	set my_script to load script (alias my_path)
	set load_script of my_script to false
	store script my_script in my_path with replacing
	tell application "Adobe InDesign CS5"
		do script my_path undo mode entire script undo name undo_name
	end tell
	set load_script of my_script to true
	store script my_script in my_path with replacing
end if

-- Undo mechanism end

-- Actual script body start

tell application "Adobe InDesign CS5"
	set sel to selection
	repeat with i from 1 to (count sel)
		delete item i of sel
	end repeat
end tell