Remove Excess Text from a Variable

image

When using the above script in a macro it often returns a large amount of text. Is there any way to keep the first 10 or so lines of text in a variable and discard the rest?

Currently I'm writing a temp file to my home directory and using a sed command. It works but I'm just wondering if there's a better way?

tell application "Safari"
	tell front document
		return paragraphs 1 thru 10 of (get source)
	end tell
end tell

That'll normally just be metadata and maybe some scripting stuff though -- and, of course, will depend on how the raw HTML is laid out (if someone's written it without line breaks you're in for a surprise!). Are you after something in particular?

1 Like

Well I have a macro for subscribing to various RSS feeds. Normally I use the Switch/Case action based on the URL to proceed but needed a way to determine if the site is a Discourse forum or not. This works a treat by just grabbing the metadata at the beginning of the page without saving the entire source. Thank you @Nige_S!

Footnote – for more focused extractions from HTML, the XQuery 1.0 syntax is simple and powerful.

(you can use it from AppleScript and JavaScript for Automation as well as from browser JavaScript)


XQuery Examples - Wikipedia

1 Like