Optimize Applet's energy consumption

Hi :slight_smile:

First, I love AppleScript and i often gathered scripts from here in the past, so i wanna say thank you :smiley:

Then, what brought me here to register today is the lack of knowledge: I have an Applet running all time on my mac and i would like to make sure it is as light as possible for obvious reasons, but i’m using code i don’t really understand… code i gathered from here or there…

It’s basically a kind of RSS news parser, except that it’s html based (with CURL): it just shows me a notification + a badge in the dock.

Here is the code:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
property checkList : {}
property rsrcFolder : ""
property counter : 0
property |@| : a reference to my NSWorkspace's ¬
	sharedWorkspace's notificationCenter

property site1 : {title:"SuperSite", URL:"supersite.dot", cookie:true, lastid:"", extract:{{fromHTML:"list_item_title", toHTML:"</a>", type:"continue"}, {fromHTML:"<span>", toHTML:"</span>", type:"label"}, {fromHTML:"href=\"", toHTML:"\"", type:"continue"}, {fromHTML:".", toHTML:".html", type:"id"}}}
property site2 : {title:"AwesomeSite", URL:"awesomesite.dot", cookie:true, lastid:"", extract:{{fromHTML:"class=\"row4 med tLeft\"", toHTML:"</a>", type:"continue"}, {fromHTML:"<b>", toHTML:"(", type:"label"}, {fromHTML:"?t=", toHTML:"\"", type:"id"}}}

on run
	_init() -- SET OBSERVER TO BE ABLE TO DETECT ON ACTIVATE
	
	-- INITIAL VALUES
	set rsrcFolder to (path to me as text) & "Contents:Resources"
	set counter to 0
	
	-- FILL THE LIST OF SITE TO CHECK
	copy site1 to the end of checkList
	copy site2 to the end of checkList
end run

on idle
	repeat with site in checkList
		try
			-- COOKIE
			if cookie of site then
				set cookie to " -b '" & POSIX path of rsrcFolder & "/" & title of site & ".txt'" as string
			else
				set cookie to ""
			end if
			
			-- CURL
			set htmlcode to do shell script "curl -L" & cookie & " '" & URL of site & "' -m 20"
			
			-- PARSING
			repeat with split in extract of site
				if type of split is equal to "continue" then
					set htmlcode to extractedText of htmlcode from (fromHTML of split) to (toHTML of split)
				else if type of split is equal to "label" then
					set label to extractedText of htmlcode from (fromHTML of split) to (toHTML of split)
				else if type of split is equal to "id" then
					set lastid_new to extractedText of htmlcode from (fromHTML of split) to (toHTML of split)
				end if
			end repeat
			
			-- NOTIFY
			if lastid_new is not equal to (lastid of site) and (lastid of site) is not equal to "" then
				display notification label with title (title of site) sound name "Glass"
				set counter to counter + 1
				(my setDockBadgeString(counter))
				set lastid of site to lastid_new
			else if (lastid of site) is equal to "" then
				display notification label with title (title of site)
				set lastid of site to lastid_new
			end if
			
		end try
	end repeat
	
	--TIMER
	set X to ((random number from 50 to 60) * (random number from 5 to 10))
	return X
end idle


on activate
	--TAKE OFF BADGE
	set counter to 0
	(my setDockBadgeString(""))
end activate

on quit
	--TAKE OFF OBSERVER
	global |@|
	|@|'s removeObserver:me
	continue quit
end quit

on _init()
	--ADD OBSERVER
	"NSWorkspaceDidActivateApplicationNotification"
	|@|'s addObserver:me selector:("_notify:") ¬
		|name|:result object:(missing value)
	idle
end _init

to _notify:notification
	local notification
	if my id = the notification's userInfo()'s ¬
		NSWorkspaceApplicationKey's ¬
		bundleIdentifier() as text ¬
		then activate me
end _notify:


--- GLOBAL FUNCTIONS
on setDockBadgeString(dockBadgeString)
	set dockBadgeString to dockBadgeString as text
	set appDockTile to current application's NSApp's dockTile()
	appDockTile's setBadgeLabel:dockBadgeString
end setDockBadgeString

on extractedText of htmlcode from aString to bString
	set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {aString}}
	return text 1 thru ((offset of bString in (text item 2 of htmlcode)) - 1) of (text item 2 of htmlcode)
end extractedText

I really welcome any advice/suggestions.

My main concern is the use of Obj-C functions, for the badge and for triggering “on activate” when i click on the Applet’s icon in the dock (to clear the badge), but as i do not know Obj-C, and i do not know how good/bad are any of those functions energy-wise and i think maybe it would be just better to forget the obj-c observer and clear the badge with a on quit dialog option…

Please feel free to enlighten me, and again, thanks a lot for all the scripts here, it’s a gold mine <3

You’re worrying needlessly. You can confirm it by checking in Activity Monitor.app.

I did check the activity monitor, and it’s very low indeed, probably the lowest on my system.
I was just thinking maybe there would be easy improvements to make, but i trust you, thank you :slight_smile: