Property to monitor if "Finder" has changed

Hi all,

I am keeping track of all Finder windows opened by using a
an applet and within the idle cycle, I run:

tell application "Finder"
	set windowList to every Finder window
	set windewListAsText to ""
	repeat with thisWindow in windowList
		try
			set thePath to thisWindow's target as string
			set theSize to (size of (folder thePath)) / (1024 ^ 2) as integer
			-- set newSize to theSize / (1024 ^ 2) as integer
		on error
			set theSize to "0"
		end try
		try
			set windewListAsText to windewListAsText & thisWindow's id & tab & thisWindow's index & tab & thisWindow's name & tab & thisWindow's target & tab & ((theSize as string) & " MB") & linefeed
		on error
			set windewListAsText to windewListAsText & thisWindow's id & tab & thisWindow's index & tab & thisWindow's name & tab & "No path found" & tab & "0 MB" & linefeed
		end try
	end repeat
	return windewListAsText
end tell

Is there any way to skip the cycle if the “Finder” has NOT changed.
I mean: I wonder if there is any property that I can interrogate to check whether it is worth to run the code above:
Something like:

If FinderHasChanged then
   --> run the above code
else
   return -- skipping idle 
end if

Thanks !

I hope I understood you correctly. It is not possible to skip the execution of the on idle() handler, so you would probably want to skip some actions in this handler (do nothing).

I’m not sure, but I don’t think the Finder updates the folder’s size immediately, so the script might not work properly. In any case, I would start like this:


property windowsInfo : ""

on run {}
	set windowsInfo to my getFinderWindowsInfo()
end run

on idle
	set newWindowsInfo to my getFinderWindowsInfo()
	if newWindowsInfo = windowsInfo then
		-- do nothing
	else
		-- do something. For example:
		tell me to activate
		tell me to display dialog "Some of the Finder window still is updated" giving up after 1
		set windowsInfo to newWindowsInfo
	end if
	return 1
end idle

on getFinderWindowsInfo()
	tell application "Finder"
		set windowList to Finder windows
		if windowList is not {} then reopen it
		set windewListAsText to ""
		repeat with thisWindow in windowList
			try
				set theSize to (size of (thisWindow's target)) / (1024 ^ 2) as integer
			on error
				set theSize to "0"
			end try
			try
				set windewListAsText to windewListAsText & thisWindow's id & tab & thisWindow's index & tab & thisWindow's name & tab & thisWindow's target & tab & ((theSize as string) & " MB") & linefeed
			on error
				set windewListAsText to windewListAsText & thisWindow's id & tab & thisWindow's index & tab & thisWindow's name & tab & "No path found" & tab & "0 MB" & linefeed
			end try
		end repeat
		return windewListAsText
	end tell
end getFinderWindowsInfo

.
NOTE: The script in this form will not notice changes if the folder size is changed by < 1 MB. To notice the slightest changes, do not convert Bytes to MB.

You can also use the PFAssistive framework from the PFiddlesoft website (https://pfiddlesoft.com/frameworks/index.html) and register an observer.
Unfortunately, the website is only available until October 17. :frowning:


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

set observer to current application's PFObserver's observerWithBundleIdentifier:"com.apple.finder"
observer's setDelegate:me
observer's registerForNotification:(current application's NSAccessibilityMainWindowChangedNotification) fromElement:(missing value) contextInfo:(missing value)

on applicationWithIdentifier:theIdentifier atPath:theFullPath didPostAccessibilityNotification:theNotification fromObservedUIElement:theObservedUIElement forAffectedUIElement:theAffectedUIElement
	display notification (theAffectedUIElement's AXTitle as text)
end applicationWithIdentifier:atPath:didPostAccessibilityNotification:fromObservedUIElement:forAffectedUIElement:

-- delay 60 -- for testing in the Script Editor

Note: The observer is only active as long as the script is running. Therefore the delay at the end of the script for testing in the Script Editor. For the runtime it’s better to use a Stay-Open Applet.

Many thanks to Bill Cheeseman for his great work - Especially the UI Browser!