On Jan 22, 2009, at 9:48 AM, Doug Pinkerton wrote:

Doug,

Here's a script that renumbers your items, and basically shows a  
couple of handlers that are re-useable.

It could be done much shorter, but this is mostly for learning purposes.

Hope it helps,

Bill Hernandez
Plano, Texas

----------------------------------------
[2401] ( BEGIN ) renumber_items.scpt
----------------------------------------
on run
        tell application "BBEdit"
                activate
                try
                        set this_file to file of text window 1
                on error
                        set the_msg to "Please make save the document, and try 
again..."
                        set the_time to 10
                        my bh_dialog_show_msg(the_msg, the_time)
                        return
                end try
        end tell
        
        tell application "BBEdit"
                activate
                set findCmdOptions to {search mode:grep, starting at top:false, 
wrap  
around:false, backwards:false, case sensitive:true, match words:true,  
extend selection:false}
                
                -- go to the beginning of the document since we are calling for 
 
"starting at top:false" in findCmdOptions
                my bb_goto("beginning")
                
                -- This shows the complete search regex
                set s to "(^[ \\t]*)(<Object no=\")([\\d]+)(\">)"
                
                -- This shows it broken down into manageable pieces, making it  
ieasier to change and understand
                set s1 to "(^[ \\t]*)"
                set s2 to "(<Object no=\")"
                set s3 to "([\\d]+)"
                set s4 to "(\">)"
                
                -- now re-assemble the regex
                set search_string to s1 & s2 & s3 & s4
                
                set counter to 0
                set done to false
                repeat until (done)
                        set counter to counter + 1
                        set replace_string to "\\01" & "\\02" & counter & "\\4" 
-- skip the  
third argument and replace it with the counter
                        if (found of (find search_string searching in text 1 of 
text  
document 1 options findCmdOptions with selecting match)) then
                                set selection of window 1 to (grep substitution 
of replace_string)
                        else
                                set done to true
                        end if
                end repeat
        end tell
        
        -- WHILE WE ARE AT IT, LET US REMOVE ALL TRAILING SPACES
        set scope to 1 -- "scope == 1" means this document only, "anything  
else" means all open documents
        set search_string to "([ \\t]+$)"
        set replace_string to ""
        set view_results to false
        my bb_replace_string(search_string, replace_string, scope,  
view_results)
        
        bh_dialog_show_msg("", 0)
end run

-- +---------+---------+---------+---------+---------+--------- 
+---------+---------+
on bb_replace_string(search_str, replace_str, scope, view_results)
        tell application "BBEdit"
                set replaceCmdOptions_01 to {search mode:grep, case 
sensitive:true,  
match words:false, extend selection:false, showing results:false}
                set replaceCmdOptions_02 to {search mode:grep, case 
sensitive:true,  
match words:false, extend selection:false, showing results:true}
                
                if (scope is equal to 1) then
                        replace search_str using replace_str saving yes 
searching in {text  
document 1} options replaceCmdOptions_01
                else
                        replace search_str using replace_str saving yes 
searching in {every  
text document} options replaceCmdOptions_02
                        if (not (view_results)) then
                                close search results browser 1 saving no
                        end if
                end if
        end tell
end bb_replace_string
-- +---------+---------+---------+---------+---------+--------- 
+---------+---------+
on bh_dialog_show_msg(the_msg, the_time)
        try
                set local_time_out to the_time
                if (the_time < 1) then
                        set local_time_out to 2
                else if (the_time > 30) then
                        set local_time_out to 2
                end if
        on error error_message number error_number
                set local_time_out to 2
        end try
        if (the_msg = "") then
                set local_str to "All done..."
        else
                set local_str to the_msg
        end if
        tell application (path to frontmost application as text)
                set b1 to "OK"
                display dialog local_str buttons {b1} default button {b1} 
giving up  
after local_time_out with icon 1
        end tell
end bh_dialog_show_msg
-- +---------+---------+---------+---------+---------+--------- 
+---------+---------+
on bb_goto(cursor_position)
        tell application "BBEdit"
                activate
                set w_ref to text window 1
                tell w_ref
                        if ((cursor_position = "b") or (cursor_position = 
"beginning")) then
                                -- set cursor_position to "beginning of the 
document"
                                select insertion point before character 1 of 
text 1 of text  
document 1
                        else if ((cursor_position = "e") or (cursor_position = 
"end")) then
                                -- set cursor_position to "end of the document"
                                select insertion point after the last character 
of text 1 of text  
document 1
                        else
                                -- DO NOTHING WITH THE CURSOR
                        end if
                end tell
        end tell
end bb_goto
-- +---------+---------+---------+---------+---------+--------- 
+---------+---------+
----------------------------------------
[2401] ( _END_ ) renumber_items.scpt
----------------------------------------

BEFORE RUNNING THE SCRIPT
----------------------------------------
[2402] ( BEGIN ) original data
----------------------------------------
        <ObjectData>
                <Type>Fields</Type>
                <Object no="9">
                        <Title>Campus</Title>
                        <Field>SchoolID</Field>
                </Object>
                <Object no="2">
                        <Title>Zip Code</Title>
                        <Field>Zip</Field>
                </Object>
                <Object no="53">
                        <Title>Ethnicity</Title>
                        <Field>Ethnicity</Field>
                </Object>
                <Object no="473">
                        <Title>Grade</Title>
                        <Field>Grade_Level</Field>
                </Object>
                <Object no="89">
                        <Title>Name</Title>
                        <Field>LastFirst</Field>
                </Object>
                <Object no="6">
                        <Title>Student Number</Title>
                        <Field>Student_Number</Field>
                </Object>
        </ObjectData>
----------------------------------------
[2402] ( _END_ ) original data
----------------------------------------

AFTER RUNNING THE SCRIPT
----------------------------------------
[2403] ( BEGIN ) re-numbered data
----------------------------------------
        <ObjectData>
                <Type>Fields</Type>
                <Object no="1">
                        <Title>Campus</Title>
                        <Field>SchoolID</Field>
                </Object>
                <Object no="2">
                        <Title>Zip Code</Title>
                        <Field>Zip</Field>
                </Object>
                <Object no="3">
                        <Title>Ethnicity</Title>
                        <Field>Ethnicity</Field>
                </Object>
                <Object no="4">
                        <Title>Grade</Title>
                        <Field>Grade_Level</Field>
                </Object>
                <Object no="5">
                        <Title>Name</Title>
                        <Field>LastFirst</Field>
                </Object>
                <Object no="6">
                        <Title>Student Number</Title>
                        <Field>Student_Number</Field>
                </Object>
        </ObjectData>
----------------------------------------
[2403] ( _END_ ) re-numbered data
----------------------------------------



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "BBEdit Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a specific feature request or would like to report a suspected (or 
confirmed) problem with the software, please email to "[email protected]" 
rather than posting to the group.
-~----------~----~----~----~------~----~------~--~---

Reply via email to