Simplified question to cut text and rename

Hi,
I need to break a long TextWrangler doc in various smaller ones

I manually select each scene from the main document and in TEXTWRANGLER it gives me the option to open a new document with the clipboard content


tell application "TextWrangler"
	activate
	cut selection
	make new text document with properties {contents:current clipboard}
end tell

However I do not know how to add a command in this Applescript which will automatically select the first paragraph of the new files always ending with a carriage return and use it as a title for the new file and save it in a predefined destination folder

Thanks a lot

Hi danwan,

Instead of paragraphs, I think they use ‘line’. e.g.

tell application "TextWrangler"
	activate
	select line 1 of front document
end tell

Not sure about this and there may be easier ways to write your script.

Edited: what I mean is that using selection often slows down your script. The best way is to get all the text (if formatting doesn’t matter), then manipulate that text without the app slowing it down. But, a lot of it depends on what you’re trying to do.

gl,
kel

Thanks,
however once the script selects the first line of the new document the document name will still be “untitled num …”.
how to save the new document giving the content of the clipboard as the name?


tell application "TextWrangler"
	activate
	cut selection
	make new text document with properties {contents:current clipboard}
	copy line 1 of front document
end tell

the script works but I hope to avoid pasting the content of line one in the save box

Dan

Hi danwan,

I didn’t look at the dictionary to see if ‘name’ is a property of document. Try looking in the Text Wrangler’s dictionary under ‘document’. I’ll do it as soon as I can.

gl,
kel

Oh I see what you’re saying. Let me think about how to name the file.

Later,
kel

I’m haven’t tried this yet, but you may be able to create a file specification. That’s a reference to a file that does not exist yet. Then, you use ‘save’ to file in Text Wrangler.

gl,
kel

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