Simplified question to cut text and rename

Hi danwan,

Good news! I think it can be done. Checking on the saving part now. How are you doing in your pursuit?

gl,
kel

Thanks again
however I am a bit old in age and I don’t really understand … sorry for this
I don’t know how to convert the clipboard content into a file name
I am trying this, but I am not sure this is what you mean


tell application "TextWrangler"
	activate
	cut selection
	make new text document with properties {contents:current clipboard}
	copy line 1 of front document
end tell
tell application "System Events"
	tell process "TextWrangler"
		keystroke "s" using command down -- Save As.
		tell sheet 1 of window 1
			repeat until exists
				delay 0.1
			end repeat
			keystroke "V" using command down
			delay 0.1
			keystroke return -- Save
			keystroke "w" using command down
			
		end tell
	end tell
end tell

There must be a better way than using select.

Later,
kel

Hi danwan,

You might think about doing it like this. To get a line:

tell application "TextWrangler"
	activate
	contents of line 3 of front document
end tell

All you need to find is the number of lines.
gl,
kel

Thanks kel
the scrpt I sent you does everything I need
it works as requested
Dan

Hi danwan,

I’ve been experimenting with TextWrangler. I didn’t realize how scriptable it is.

Here’s something like what I meant about not using ‘select’:

tell application "TextWrangler"
	activate
	set line1 to contents of line 1 of document 1
	set contents of line 1 of document 1 to ""
	set first_char to contents of first character of document 1
	set first character of document 1 to ""
end tell
delay 2
activate me
{line1, first_char}

(* some text
the rain in Spain
stays mainly in the plain
the quick brown fox
jumps over the lazy dog
*)

The last line in the tell block was the only way I could figure how to get rid of the end of line character so far. I ran this script in AppleScript Editor.

Edited: btw, you can undo the scripting in TextWrangler! I did not know that.

gl,
kel

Hi ranwan,

If you’re still here, there might be a better way. TextWrangler has this:

So you may be able to get and delete line 1 using regular expressions. I haven’t tried it yet, but it sounds like a good way to do part of what you’re doing.

Edited: here’s a grep tutorial that’s pretty easy to understand if you didn’t know much about it:
http://www.grymoire.com/Unix/Grep.html. It looks updated.

Edited: this updated version is not so good. I have the old tutorial somewhere I think if you want it. Not sure if that’s legal though.

Edited: this is the better tutorial:
http://www.grymoire.com/Unix/Regular.html

gl,
kel

Hi,

I don’t have TextWrangler but the dictionary of BBEdit is quite similar.
Both are pretty well scriptable, GUI scripting or using the clipboard is not needed.
Try this, I commented out the line to cut the selection.


set baseFolder to path to desktop as text
tell application "TextWrangler"
	set theSelection to contents of selection
	-- cut selection
	set newDocument to make new document with properties {contents:theSelection}
	set fileName to contents of line 1 of newDocument & ".txt"
end tell

-- replace colons with underscores
set {TID, text item delimiters} to {text item delimiters, ":"}
set textItems to text items of fileName
if (count textItems) > 1 then
	set text item delimiters to "_"
	set fileName to textItems as text
end if
set text item delimiters to TID

tell application "TextWrangler"
	save newDocument to file (baseFolder & fileName)
end tell


I think you need to change these application properties to search:

Getting there.

gl,
kel

Thanks guys …
I like Textwrangler as it interacts wonderfully with Applescript and has great recording functions which always work.
So I often use textwrangler even as a blue print for other apps and it almost always gives suggestions I will eventually fine tune looking at other scripts

I only wished Apple computers which gave this great tools would make is own applications recordable but unfortunately this isn’t the case …

Dan

TextWrangler and BBEdit as of 10.x allow you to find/replace within a string - not just the text of a document.


---------------------------------------------------
# Find and Replace within a literal string.
---------------------------------------------------
set _string to "This is your file name 1
"
# Removing whitespace at the end of a string:
set fileName1 to replStr("\\s\\Z", "¢", _string)

set _string to "This is your file name 2"

# Replacing spaces with underscores:
set fileName2 to replStr(" ", "_", _string)

on replStr(findStr, replStr, srcStr)
	tell application "TextWrangler"
		set newString to replace findStr using replStr searchingString srcStr options {search mode:grep}
	end tell
end replStr
---------------------------------------------------

Keep in mind that many of the search options (in AppleScript - not the find dialog) are defaults that need not be mentioned unless you make them true.

HOWEVER:

A scripted search will start at the cursor UNLESS you set starting at top to true.

This can make you crazy when testing if you forget, because something won’t be found - even though you know it’s there - and you’re sure you got the syntax right.

Another handler.


-------------------------------------------------------------------------------------------
# Find the text of the first line of text window 1
-------------------------------------------------------------------------------------------
tell application "TextWrangler"
	set findReco to twFind("\\A.+", (a reference to text window 1)) of me
	if found of findReco = true then
		set fileName to found text of findReco
	end if
end tell
-------------------------------------------------------------------------------------------
on twFind(findStr, targetWin)
	tell application "TextWrangler"
		tell targetWin
			find findStr options ¬
				{search mode:grep, case sensitive:false, match words:false, extend selection:false, starting at top:true}
		end tell
	end tell
end twFind
-------------------------------------------------------------------------------------------

If memory serves the defaults for these options are:

case sensitive: true
extend selection: false
match words: false
starting at top: false

Hi,

Finally figured out how to use the find and replace commands. Here’s what I ended up with to get the first non blank line:

tell application "TextWrangler"
	activate
	
	set r to find "..*\\n" searching in text 1 of document 1 options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	set {was_found, found_object, found_text} to {found, found object, found text} of r
	if was_found then
		say "found"
		set contents of found_object to ""
	else
		say "not found"
	end if
	
end tell
delay 2
activate me
{found_object, found_text}

(* some text
the rain in Spain
stays mainly in the plain
the quick brown fox
jumps over the lazy dog
*)

Still experimenting with it.

Edited: note that the found text contains the linefeed which you will want to exclude in the name of the file.

gl,
kel

Finding the first non-blank line doesn’t require regular expressions.

Although this will fail if there is white-space within a “blank” line that precedes any text.


tell application "TextWrangler"
	tell text of front text window
		set fileNameText to contents of first line whose contents is not ""
	end tell
end tell

Hi Chris,

I didn’t know that ‘.’ (dot or period) meant not white space. I thought that the period meant any character but some end of line or end of file. I’ll have to look at that again.

Edited: I see now. You don’t want the name to start with white space. Need to think about that.

Thanks,
kel

Finding “…*\n” will find a line starting with whitespace or containing only whitespace.

Remember that ‘.’ is any character.

The search-options required for this job are minimal.

The following regex finds the text of the first line that starts with a non-whitespace character.


tell application "TextWrangler"
	tell text of front text window
		set findReco to find "^\\S.*" options {search mode:grep, starting at top:true}
	end tell
	if found of findReco is true then
		set foundText to found text of findReco
	else
		error "Text was not found!"
	end if
	set fileName to replace "\\A\\s*(\\S+.+?)\\s*$" using "\\1" searchingString foundText options {search mode:grep}
end tell

The problem with using the result here is that the text may contain trailing whitespace, so I’d want to massage the text before using it. The last line of the script removes any leading or trailing whitespace (although the find regex prevents any leading whitespace).

Hey kel,

‘.’ indeed finds any character including whitespace but excluding EOL characters unless you use the magic dot modifier (?s).

So “…*\n” will find:

“\n”

or

“Something\n”

Etcetera.

So lets try to make the pattern more specific:

^ == Anchor at beginning of line.
\S == Non-whitespace character.
. == Any character.

  • == Zero or more.

^\S.*

That still leaves us with possible trailing whitespace, but I’ve addressed that in my last script.

It’s handy that TextWrangler allows replacing within a string.

This is a bit easier with the Satimage.osax which is why I use it so much.

Hi Chris,

I was thinking that in the search, there would be something like: “first line that does not start with space, linefeed, or eof”. Then, we can use that as the name. I forgot how to to ‘and’ in the regular expression.

Thanks,
kel

My previous pattern will do that, but you can be more specific:

^[^[:blank:]].+

Beginning of line; any-non-horizontal-whitespace-character; any-character-zero-or-more.