Getting error 1728 when trying to run this script

Am trying to figure out why the following AppleScript program fails - get an error when it finds a matching notificationcenter dialog with the name of “Disk Not Ejected Properly” and tries to simulate clicking the Close button. That is when i get the 1728 error. Am rather a newbie so am sure I’m doing something wrong, but not able to see what the problem is, so welcome any ideas/suggestions. Thanks…

use AppleScript version “2.4” – Yosemite (10.10) or later
use scripting additions

tell application “System Events”
tell process “NotificationCenter”
set numwins to (count windows)
repeat with i from 1 to numwins by 1
tell window i
set rawElements to UI elements
if name of item 1 of result = “Disk Not Ejected Properly” then
click button “Close” of window i
end if
end tell
end repeat
end tell
end tell

Here is the image of the 1728 error dialog when run from ScriptDebugger program:

The issue is the click button command is already in a window i tell block, and you’re script is confusing things by specifying window i again.

Replace that line with:

click button "Close"

Also, here’s a tip for the forum: When you’re quoting a script precede it with three ` characters (to the left of 1 on most keyboards) and the word AppleScript

and at the end of your script do three ` characters again.

``` applescript
–your applescript here
```

That way you get nicely formatted script that can be easily opened in a script editor:

tell application "System Events"
	tell process "NotificationCenter"
		set numwins to (count windows)
		repeat with i from 1 to numwins by 1
			tell window i
				set rawElements to UI elements
				if name of item 1 of result = "Disk Not Ejected Properly" then
					click button "Close"
				end if
			end tell
		end repeat
	end tell
end tell

Great – thanks so much. Really appreciate the tip and the help!!!

Am gonna give the new version 8 of Script Debugger a try as soon as I have a Mojave (or newer) system to give it a try on.

Script Debugger has been around a long time and look forward to many more years of having fun with it and learning new stuff. Thanks for all the work you guys do to support AppleScript!!!

-bob

1 Like

There is a logic error in my program - was closing the NotificationCenter windows in window order (from first to last) but need to do it in reverse window order (from last to first), as got an error about a nonexistent window. Hope this makes sense – here is the new program in case anyone is interested (with the triple quote tip from Ed):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- 2022-11-10 - Version 1.0 - modified slightly from original source code
-- 2022-11-11 - Version 1.3 - need to close in reverse order as get an error otherwise

tell application "System Events"
	tell process "NotificationCenter"
		set numwins to (count windows)
		-- have to do in reverse order because
		--    window numbers change dynamically
		--    as they are deleted if done in
		--    in normal order.
		repeat with i from numwins to 1 by -1
			tell window i
				set rawElements to UI elements result
				if name of item 1 of result = "Disk Not Ejected Properly" then
					click button "Close"
				end if
			end tell
		end repeat
	end tell
end tell

One other question I have - is there a way to set the “version” of an AppleScript program without manually editing the Info.plist file located in the xxx.app/Contents/Resources folder, ie directly in the AppleScript code itself??? By this I mean when you click on the “About” menu item when the program is running and up pops a dialog box that says what the program is and what version and other similar info?

Thanks again for the help…

-bob

Hi Folks … have another issue with this program that just happened … sometimes, but not all the time, when the ‘click button “Close”’ call is executed in the script, more than one of the Notifications are closed - any idea how that would happen? Have seen two or three or four of them disappear at once - happens very quickly so it’s hard to count how many for sure. So if this does happen, the program aborts with an error before completely closing all of them. All these are “Disk not properly ejected” messages that have been happening to some of the USB drives mounted on a High Sierra system, but not all of them oddly.

Any idea how to fix the AppleScript program when more than one window disappears when the single Close is executed? I tried to do another set of tell/tell/set/end/end inside the loop but the problem is that modifying the “i” variable that auto decrements in the “set numwins to (count windows)” loop is not affected when I try and change value of i inside the loop like this:

repeat with i from numwins to 1 by -1
	tell window i
		set rawElements to UI elements
		if name of item 1 of result = "Disk Not Ejected Properly" then
			click button "Close"
			tell application "System Events"
				tell process "NotificationCenter"
					set numwins2 to (count windows)
				end tell
			end tell
			i = numwins2
		end if
	end tell
end repeat

But the value of “i” variable in the repeat loop is not always affected.

Appreciate any suggestions or explanations on how more than one of the “disk not ejected properly” notifications can close on one AppleScript ‘click button “Close”’ call?? Thanks very much…

-bob

ps - the trick with the ‘’’ does not seem to be working the same - what is the best way to display AppleScript code in the forum now? Also note that the first line of code is not indented correctly - how to fix that? As the code appears in my Safari browser window the second line beginning with “tell” is to the left of the first “repeat” but that’s not how I typed it in??? Thanks again…