AppleScript markup question

Is there a way to get markup to treat tabs in scripts copied from SD as 4 spaces rather than 8? (when surrounding a script with triple-backquotes and adding “AppleScript” on the first line)

How about something like this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theText to the clipboard
set AppleScript's text item delimiters to {tab}
set theTextItems to text items of theText
set AppleScript's text item delimiters to {"    "} -- 4 spaces
set theText to "```AppleScript" & return & (theTextItems as text) & return & "```" & return

set the clipboard to theText
theText

Should work, but seems extreme. I will have to keep my eye out for the formatting of other people’s scripts. I can’t see why I wouldn’t have noticed this before with Markdown being used on so many sites.

Hey Mitchell,

Over the years I’ve found it problematic to replace tabs in scripts en mass.

Sometime, somewhere there will be one or more literal tabs in places that will break your script if they get replaced – and they can be hard to find and fix.

So for some while now I’ve taken to replacing only the leading tabs – I usually crunch them down to 3 spaces each but 4 is fair.

--------------------------------------------------------------------------------
# Auth: Christopher Stone { ASObjC by Shane Stanley }
# dCre: 2016/05/08 00:59
# dMod: 2016/05/08 01:09
# Appl: AppleScriptObjC
# Task: Replace leading tabs in AppleScript with 4 spaces.
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Find, @Replace, @Text, @RegEx
--------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
--------------------------------------------------------------------------------

set dataString to "

tell application \"Safari\"
	tell (windows where its document is not missing value)
		{name, tabs}
	end tell
end tell

"

set newStr to its cngStr:"(?ms)(\\A\\s+|\\s+\\Z)" intoString:"" inString:dataString
set newStr to its cngStr:"(?m)(?:\\G|^)\\t" intoString:"    " inString:newStr
set newStr to "```applescript" & return & newStr & return & "```"

set the clipboard to newStr

newStr

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
      options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
--------------------------------------------------------------------------------

-Chris

I have a Keyboard Maestro macro that starts with styled AppleScript copied to the clipboard – crunches it similarly to the script above – but returns styled text to the clipboard.

That way I can crunch down nicely styled text for forums that accept it like the Applescript Users List and still paste plain-text into forums like this one (using the same macro).

-Chris

Want ti wake up this discussion and take in perhaps another direction. Is there any to just turn off entabbing in scripts? Tabs are nice sometimes, but they keep screwing up a script I have that is generates multi-line strings with specific indentation on each line. (HTML lists for example)

Furthermore, I just ran into a surprise: I deathbed a file with my usual setting of 4 spaces per tab. Then I thought I was being clever and set the tabbing to 20 figuring that meant it would take 20 spaces before it would tab, and then it would insert 1 tab. Hopefully, I wouldn’t reach an indent of 20 spaces, and if I did I’d deal with it by splitting the line up. However, to my surprise, when I compiled the file with just spaces, it indented everything massive numbers of spaces.

OK, spoke too fast as often. Spaces inside strings are not replaced by tabs. There was something else wrong with my strings.

The issue of how tabes get handled can be confusing because AppleScript deals only in tabs and will reformat your code based on the logical indentation of your code rather then the explicit indentation in your source text. This is part of the generally annoying feature of AppleScript will reformat line continuations and other stylistic formatting in the way that it sees fit.

The Tab Width setting (in the Editor preferences panel) within Script Debugger controls how many space-widths Script Debugger uses for each tab.

Note that AppleScript will not translate spaces/tabs within string literals and (* style *) comment blocks.

Aha. I found out one of the reasons I thought there were tabs in strings. I have a multi-line string. The last line is four spaces a word then the terminating quote. When I put the cursor before the word and hit DELETE all four spaces go away. I thought that was a tab that got deleted. Why would four spaces be deleted in that situation — is it possibly a bug? I’ll send you the text if you want.

Turn on Show Invisibles to see spaces and tabs explicitly displayed.