Maybe Discourse can’t handle html in a post?
Discourse forum software does not, in general, support HTML by users in a post. There are a few exceptions, like HTML tables (requires Admin to enable). Markdown is the primary formatting system for user posts:
The Discourse software does not allow non http/https URL schemes in URLs. When something like applescript://...
appears within a Markdown URL or an HTML href, Discourse strips it out.
There appears to be movement within the Discourse developers to relax this restriction in some future version of Discourse.
OK, what I would like to achieve is being able to select text in Script Debugger and run a script that grabs the selection (or the entire script) from the front window, and formats it for discourse, with all the color and style attributes, and drops it into an appleScript:// type link that displays the formatted script and when a user clicks on it, it will open the script in their script editor.
Is this doable with the current version of discourse, using HTML or Markdown?
We all share the same desire. For the time being, this is beyond our abilities for technical reasons having to do with the Discourse software. The Discourse folks are talking about fixing the issue where non-http/https URL schemes are filtered.
Once that issue is resolved, when we can generate applescript://
URLs. The final step will be to get the code which pretty-prints AppleScript for us to also generate the applescript://
URL string as well. I’m pretty confident I can make that JS/Ruby code work when the time comes.
It is all doable, except for the “AppleScript://” link. But you can use two separate scripts or macros to achieve the same:
- One script/macro to copy selection from SD and paste into forum post using markdown which will auto format for AppleScript (or any other supported language).
- Another script/macro to copy selected script (or script that is moused-over) from forum and paste into SD.
For #1, see Keyboard Maestro Macro to Paste Script in Discourse Forum . You will just need to insert steps at the beginning to copy the SD selection.
For reference, here’s the forum markdown to format a script as AppleScript:
```applescript
-- your code here
It is best to convert any TABs used for formatting to spaces. Otherwise the forum software will use 8 spaces/TAB.
And please note that I have made AppleScript the default language for code blocks so even if you omit the applescript
specification, it will still work.
One script/macro to copy selection from SD and paste into forum post using markdown which will auto format for AppleScript (or any other supported language).
Right, and I have those that work in other contexts, but I’m not sure about discourse and markdown. The version I use requires style sheets.
Another script/macro to copy selected script (or script that is moused-over) from forum and paste into SD.
It really should be as simple to use for anyone as macScripter. You can’t expect users to install some proprietary software to make simple things work.
I thought I had clearly explained that. No CSS is required. Only Markdown, as shown in this post:
You speak of Keyboard Maestro as if it were some otherwise useless piece of software. It is quite the contrary. It is one of the most useful software apps/utilities I have ever owned. I use it countless times a day. If you are not familiar with it, I suggest you try the 30-day free trial – full version with no limitations.
I was simply offering a KM solution for those that want to use it. AFAIK, MacScripters.net is the only forum that offers the “applescript://” protocol, but maybe there are others.
Discourse forum software is open source, and supports custom plugins.
If you feel strongly about making it very easy for users to copy from the forum to SD, then you can do any of the following:
- Write an AppleScript that will do it (to be run from FastScripts or the Apple Script menu). It should be easy enough, given the scripts/macros already posted here.
- Write a Discourse plugin that will do it.
I was throwing my script snippets into TextWrangler, adding four spaces at the beginning and then doing a search & replace on \n with four spaces and \n.
Now that I’ve seen the four backticks trick, hopefully that won’t be necessary.
I didn’t get that impression. I think Ed was making a valid point that people shouldn’t have to install software - however valuable it might be for other uses - just to grab a script from this forum.
Thanks for the correction.
This info should really be more prominent, like in a pinned thread somewhere. I’ve been here almost since day 1 and I’ve only just learned how this is done.
In a couple of weeks time this thread will be buried and newcomers will be forever asking how it’s done.
Just an FYI, I’ve been copying and pasting script samples from the forum into scripts with no problem. (Although when that link becomes available, that will be good).
But today I tried to copy and paste the script into an email (Outlook) and while it looked OK on my end, the received message looked terrible and was unusable. (see below).
I don’t know if that’s an outlook issue, or a general email issue or what, but if there’s anything that can be done to avoid the issue or fix it in the forum, that would be good.
use
AppleScript
version
"2.5"
10.11 or later
use framework "Foundation"
use framework "AppKit"
use framework “Carbon”
AEInteractWithUser() is in Carbon
use scripting additions property
returnCode : missing
value
on
showMessage:theMessage withTitle:boldBit textFrame:textFieldSize textMaxWidth:maxWidth withButtons:buttonsList
credit to Shane Stanley for this handler
make attributed string system font with monospaced digits
set
fontSize to
current
application’s
NSFont’s systemFontSizeForControlSize:(current
application’s
NSRegularControlSize) set
theFont to
current
application’s
NSFont’s monospacedDigitSystemFontOfSize:fontSize weight:(current
application’s
NSFontWeightRegular) set
attsDict to
current
application’s
NSDictionary’s dictionaryWithObject:theFont forKey:(current
application’s
NSFontAttributeName) set
attString to
current
application’s
NSAttributedString’s alloc()'s initWithString:theMessage attributes:attsDict –
make a text field to hold the message
set
theField to
(current
application’s
NSTextField’s alloc()'s initWithFrame:textFieldSize) tell
theField (its
setEditable:false)
(its
setBordered:false)
its
setDrawsBackground:false
its
(cell()'s setWraps:true)
its
setPreferredMaxLayoutWidth:maxWidth its
setAttributedStringValue:attString end
tell
make it fit; needs to be done on the main thread
my
performSelectorOnMainThread:"fitToSizeView:"
withObject:theField waitUntilDone:true
make sure we have permission
set
theError to
current
application’s
AEInteractWithUser(-1,
missing
value,
missing
value)
-1 is kAEDefaultTimeout
if
theError is
not
0
then
error
"User
interaction disallowed"
number
theError –
create an alert set
theAlert to
current
application’s
NSAlert’s alloc()'s init() tell
theAlert its
setMessageText:boldBit repeat
with
anEntry in
buttonsList (its
addButtonWithTitle:anEntry) end
repeat
its
setAccessoryView:theField end
tell
show the alert; needs to be done on the main thread
my
performSelectorOnMainThread:"showTheAlert:"
withObject:theAlert waitUntilDone:true
set
buttonNumber to
returnCode mod
1000
- 1
–
where 1 = right-most button
set
buttonName to
item
buttonNumber of
buttonsList return
buttonName end
showMessage:withTitle:textFrame:textMaxWidth:withButtons: on
showTheAlert:theAlert #
credit to Shane Stanley for this handler
–
check we are running in foreground
if
not
(current
application’s
NSThread’s isMainThread()) as
boolean
then
error
"This
handler must be called on the main thread.“
from
current
application
set
my
returnCode to
theAlert’s runModal() end
showTheAlert: on
fitToSizeView:aView #
credit to Shane Stanley for this handler
aView’s setFrameSize:(aView’s fittingSize()) end
fitToSizeView: #
set up the parameters for the showMessage call:
set
theMessage to
"How
many buttons would you like? There’s no real limit except for practical and aesthetic considerations.”
& return
& “Of
course, I hope you’ll never really think about using something as ugly as this!”
& return
& return
& "Choose
your heart out!“
set
theTitle to
"Many
Buttons”
increase or decrease the second item’s numbers
to fit larger or smaller amounts of text
the ‘650’ here is the text field’s width
the ‘80’ is its height
set
theTextFieldSize to
{{0,
0},
{650,
80}}
set
buttonsToDisplay to
{“OK”,
“Five”,
“Four”,
“Three”,
“Two”,
“One”,
“Cancel”}
set
theButtonReturned to
its
showMessage:theMessage withTitle:theTitle textFrame:theTextFieldSize textMaxWidth:(650)
withButtons:buttonsToDisplay
Hi Ed
I’ve been caught out by the pasteboard adding formatting (often the wrong quotes marks, as well as line endings) so many times when copying from webpages that as a matter of habit I always paste a script into the editor and check it compiles first, then copy it from there directly before pasting it into anywhere else.
I’ve had this happen as well.
Two other workarounds:
- Attach script as a file
- Send the email as plain text
All good solutions.
If possible, I think it’s better to fix at the source.
They Yahoo group and the MacScripter list both allow copy and paste directly from displayed, formatted appleScript into email (and other apps). The result is plain text, which works just fine.
That would be better than what I’m seeing here.
Interesting. I have copy/paste a lot of scripts from other Discourse forums, and have never noticed this – never had an issue. Then I realized I am always pasting into an app that is expecting plain text.
I just tested pasting into Evernote and Outlook 2011, and in both cases the script was not pasted correctly.
BUT, when I did a “Paste & Match Style”, the script pasted correctly into both.
Thanks to a great little app named Script Debugger (ever hear of it?), I can easily discover the classes put on the clipboard:
set clipRec to the clipboard as record
shows this:
The problem is the «class HTML» class.
Thanks to Shane for some really cool code:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit" -- for pasteboard
### SHANE'S CODE ###
set pb to current application's NSPasteboard's generalPasteboard()
-- read as html
set htmlText to (pb's stringForType:"public.html") as text
I can get the actual HTML text from the encoded «class HTML» class.
I have to say it is shocking! It is very verbose, may worse than the HTML code that Microsoft Word produces. I would post it here, but it is too long. Just run the above script after a copy of script from a script block somewhere in this forum, and you will see the results. As you know, HTML ignores CR and LF, and needs <BR>
for line breaks. I didn’t see any in the HTML text. Or, it needs to be put in a block using <pre>
tags. Didn’t see any of these either.
###The Workaround
Use “Paste & Match Style” in rich text apps.
OR, use this simple script (enhance as you see fit):
set scriptStr to the clipboard as text
set the clipboard to scriptStr as text
###The Fix
I’ll report this in the Discourse forum (meta.discourse.org), and see if they are aware of it, and if so, have any fixes/suggestions.