Extracting scripts from forum posts

Ed’s line should be a link (it is here).

The problem, though, is that the Discourse software doesn’t generally support this approach at present.

Hey Shane,

It shows as an apparent link in Safari and Chrome, but there’s nothing connected to it – even when you inspect the source.

-Chris

Ah – I could have sworn it was working earlier.

Trying again:

Open this Scriplet in your Editor:



use AppleScript version "2.4"

use scripting additions

use framework "Foundation"



timer(“Start”)


YOUR CODE

— NOTE: The below shell scripts have nothing to do with the timer() handler —



set cmdStr to "perl -e ‘use Time::HiRes qw(time); print time’"

set timeStart to do shell script cmdStr

timer(“After First Shell Script”)

set timeStop to do shell script cmdStr



timer(“STOP”)

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:

  1. 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).
  2. 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 #2, see this script by Chris Stone, above.

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.

1 Like

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.

1 Like

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:

  1. 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.
  2. 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.

Definitely not necessary. And it is only 3 backticks.
See above.

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.