Remove the HTML elements in the HTML file using attributes

Hi All,

Hope you all are doing good!

I want to remove the HTML elements find globally to match with attributes names in the HTML file at backend. Is it possible via applescript?

For Example

HTML element

<div><a href="#" class="link-wrapper"><span class="icon-1"><span>test</span></span></a></div>

I want to find the Html element with “link-wrapper” attribute globally and remove the

parent element.

Is it possible to do the process in backend without open the file in editor?

You can use NSXML
Create a NSXMLDocument from the HTML
the use XPath to find the div nodes whose a node class is “file-wrapper”

//div/a[@class="link-wrapper"]/..

Then remove those nodes from
The NSXMLDocument and save it

The HTML file is plain text. So, you can replace its contents like any other plain text:

  1. Read the text from HTML file to variable theText
  2. Get list of paragraphs of theText, which contains “class="link-wrapper"><span class=” and begins with “
    ” and ends with “
  3. Repeat loop thru every paragraph of this list
  4. Set current loop’s paragraph of theText to text 6 thru -7 of current loop’s paragraph of theText
  5. Save edited theText back to HTML file.

Try this.
If you want to use a weblink change the on run line to:

createXMLDocFromLink:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions


-- classes, constants, and enums used
property NSXMLNodeCompactEmptyElement : a reference to 4
property NSXMLAttributeKind : a reference to 3
property NSXMLNodePrettyPrint : a reference to 131072
property NSXMLDocumentTidyHTML : a reference to 512
property NSXMLDocument : a reference to current application's NSXMLDocument
property NSXMLElement : a reference to current application's NSXMLElement
property |NSURL| : a reference to current application's |NSURL|
property NSUTF8StringEncoding : a reference to 4
property NSString : a reference to current application's NSString

property theXMLDoc : missing value
property theXMLCleanDoc : missing value
property theXMLCleanString : ""

property theLink : ""
property theFilePath : ""
property theSavePath : ""
property theXpath : ""


on run {}
	set theLink to "https://macscripter.net/viewtopic.php?pid=209008#p209008"
	set theFilePath to "/Users/kerry/Desktop/test.html"
	set theSavePath to "/Users/kerry/Desktop/testCleaned.html"
	set theXpath to "//div/a[@class='link-wrapper']/.."
	--my createXMLDocFromLink:theLink
	my createXMLDocFromFile:theFilePath
	my findAndRemoveNodes()
	my makeCleanString()
	my makeCleanDoc()
	my saveCleanString()
end run

-- CREATE XML DOC

-- CONVERT WEBLINK TO NSURL AND MAKE XMLDOC

on createXMLDocFromLink:aLink
	set aString to NSString's stringWithString:aLink
	set aURL to |NSURL|'s URLWithString:aString -- make NSURL
	my createXMLDocFromURL:aURL
end createXMLDocFromLink:

-- CONVERT FILEPATH TO NSURL AND MAKE XMLDOC

on createXMLDocFromFile:thePath
	set aURL to |NSURL|'s fileURLWithPath:thePath -- make NSURL
	my createXMLDocFromURL:aURL
end createXMLDocFromFile:

-- CREATE XML DOC FROM NSURL

on createXMLDocFromURL:aURL
	set {theXMLDoc, theError} to NSXMLDocument's alloc()'s initWithContentsOfURL:aURL options:516 |error|:(reference) -- make XMLDoc
	if theXMLDoc is missing value then error (theError's localizedDescription()) as text
end createXMLDocFromURL:


-- FIND NODES VIA XPATH AND DETACH

on findAndRemoveNodes()
	set foundNodes to my findElementsInNode:theXMLDoc usingXPath:theXpath
	repeat with aNode in foundNodes
		aNode's detach()
	end repeat
end findAndRemoveNodes

-- CONVERT THE XML DOC TO A STRING

on makeCleanString()
	set aString to (theXMLDoc's XMLStringWithOptions:NSXMLNodePrettyPrint) as text
	set theXMLCleanString to NSString's stringWithString:aString
end makeCleanString

-- NOT REALLY NEEDED BUT EXAMPLE OF CREATING XML DOC FROM STRING

on makeCleanDoc()
	set {theXMLCleanDoc, theError} to NSXMLDocument's alloc()'s initWithXMLString:theXMLCleanString options:0 |error|:(reference) -- make XMLDoc
	if theXMLCleanDoc is missing value then error (theError's localizedDescription()) as text
end makeCleanDoc

-- SAVE THE CLEANED STRING TO HTML FILE

on saveCleanString()
	--set aURL to |NSURL|'s fileURLWithPath:theSavePath -- make NSURL
	set {theResult, theError} to theXMLCleanString's writeToFile:theSavePath atomically:true encoding:NSUTF8StringEncoding |error|:(reference)
	if theResult as boolean is false then error (theError's localizedDescription() as text)
end saveCleanString

-- FIND ELEMENTS FROM XPATH

on findElementsInNode:aNode usingXPath:aXPath
	set aString to NSString's stringWithString:aXPath
	set {foundNodes, xpathError} to (aNode's nodesForXPath:aString |error|:(reference))
	if (not xpathError is missing value) then
		error (theError's localizedDescription()) as text
		return missing value
	end if
	return foundNodes
end findElementsInNode:usingXPath:


I did a test by taking this web page and randomly sticking some of these in being sure to change some of inner span properties IE icon-2 and test2.


<div><a href="#" class="link-wrapper"><span class="icon-1"><span>test</span></span></a></div>
 <div><a href="#" class="link-wrapper"><span class="icon-2"><span>test2</span></span></a></div>