Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Even better' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Even better
Authored by: blinkintosser on May 06, '10 10:37:19PM
For what it is worth, here is an AppleScript to change the margins in the frontmost TextEdit document. Save it to ~/Library/Scripts/Applications/TextEdit to have it appear in your scripts menu (if you have it enabled). The code is potentially dangerousóit could devour your homeworkóbut it keeps your original document (temporarily) at $TMPDIR./remarginizer.rtf should you need to recover it.

-------------------------------------------------------------
-- AppleScript: Modify margins in frontmost TextEdit document
-------------------------------------------------------------
property sTempFilePosixPath : "$TMPDIR./remarginizer.rtf"

-- RTF margins are measured internally in twips (1/20th of a point, 1/1440")
property iUnitConversion : 1440 -- adjust to express your margins in an alternate unit: 1440 for inches, 20 for points, 567 for points
property lMargins : {0.5, 0.5, 0.5, 0.5} -- left, right, top, bottom
property lRtfControlWords : {"\\margl", "\\margr", "\\margt", "\\margb"}

main()
on main()
	if name of document 1 of application "TextEdit" does not end with ".RTF" then -- trivial test to see if doc is rtf and has been saved
		display alert "Cannot Resize Margins" message "You must save the document as rich text (RTF) before resizing margins."
	else
		-- get margin values from user
		set AppleScript's text item delimiters to ", "
		copy lMargins as string to sMargins
		set AppleScript's text item delimiters to ","
		copy text returned of (display dialog "Enter new margins as:" & return & "<left>, <right>, <top>, <bottom>" default answer sMargins) to sMargins
		repeat with i from 1 to 4
			set item i of lMargins to ((text item i of sMargins) as number) -- save back to property as new default
		end repeat
		set AppleScript's text item delimiters to ""
		
		-- compile margin list into a string of RTF control words
		copy "" to sRtfMargins
		repeat with i from 1 to 4
			copy sRtfMargins & item i of lRtfControlWords & ((item i of lMargins) * iUnitConversion as integer) to sRtfMargins
		end repeat
		
		tell application "TextEdit"
			copy path of document 1 to sRtfDocPosixPath
			close document 1
		end tell
		
		-- copy RTF file to $TMPDIR, then write modified RTF back to original (rather than move) to preserve inode, attributes, etc.
		copy "/bin/cp " & quoteText(sRtfDocPosixPath) & " " & quoteText(sTempFilePosixPath) to sCmdCopy
		copy escapeText(" && /usr/bin/awk '{while ($0 ~ /\\marg[rtb][0-9]*/) {sub(/\\marg[rtb][0-9]*/, \"\", $0)};{sub(/\\margl[0-9]*/, " & quoteText(sRtfMargins) & ", $0); print}}'") to sCmdAwk
		copy " < " & quoteText(sTempFilePosixPath) & " > " & quoteText(sRtfDocPosixPath) to sCmdRedirect
		copy " && /bin/rm " & quoteText(sTempFilePosixPath) to sCmdDelete
		do shell script (sCmdCopy & sCmdAwk & sCmdRedirect) -- left off "& sCmdDelete" from line above so original doc is preserved in temp file for emergency recovery; add at own risk
		
		tell application "TextEdit" to open sRtfDocPosixPath
	end if
end main

on escapeText(sText)
	set AppleScript's text item delimiters to "\\"
	copy every text item of sText to sTextList
	set AppleScript's text item delimiters to "\\\\"
	copy sTextList as string to sText
	set AppleScript's text item delimiters to ""
	return sText
end escapeText

on quoteText(sText)
	return "\"" & sText & "\""
end quoteText


[ Reply to This | # ]