A script that makes formatted Applescript comment of the clipboard

Hello.

This script is meant for pasting in descriptions of scripts into them, for later reference.

It removes any styles, from the text you have copied onto the clipboard, formats it to a width of 60 characters and inserts “”” in front of every line, so you can just paste the comment into the script.


set the clipboard to «class ktxt» of ((the clipboard as text) as record)
do shell script ("pbpaste |fmt 60 60 | sed -n 's/^\\(.*\\)$/-- \\1/p' |pbcopy")

Enjoy! :slight_smile:

This is better when there are Unicode-only characters: :slight_smile:

set the clipboard to (do shell script ("echo " & quoted form of (the clipboard as Unicode text) & (" | fold -sw 60 | sed -e 's/^\\(.*\\)$/-- \\1/g' -e 's/" & return & "/\\" & linefeed & "-- /g'")))

Hello!

Nigel Garvey wrote:

Thanks! :slight_smile:

I’ll Try it! I hadn’t come around to that yet.

Hello! :slight_smile:

Hello, here are the results from a non unicode page, (it was rendered with MacRoman), as you can see neither yours, nor mine suffered from the encoding. I hardly stumble over characters that makes for bad results nowadays.

Here is the result of the formatting:

– Yours

– Useful script examples for Safari
– Aug 04, '10 07:30:00AM ¢ Contributed by: nathanator11
– I was working with AppleScript for Safari, and decided to
– make a script that demonstrated some of the handy things
– you could do. Hope they prove as useful to others as they
– were for me.

– MIne
– Useful script examples for Safari Aug 04, '10 07:30:00AM ¢
– Contributed by: nathanator11I was working with AppleScript
– for Safari, and decided to make a script that demonstrated
– some of the handy things you could do. Hope they prove as
– useful to others as they were for me.

I am not challenging the Dark Lord of Warwickshire in any way, but it must be allowed to say that I like mine better, although yours formats the comment more precisely, I’ll see if I can come around with a compromise. :wink:

Right. Mine needs not to enquote an empty last line. Yours has a problem (in this case) between “nathanator11” and “I”.

Here’s a reworked version of mine. The “sed” bits replace all returns with linefeeds, then enquote all the lines, then unenquote the last line if it’s otherwise empty. I’ve had to use three separate “sed” commands for this rather than a single one followed by -e options. If there’s more than one empty line at the end, only the last one’s dequoted ” which I regard as a feature rather than a weakness. :wink:

Although “fold” inserts linefeeds to break lines, it recognises linefeeds, returns, and CRLFs as existing breaks at which to start counting, so it doesn’t matter if the first “sed” command comes before or after it.

set the clipboard to (do shell script ("echo " & quoted form of (the clipboard as Unicode text) & (" | fold -sw 60 | sed 's/" & return & "/\\" & linefeed & "/g' | sed 's/^.*$/-- &/g' | sed '$ s/^-- $//'")))

Hello! :slight_smile:

Your solutiion is best.

I actually resorted to groff, what I didn’t take into account, was the compiler wiping out the whitespace.

It would have looked like this really, but with totally straight edges. My solution is only good for my text editor,
one of the days I’ll write a groff filter for BBEdit!

The text I used for comparison was from the fine article about screen sizing by John Gruber at DaringFireball.net
http://daringfireball.net/2006/12/display_size_applescript_the_lazy_way

Yours

-- The tricky part in writing a script to do this is -- determining the height of the screen. (Or the width, for -- that matter, if I wanted to do something to make sure all -- the windows fit on screen, no matter how many there are.) I -- wasn't aware of any way to obtain the size of the screen -- using AppleScript. I could hard code a size into the -- script, but the main reason I wanted to write the script in -- the first place is that I switch between my 20-inch Cinema -- Display and the built-in display of my 15-inch PowerBook, -- which (the switching) can leave browser windows in weird -- sizes. I want the same script to work with both displays.
Mine

(* The tricky part in writing a script to do this is determining the height of the screen. (Or the width, for that matter, if I wanted to do something to make sure all the windows fit on screen, no matter how many there are.) I wasn't aware of any way to obtain the size of the screen using AppleScript. I could hard code a size into the script, but the main reason I wanted to write the script in the first place is that I switch between my 20inch Cinema Display and the builtin display of my 15inch PowerBook, which (the switching) can leave browser windows in weird sizes. I want the same script to work with both displays. *)
This is what I used, and the other problem, is that it seems like groff only handles 7 bit ascii, I may be wrong in that assumption, but, it really seems so, or there is some other oddity, that I have no time for at the moment. (yes, I have had a go with iconv trying to fix it, maybe it is windows latin that is the cure.

You should however try to paste the output of my script into your regular text editor :slight_smile: (the script below this.)

Comment Formatter/Reformatter


set the clipboard to «class ktxt» of ((the clipboard as string) as record)
set the clipboard to "(*\n" & (do shell script ("pbpaste |tr  \"'\" \"'\" |tr -d '#' |sed -E 's/[-][-]//g' |iconv -f utf-8 -t latin1 |groff -c -i -s -T utf8 |tr  \"'\" \"'\" |grep -E [:print:]\\+")) & "\n*)"

Edit I have been back fixing my solution a little, it is of course of no use for formatting AppleScript, it was just for the hell of it, but now I believe it handles unicode as well, when the dreaded back tick is filtered away. Your solution is still the right one and it is not about winning, not for my part, just saying. So I’ll end up using your script I think, while mine is a great formatter for pure text.

I got some ideas during messing with this, (quite fun!) so now my script for formatting comments can reformat them, after I have edited them! :slight_smile:

Here it is, slightly revamped, as a text formatter, that doesn’t accept lines starting with #`s! they are interpreted as comments! :smiley: That means you really can’t reformat Markdown with this either. :expressionless:

Standard Text Formatter


set the clipboard to «class ktxt» of ((the clipboard as string) as record)
set the clipboard to (do shell script ("pbpaste |tr  \"'\" \"'\" |tr -d '#'  |iconv -f utf-8 -t latin1 |groff -c -i -s -T utf8 |tr  \"'\" \"'\" |grep -E [:print:]\\+"))

Final Words

I can actually reformat comments, where they given in my format for starters, so I have gone back, and added even a sed command, so that I can reformat comments already in applescript as well, taking any kind of comment, whether they start with “#” or “””, assuming you leave “(*” and “*)” above and below the comments, that shouldn’t be too hard.

End of Story

It should work properly now.

Neat. (Literally!) But perhaps fully justified comments are just a little OTT in a script. :lol:

I hope it’s not. It should be about being inspired to look for different, better, and/or just more interesting ways of doing things, and having fun and learning something in the process. I’ve learned a bit more about sed and have become aware of fold and groff. Thanks! :slight_smile:

Hello! :slight_smile:

It is about getting the ultimate solutions!

Well, having tried my version, it is useless, when you are to format a script really.

My version messes up the lines, and compacts the whole, to one single paragraph, not very wanted for, when the comment consists, of text and some code, or a list with stuff!

So, I end up with using yours, but with the possibility of reformatting comments. :slight_smile:

I decided on this style because it makes editing of comments easier. And I kept the way of styling the clipboard because it makes the commandline, a little bit easier, I could have used a ( cmd1 ; cmd2 ; cmd3 ) approach to make one long line, but I don’t see the point really.

Here it is: A collabrative effort!


set bugger to ASCII character 202
set the clipboard to «class ktxt» of ((the clipboard as string) as record)
set the clipboard to "(*
" & (do shell script (" pbpaste |tr \"" & bugger & "\" \" \" |tr -d '#' |sed -E 's/[-][-]//g' |fold -sw 60 | sed 's/" & return & "/\\" & linefeed & "/g' ")) & "
*)"


A I’ll see if I manage to rework the text formatter, because being able to straighten up markdown, would have been nice actually. The first step, is to keep the paragraphs.

And it is good to be able to contribute with something now and then as well. I have learnt a whole lot from you!

Hello!

Below is the Text formatter it handles Markdown input, and Unicode text you are in trouble if you have sequences of @%_, as I use that as a sigil.

You copy the text from your editor to the clipboard, and runs the scripts, and you get nicely formatted text back.

It can for instance be used to format stickies with!


-- http://macscripter.net/viewtopic.php?id=39004

set the clipboard to (do shell script ("echo " & quoted form of (the clipboard as Unicode text) & (" | tr  \"'\" \"'\" | sed 's/#/@%_/g' |iconv -f utf-8 -t latin1 | sed 's/" & return & "/\\" & linefeed & "/g'  |groff -c -i -me -T utf8 |sed 's/[@][%][_]/#/g' |tr  \"'\" \"'\"  |cat -s")))

I think this concludes it all, it was fairly interesting.

cat -s removes trails of newlines that is what I will remember the most! :slight_smile:

Here is a script I use for double and triple spacing man files It is nice to read it in the terminal really, when the colours and fonts are how you like them. Give it a try, it is faster than Anything you can come up with in Perl, without the logic!

Usage:

man -s 7 reformat |doublespace |more

[code]#! /bin/bash

### EDIT Prints text files with double linespacing or optionally triplespacing." !#

declare prgname version Usage triplespace
prgname=doublespace
Usage=“Usage : $prgname [-huv3]|[–help --usage --version --triple] | [filename]”
PATH=“/bin:/usr/bin”
version=“$Revision: 1.3 $”
version=${version%$}
phelp()
{
echo $Usage 1>&2
echo “Prints text files with double linespacing or optionally triplespacing.” 1>&2
}
usage()
{
echo $Usage 1>&2
}
version()
{
echo $prgname" version “$version 1>&2
echo “$prgname is copyrighted 2008 by McUsr and put in the Public Domain.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.” 1>&2
}
if [ “x$1” = “x-h” -o “x$1” = “x–help” ] ; then
phelp
exit 0
elif [ “x$1” = “x-u” -o “x$1” = “x–usage” ] ; then
echo $Usage 1>&2
exit 0
elif [ “x$1” = “x-v” -o “x$1” = “x–version” ] ; then
version
exit 0
fi
if [ “x$1” = “x-3” -o “x$1” = “x–triple” ] ; then
triplespace=true
shift
else
triplespace=false
fi
if [ -t 0 ] ; then # connected to tty
if [ $# -ne 1 ] ; then
echo -n “$prgname”” : Needs a file to " 1>&2
if [ $triplespace = true ] ; then
echo " triplespace." 1>&2
else
echo " doublespace." 1>&2
fi
echo $Usage
exit 1
else
for curfile in “$@” ; do
if [ $triplespace = true ] ; then
sed ‘G;G’ <$curfile
else
sed ’ s/$/
/’ <$curfile
fi
done
fi
elif [ $triplespace = true ] ; then # triplespace da stream
sed ‘G;G’
else # doublespace stream.
sed ’ s/$/
/’
fi[/code]
Enjoy! :slight_smile:

Hi, McUsr.

I tried this with your test text in post #6. I’m afraid it cut the last three lines and tripled the apostrophe. :frowning:

Hello! :slight_smile:

It is funny that the em-dash didn’t turn up before now, well, now it is translated, I did translate it back again first, but you have programs for doing such conversions, the back tick is another story, so I there is a one way translation of the em-dash!

And just to say it, I only guarrantee that you keep Markdown headers, ( I see no problem with Settext style headers, which looks much better anyway), whatever else happens with codeblocks and such within a Markdown document is nothing I am going to spend time on at the moment, but I’ll update the formatter here as I go along and fix it, if I should. I am happy if I keep my headings! :smiley:

New version, that handles “em-dash” characters



-- http://macscripter.net/viewtopic.php?id=39004
set bugger to ASCII character 202
set the clipboard to (do shell script ("echo " & quoted form of (the clipboard as Unicode text) & (" |tr \"" & bugger & "\" \" \" | tr  \"'\" \"'\" |  tr  \"\" \"-\" | sed 's/#/@%_/g' |iconv -f utf-8 -t latin1 | sed 's/" & return & "/\\" & linefeed & "/g'  |groff -c -i -me -T utf8 |sed 's/[@][%][_]/#/g'  |tr  \"'\" \"'\"  |cat -s")))


Enjoy!
Have a nce evening from a beautiful sunny afternoon in Norway!

Hello! :slight_smile:

I have updated the script in the post above and post #8 to filter out hard spaces.

I had an incident recently, where a bash function library started to not working properly, after I had snagged some code from a web page, due to hard line breaks ascii character 202 (The show up like large round dots in TextWrangler and BBEdit when invisibles are turned on. )

I also toss in this here, as it is appropriate, when copying code from web-pages it just replaces the hard spaces with normal spaces.

tell application "Safari" to set the clipboard  to (do JavaScript "\"\"+window.getSelection();" in document 1)
set dirtyText to the clipboard
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {ASCII character 202}
set cleanText to every text item of dirtyText
set AppleScript's text item delimiters to oldDelims
set cleanText to cleanText as text
set the clipboard to cleanText