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.
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).
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.