t quinn

Q: Using Applescript to replace negative indent in HTML

I am playing with a script to take HTML from this forum window and remove the negative indent that this page adds when you paste an applescript copied from the editor into it. I have sumitted feedback to apple and if anyone cares I encourage them to do so.

 

What I want to do:

Starting with the focus in this window,

click the HTML button

find/replace "text-indent: -" with "text-indent: "

paste back into this window

click the "show full editor" button

 

FYI- I do believe that applescript is probalbly not the best approach for this task but my true mission is to learn more about scripting and a small detour to a shell script hasn't seemed too far.

 

So far I have:

 

tell application "Safari"

  activate application "Safari"

  tell application "System Events" to keystroke "a" using command down

  delay 0.15

  tell application "System Events" to keystroke "c" using command down

end tell

 

activate application "TextEdit"

tell application "TextEdit"

  make document

  set text of front document to the clipboard

  set fdText to "text-indent: -"

  set rplText to "text-indent: "

 

 

  activate application "TextEdit"

  set the clipboard to fdText

  tell application "System Events" to keystroke "f" using command down

  tell application "System Events" to keystroke "v" using command down

  delay 0.15

  set the clipboard to rplText

  tell application "System Events" to keystroke "f" using [command down, option down]

  tell application "System Events" to keystroke "v" using command down

  delay 0.15

  activate application "TextEdit"

  tell application "System Events" to keystroke tab

  delay 0.15

  tell application "System Events" to keystroke space

  delay 0.15

  set sr to front document's text

  set the clipboard to sr

end tell

 

activate application "Safari"

tell application "Safari"

  tell application "System Events" to keystroke "v" using command down

end tell

 

This works for me as you see above but it is a bit of a kludge. Drew suggested in this thread here that I could do my find replace using a shell script and I am trying to implement that now.

 

 

set fdText to "text-indent: "

set rplText to "text-indent: "

set inputstring to the clipboard

 

 

-- use 'sed' to replace the '-' after the text-indent:

set cmd to "echo \"" & inputstring & "\" | sed -e 's/text-indent: /text-indent: /g' "

set outputstring to (do shell script cmd)

 

--see what we got

display dialog "inputstring: " & return & inputstring & return & return & "outputstring:" & return & outputstring


This script is stumbling when the clipboard has text with quotes in it. I have been copying this window after I click the HTML button.

My questions:

How do I get this shell script to work with the quotes?

How do I find and click the HTML button above and the "show full editor" button in the other window?


Thanks,

quinn

MacBook Pro, OS X Mavericks (10.9.5)

Posted on Jan 25, 2015 2:09 PM

Close

Q: Using Applescript to replace negative indent in HTML

  • All replies
  • Helpful answers

Previous Page 2
  • by Hiroto,Solvedanswer

    Hiroto Hiroto Jan 27, 2015 1:07 PM in response to Hiroto
    Level 5 (7,276 points)
    Jan 27, 2015 1:07 PM in response to Hiroto

    Oops. I tested the script with wrong data. I should have paid closer attention to the html code given in HTML mode so that I could notice the literal tabs with style="white-space:pre;" are replaced with double spaces in HTML editor.

     

    The previous code assumed literal tabs are being preserved and thus won't work as expected without them. Please replace the _edit_html() handler with the following one.

     

     

     

    on _edit_html(t)
        (*
            string t : html source code
            return string : edited code
        *)
        do shell script "perl -CSDA -w <<'EOF'
    use strict;
    while (<DATA>) {
        s/ ( <p [^>]*? ) ( margin: [^;]+; ) /$1margin: 0px;/ogx;            # set margin to 0px in (style of) p element
        s/ ( <p [^>]*? ) ( text-indent: \\s* ) - ( [^;]+; ) /$1$2$3/ogx;    # remove - from text-indent in (style of) p element
        print;
    }
    __END__
    " & t & "
    EOF"
    end _edit_html
    

     

     

     

    The whole script would be as follows.

     

     

    _main()
    on _main()
        set _delay to 0.3 -- adjust this as is needed
        tell application "Safari"
            activate
            my _toggle_html_mode(window 1's current tab) -- to html mode
        end tell
        delay _delay
        tell application "System Events" to keystroke "ac" using {command down} -- select all & copy
        delay _delay
        set the clipboard to _edit_html(the clipboard as text) -- edit html
        delay _delay
        tell application "System Events" to keystroke "v" using {command down} -- paste
        delay _delay
        tell application "System Events" to keystroke tab -- [1]
        delay _delay
        tell application "Safari"
            my _toggle_html_mode(window 1's current tab) -- to full editor mode
        end tell
        (*
            [1] required to remove focus from textarea so as to let focus be in textarea when html mode is entered next time
        *)
    end _main
    
    on _toggle_html_mode(doc)
        (*
            reference doc : reference of Safari document, tab or window
        *)
        tell application "Safari"
            tell doc
                do JavaScript "// click HTML toggle button
    function main()
    {
        var el =
            document.getElementById('wysiwygtext_html')  || // HTML toggle button in advanced editor
            document.getElementById('wysiwygtext1_html') || // HTML toggle button in normal editor
            document.getElementById('wysiwygtext2_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext3_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext4_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext5_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext6_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext7_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext8_html') || // HTML toggle button in normal editor (alternative id)
            document.getElementById('wysiwygtext9_html');   // HTML toggle button in normal editor (alternative id)
        if (!el) { return null; }
        return clickElement(el);
    }
    function clickElement(el)
    {
        var e = document.createEvent('MouseEvent');
        e.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        return el.dispatchEvent(e);
    }
    main();" in it
            end tell
        end tell
    end _toggle_html_mode
    
    on _edit_html(t)
        (*
            string t : html source code
            return string : edited code
        *)
        do shell script "perl -CSDA -w <<'EOF'
    use strict;
    while (<DATA>) {
        s/ ( <p [^>]*? ) ( margin: [^;]+; ) /$1margin: 0px;/ogx;            # set margin to 0px in (style of) p element
        s/ ( <p [^>]*? ) ( text-indent: \\s* ) - ( [^;]+; ) /$1$2$3/ogx;    # remove - from text-indent in (style of) p element
        print;
    }
    __END__
    " & t & "
    EOF"
    end _edit_html
    

     

     

     

    Sorry for confusions.

    Hiroto

  • by Hiroto,

    Hiroto Hiroto Jan 27, 2015 1:27 PM in response to VikingOSX
    Level 5 (7,276 points)
    Jan 27, 2015 1:27 PM in response to VikingOSX

    Hello VikingOSX,

     

    Thank you for your testing and comments. I have overlooked pre-formatted tabs are being replaced with bare spaces in html source in HTML editor. Have posted a correction.

     

    Indeed I have used the following rubycocoa code to experiment with rtf and corresponding html. (To use it, copy rtf text in clipborad, run the script and it will print the html code representing the rich text.)

     

    All the best,

    Hiroto

     

     

    #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
    
    require 'osx/cocoa'
    include OSX
    
    pboard = NSPasteboard.generalPasteboard
    pbtype = pboard.availableTypeFromArray(['public.rtf'])
    exit if pbtype == nil
    rtf = pboard.dataForType(pbtype)
    
    docattr = OCObject.new
    as = NSAttributedString.alloc.objc_send(
        :initWithRTF, rtf,
        :documentAttributes, docattr)
    
    docattr = {
        NSDocumentTypeDocumentAttribute      => NSHTMLTextDocumentType,
        NSPrefixSpacesDocumentAttribute      => 4,
    #   NSExcludedElementsDocumentAttribute  => ['style'],
        
    }
    err = OCObject.new
    html = as.objc_send(
        :dataFromRange, NSMakeRange(0, as.length),
        :documentAttributes, docattr,
        :error, err)
    
    $stderr.puts "failed to create html: %s" % err.description.to_s unless html
    
    s = NSString.alloc.objc_send(
        :initWithData, html,
        :encoding, NSUTF8StringEncoding)
    puts s
    
  • by Drew Reece,

    Drew Reece Drew Reece Jan 27, 2015 5:31 PM in response to Hiroto
    Level 5 (7,480 points)
    Notebooks
    Jan 27, 2015 5:31 PM in response to Hiroto

    Anyone want to try a bookmarklet?

    Make a new bookmark in your favourites bar (or duplicate one and use the following for the link value)…

     

    javascript:function()%7Bvar%20mceFrame%20%3D%20document.getElementById('wysiwygt ext1_ifr')%3Bvar%20content%20%3D%20mceFrame.contentWindow.document.body.innerHTM L%3Bvar%20newcontent%20%3D%20content.replace(%2Ftext-indent%3A%20-%2Fg%2C%20'tex t-indent%3A%20')%3BmceFrame.contentWindow.document.body.innerHTML%20%3D%20newcon tent%7D)()

     

    Paste in the badly indented Applescript into the forum, click the bookmarklet, does it work?

     

    I dare say this is hideous code, so here is the version without the URI encoding…

    var mceFrame = document.getElementById('wysiwygtext1_ifr');
    var content = mceFrame.contentWindow.document.body.innerHTML;
    var newcontent = content.replace(/text-indent: -/g, 'text-indent: ');
    mceFrame.contentWindow.document.body.innerHTML = newcontent;
    

     

    I used this page to URI encode.

    This is your fault Hiroto, once I saw a bit of JS I had to fiddle

    Apologies if this is stepping over your work quinn.

    Feel free to cleanse the indents in better ways.

     

    NOTE: I have only tried it in Safari 7 on 10.9 using the standard editor.

  • by t quinn,

    t quinn t quinn Jan 27, 2015 9:38 PM in response to Drew Reece
    Level 5 (4,980 points)
    Mac OS X
    Jan 27, 2015 9:38 PM in response to Drew Reece

    Hi Drew,

     

    You guys go for it. You are leaving me in the dust but I am sure I will be learning something- many things.

     

    quinn

  • by t quinn,

    t quinn t quinn Feb 11, 2015 6:42 PM in response to Drew Reece
    Level 5 (4,980 points)
    Mac OS X
    Feb 11, 2015 6:42 PM in response to Drew Reece

    Hi Drew,

     

    I was trying to get your bookmarklet to work for me but I must be doing something wrong. I was pasting the javascript:function()... into a bookmark as the address. At one point it was opening a blank page but now- nothing happens. It would be sweet if a little bit of code would do the trick. (Of course I have already unchecked the preference as Hiroto suggested.)

     

    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Feb 11, 2015 7:17 PM in response to t quinn
    Level 5 (7,480 points)
    Notebooks
    Feb 11, 2015 7:17 PM in response to t quinn

    Lets see if this works?

    De-indent

    It looks like the forum split the long line, which may explain why it is failing.

     

    edit: Nope the forum wont let me post the js in a link.

    I'll find a better option & post it somewhere that allows bookmarklets on the page tomorrow, once you can drag it makes life easier.

     

     

    This is the full 'address'… lets see if the forum implodes it again (it is all one line, no spaces).

    javascript:(function()%7Bvar%20mceFrame%20%3D%20document.getElementById('wysiwyg text1_ifr')%3Bvar%20content%20%3D%20mceFrame.contentWindow.document.body.innerHT ML%3Bvar%20newcontent%20%3D%20content.replace(%2Ftext-indent%3A%20-%2Fg%2C%20'te xt-indent%3A%20')%3BmceFrame.contentWindow.document.body.innerHTML%20%3D%20newco ntent%7D)()

  • by Drew Reece,Helpful

    Drew Reece Drew Reece Feb 11, 2015 9:18 PM in response to Drew Reece
    Level 5 (7,480 points)
    Notebooks
    Feb 11, 2015 9:18 PM in response to Drew Reece

    Try this …

    http://marklets.com/De-indent.aspx

     

    Drag the bookmark to the bookmarks bar (or into a window with the bookmarks open).

  • by t quinn,

    t quinn t quinn Feb 12, 2015 8:13 AM in response to Drew Reece
    Level 5 (4,980 points)
    Mac OS X
    Feb 12, 2015 8:13 AM in response to Drew Reece

    Hi Drew,

     

    That is sweet, effortless. This is the solution I would recommend to people that just want a fix.

     

    quinn

  • by Drew Reece,

    Drew Reece Drew Reece Feb 12, 2015 10:40 AM in response to t quinn
    Level 5 (7,480 points)
    Notebooks
    Feb 12, 2015 10:40 AM in response to t quinn

    thank you.

     

    It's why I was ranting about making the bookmarklet in the other thread.

    I love Applescript, but at times it feels like you are making things from string, duct tape & bits of your own body

    You should continue with AS if you like learning, Dougscripts is a good source of ones that can be modified. 

Previous Page 2