Re: Bates scripting


Is there a way to make the following script work for downstream directories and to go page by page on on pdfs within those directories? It seems happy to insert a comment, but only on separate pages that are inside a directory; beneath that, it doesn’t seem to function.

To be clear, I’m looking for a way to make the following happen:

/maindir <–insert script at this level
/maindir/a/60-pagepdf [[hoping it will number each page of these pdfs]]
/maindir/a/29-pagepdf [["----"]]

Thanks for any advice you have.



tell application id "DNtp"
	set theResult to display dialog "Enter the Bates number (Prefix Optional, Number zero prefixed to number length eg E000100)" default answer ""
	set theSelection to (get selection)
	set bates to getBates(text returned of theResult & "_") of me
	set batesprefix to item 1 of bates
	set baseCount to item 2 of bates as number
	set bateslength to length of item 2 of bates
	repeat with theItem in theSelection
		set comment of theItem to insertBates(makeBates(batesprefix, baseCount, bateslength) of me, comment of theItem) of me
		set baseCount to baseCount + (page count of theItem)
	end repeat
end tell

-- A bates number is a prefix, a zero prefixed number string terminated by a "_"

-- Normal spotlight comments follow the "_" terminator


-- Valid forms:

-- 000100_

-- E1000100_

-- E100_

-- 1E000100_

-- Note prefixes which are number terminated are terminated by "-" prior to the zero prefixed string ie E1-000100

on hasBates(spcomment)
	--spcomment: string - spotlight comment string
	--looks for the presence of the "_" characted in spotlight comment string
	if "_" is in spcomment then return true
	return false
end hasBates

on insertBates(bates, spcomment)
	-- requires
	-- bates : string - bates number in form Prefix|Number|_
	--spcomment : string - spotlight comment string
	-- effect
	-- Replaces existing bates number in string with new bates number or inserts bates number if not present
	set spcomment to getBates(spcomment)
	return bates & item 3 of spcomment
end insertBates

on getBates(tb)
	-- requires
	-- tb : string - spotlight comment string 
	-- returns 
	-- {Bates prefix as string , Bates number as string, Comment as string}
	--breaks up spotlight comment into components. if no bates prefix or bates number then both list items are empty
	if "_" is in tb then
		set spcomment to ""
		--set batesnum to tb
		set k to offset of "_" in tb
		-- return three params, prefix, number and comment
		if k is not length of tb then set spcomment to text (k + 1) thru -1 of tb
		set tb to text 1 thru (k - 1) of tb
		--set batesprefix to bates
		set batesprefix to ""
		set batesnum to ""
		set bcount to 1
		repeat while bcount ≤ (length of tb) and character -bcount of tb is in "0123456789"
			set batesnum to character -bcount of tb & batesnum
			set bcount to bcount + 1
		end repeat
		if length of tb is not equal to length of batesnum then
			set batesprefix to text 1 thru ((length of tb) - (length of batesnum)) of tb
		end if
		return {batesprefix, batesnum, spcomment}
		return {"", "", tb}
	end if
end getBates

on commenttoBates(spcomment, newcomment)
	set spcomment to getBates(spcomment)
	return item 1 of spcomment & item 2 of spcomment & "_" & newcomment
end commenttoBates

on addtoBates(addB, spcomment)
	set spcomment to getBates(spcomment)
	set thePage to item 2 of spcomment as integer
	set thePage to thePage + addB
	set thePage to thePage as string
	set number_length to length of (item 2 of spcomment)
	set thePage to text -(number_length) thru -1 of ("000000" & thePage)
	return item 1 of spcomment & thePage & "_" & item 3 of spcomment
end addtoBates

on makeBates(batesprefix, batesnum, bateslength)
	if length of batesprefix is not 0 and character -1 of batesprefix is in "0123456789" then
		set batesprefix to batesprefix & "-"
	end if
	set batesnum to batesnum as string
	set thePage to text (-bateslength) thru -1 of ("000000" & batesnum)
	return batesprefix & thePage & "_"
end makeBates

I’m not clear what this is ultimately supposed to be doing. I ran it on one file and got this…