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
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
Posted on Jan 27, 2015 1:07 PM