Script Debugger Script

Trying to write a script for SD that’ll take out extra returns. Here’s what I have, but this is one of many variations I haven’t been able to get to do anything. It doesn’t error out or anything. Just does nothing. :stuck_out_tongue:
Can anyone gimme’ a nudge?

tell application "Script Debugger"
	tell first script document       
		set theList to every line of selection
		repeat with c in theList          
			if c contains {return & return} then            
				delete c
			end if
		end repeat
	end tell
end tell

: Trying to write a script for SD that’ll take out extra returns.
: Here’s what I have, but this is one of many variations I
: haven’t been able to get to do anything. It doesn’t error out
: or anything. Just does nothing. :stuck_out_tongue:
: Can anyone gimme’ a nudge?

[code snipped 'cause it didn’t look good in the reply]
I don’t think a single line can contain more than one return,
which might explain why the script is doing nothing. You
might need to look at the first and last characters of more
than one line at a time to do what you want.
– Rob

I said:
: I don’t think a single line can contain more than one return,
: which might explain why the script is doing nothing. You
: might need to look at the first and last characters of more
: than one line at a time to do what you want.
T.J.,
I’m just guessing here but…
I don’t know if you can actually modify a running script (if that is
in fact what you are trying to do). A workaround would be to use
a second script which cleans up the target script. For example:
– Begin untested pseudo code –
tell application “Script Debugger”
set dirtyScript to contents of script document 1 – document 1 is the target
– do the cleanup work here
set cleanedText to the result
set script document 1 to cleanedText
compile script document 1
end tell
– End pseudo code –
To further complicate the cleanup process, you’ll need to deal
with the tabs that are inserted into a compiled script. I’d work
on this myself but I’m running short on time right now. Sorry
'bout that.
– Rob

Bill Hoffman:
Thanks so much for the time you took to provide your input on this. Here’s the script (many thanks to Bill “The Man” White) which ultimately served my purposes beautifully.

tell script document 1 of application "Script Debugger"
   repeat with i from (count lines) to 1 by -1
     if line i contains (tab & return) or line i is return then set line i to ""
   end repeat
end tell

And it’s SO perfect, it brings a tear of joy to my eye. :wink: (And a tear of frustration that I couldn’t come up with it on my own. lol) Be well.

: Trying to write a script for SD that’ll take out extra returns.
: Here’s what I have, but this is one of many variations I
: haven’t been able to get to do anything. It doesn’t error out
: or anything. Just does nothing. :stuck_out_tongue:
: Can anyone gimme’ a nudge?
T.J.:
Try this:

tell script document 1 of application "Script Debugger"
   repeat with i from (count lines) to 1 by -1
      if line i contains (tab & return) or line i is return then set line i to ""
   end repeat
end tell

– Bill

Bill, there’s a tear in my eye.
It’s perfect. sniff
Now why the heck couldn’t I figure that out?!
Can you give me an idea as to why mine didn’t work? Or why my approach was all wrong?
T.J.

use javascript osa and this regex pattern in a string.replace on the contents of the document.
regexPattern = /^[s]*[n]/mg;
see the script code for the included “Stingify” script for a straightforward sample of how to implement a jsosa script.
I’ve written (and tested) a script in jsosa that strips multiple blank lines. posting it here just ruins the formatting…email me if you want it.

posting it here just ruins the formatting
oh, maybe it doesn’t -:slight_smile:
here’s the script. the advantage to this is that it preserves single blank lines, but strips all occurences of multiple blank lines.

var regexPattern = /^[s]*[n]/mg;
with (MacOS.appSelf()) {
if (window[1].exists()) {
if (script_document[1].debugger_state.toString() == _constants.stopped.toString()) {
var theText = script_document[1].contents;
var processedText = theText.replace(regexPattern, "n");
script_document[1].contents = processedText;
}
else
display_error("Script “" + script_document[1].name +
"” is running or recording and cannot be changed." +
"nnStop the script and try again.");
}
else
display_error("There are no open windows"); }

: Bill, there’s a tear in my eye.
: It’s perfect. sniff
: Now why the heck couldn’t I figure that out?!
: Can you give me an idea as to why mine didn’t work? Or why my
: approach was all wrong?
T.J.,
Your approach was fine and your thinking was sound. It’s just that AppleScript was “thinking different.”
In your original script theList returns just that: a list. And theList is just a text listing of each line of the script and is, as such, “detached” from the script itself. So to then say “delete c” (where c is really an entry in the list but in your mind a line of the script) is meaningless. Your intentions are intuitively good but in this case you can’t do what intuition leads you to want to do.
The only other problem I saw was that none of your lines (if memory serves me) contained (return & return). They were either just returns or had a tab in front of a return in them.
I’m glad my small solution worked. But for the life of me I can’t see why you’d want to get rid of all that readability-rendering wonderful white space!
:slight_smile:
– Bill