Benches Uncategorized

4

Posting script code in these forums

apple Verified

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.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi red_menace

    Have a look at this thread, Hiroto and Camelot explain the issue and provide some work arounds, alas my code wont format properly at the other end.

    http://discussions.apple.com/thread.jspa?threadID=1058023&tstart=0

    Budgie

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It looks like I may have just been lucky since I don't use the underscore or brackets that much. So far, it looks like in addition to putting the code into a 'pre' or 'code' tag, the characters "@", "_", and the brackets (probably "-" as well) need to be encoded. Although it would be better if the forum respected regular HTML formatting like it used to, maybe we can come up with a formatting script in the meantime (I am sure someone can whip out a perl one-liner or something).

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello red_menace,

    Indeed a pre-processing code like yours is always an option for us, but from my point of view, the correct solution is for the discussions' admins to disable this meddlesome formatting feature of the new fora software.

    This new feature affects not only new posts but also the existing messages in the fora, that means it has rendered quite a few useful messages totally useless. This is actually serious data loss caused by the thoughtless software. And for what?

    After all, these discussions' boards are not chat rooms where people might want some simple input methods for fancy text formattings, but technical support fora where people frequently post programming codes, computer outputs and so forth that have many reserved symbols and notation.

    I really wish they may fix this promptly.

    Best regards,
    Hiroto

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I agree that the solution is to fix this, and didn't realize the scope of the problem. In looking at some of the characters to find which ones needed 'fixing', this new formatting is truly horrible. Programming notation aside, not being able to use brackets is just silly - someone must have screwed up somewhere. In the mean time, I have added a fixup handler to my formatter - although being able to preview a post wold be nice too.