Posting script code in these forums
Imported from the Apple Support Forums via the Wayback Machine 🔗
OK, it looks like there are some (new) formatting issues with these forums when people try to post their scripts. I have been using a formatter script that converts the text on the clipboard to HTML and then wraps it in a pre tag, but there still seems to be some issues with certain characters. The following script has worked for me so far (although I don't post that much) - what other scripts or methods work for you?
<pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; width: 720px; color: #000000; background-color: #E0E0E0; overflow: auto">-- script formatter
property TempFile : "SF_TempFile" -- a temporary work file
property TheWidth : "width: 720px; " -- a width attribute (deprecated in HTML 4.01)
try
set TheClipboard to (the clipboard) as string
set MyOpenFile to open for access ("/tmp/" & TempFile & ".txt" as POSIX file) with write permission
set eof of MyOpenFile to 0 -- empty the temp file
write TheClipboard to MyOpenFile
close access MyOpenFile
-- convert to HTML - see man textutil
do shell script "cd /tmp/; textutil -convert html -excludedelements '(html, head, title, body, p, span, font)' -encoding US-ASCII " & TempFile & ".txt"
-- strip the first line (<?xml ?>) and add the <pre>...</pre> wrapper
set TheText to rest of (paragraphs of (read ("/tmp/" & TempFile & ".html" as POSIX file))) as text
set TheText to "<pre title="this text can be pasted into the Script Editor" style="font-family: Monaco, 'Courier New', Courier, monospace; font-size: 10px; padding: 5px; " & TheWidth & "color: #000000; background-color: #E0E0E0; overflow: auto">" & TheText & "</pre>"
set the clipboard to TheText
display dialog ((count TheText) as text) & " characters placed on the clipboard" with title "Script Formatted" buttons {"OK"} default button "OK" giving up after 4
on error ErrorMessage number ErrorNumber
display alert "Error " & (ErrorNumber as string) message ErrorMessage as warning buttons {"OK"} default button "OK"
end try</pre>
4 replies
Sign in or join Beachball Help to reply.
- 0
- 0
- 0
- 0