The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Looking for a "add characters" script (http://hintsforums.macworld.com/showthread.php?t=102529)

DaleMox 06-15-2009 06:10 PM

%s/^\(.*\)\n\1$/<location>\1<\/location>\r<title>\1<\/title>/g

cwtnospam 06-15-2009 07:20 PM

Scary!

Code:

tell application "TextWrangler"
        activate
        set x to text of window 1
        set mytext to ""
        set y to every paragraph of x
        repeat with eachline in y
                set z to eachline as text
                set mytext to mytext & "<location>" & z & "</location>" & return & "<title>" & z & "</title>" & return
        end repeat
        set text of window 1 to mytext
end tell


paldo 06-17-2009 09:48 AM

Thank you very much
 
Great job, thank you. I wish I can write applescripts.

cwtnospam 06-17-2009 10:35 AM

It's easy! Just do one step at a time until you're done. Here it is with comments:
Code:

tell application "TextWrangler"  -- Tells Applescript that you want to instruct TextWrangler what to do
        activate  -- Tells TextWrangler to be the active application, as if you'd clicked on its window.
        set x to text of window 1  -- Sets a variable "x" to be all of the text in the active window
        set mytext to "" -- Initializes mytext to be empty
        set y to every paragraph of x -- Sets variable "y" to be a list of the paragraphs in "x" (paragraphs are separated by returns)
        repeat with eachline in y  -- Sets up a loop where "eachline" takes on each successive value in "y"
                set mytext to mytext & "<location>" & eachline & "</location>" & return & "<title>" & eachline & "</title>" & return
                -- Adds to "mytext" the location and title tags around "eachline" and separates them with carriage return characters
        end repeat -- End of loop is reached when "eachline" has been every item in the list "y"
        set text of window 1 to mytext -- Tells TextWrangler to replace the text in its current window with the text in "mytext"
end tell -- Done with TextWrangler

It's slightly different code because I saw that I was being redundant. See if you can spot where!


All times are GMT -5. The time now is 11:07 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.