Script Error Issue Question

I have combined two scripts that both work when run separately. But when combined I get an error (see attachment).

screenshot_174

This is the script:

tell application "System Events" to tell application process "Finder"
	tell menu "Apple" of menu bar item "Apple" of menu bar 1
		click menu item "Clear Menu" of menu "Recent Items" of menu item "Recent Items"
	end tell
end tell

tell application "Finder" to activate
tell application "System Events" to tell application process "Finder"
	tell menu "Go" of menu bar item "Go" of menu bar 1
		click menu item "Clear Menu" of menu "Recent Folders" of menu item "Recent Folders"
	end tell
end tell

set restartDelay to 15

set progress total steps to restartDelay

repeat with i from restartDelay to 1 by -1
	set progress description to "The computer will restart in " & i & " seconds."
	set progress additional description to " "
	set progress completed steps to i
	delay 1
end repeat

tell application "System Events" to restart with state saving preference```

Just to add a bit more information. This “Simple Restart Combined” script was originally put together on my Intel 2014 Mac mini, running Monterey 12.6.8. The error occurs when I try to run this script on my 2020 M1 MacBook Pro, also running the same Monterey 12.6.8.

One last item, yes, I do have it added in Security & Privacy/Accessibility, and it runs perfectly if I run it from Script Editor.

You haven’t said how else you’re trying to run it. i.e. standalone app, from script menu, from command line using osascript, etc

Trying to run it from an “exported” Script Editor app, won’t run. It won’t run from the script editor either, at least not the last part. The first half of the script clears the Go/Recent Folders menu and the Apple/Recent Items menu. The second part of the script is to put a countdown to restart window on the screen starting at 15 seconds. The first part works just fine as both menus get cleared, but no window/clock come up on the screen.

Haven’t tried the Terminal as I have no experience running via % osascript.

The script below is similar, but with a different window/clock. It runs perfectly!

tell application "System Events" to tell application process "Finder"
	tell menu "Apple" of menu bar item "Apple" of menu bar 1
		click menu item "Clear Menu" of menu "Recent Items" of menu item "Recent Items"
	end tell
end tell

tell application "Finder" to activate
tell application "System Events" to tell application process "Finder"
	tell menu "Go" of menu bar item "Go" of menu bar 1
		click menu item "Clear Menu" of menu "Recent Folders" of menu item "Recent Folders"
	end tell
end tell

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property wController : missing value -- outlet equivalent in AsObjC
property notificationTitle : "Restarting...                                                  "
property aWidth : 360
property aHeight : 30


my performSelectorOnMainThread:"displayNotification:" withObject:({aWidth, aHeight, notificationTitle}) waitUntilDone:true
tell application "System Events" to restart with state saving preference


------------------------------------ HANDLERS ------------------------------------------------
on displayNotification:paramObj
	copy paramObj to {aWidth, aHeight, aTitle}
	set aColor to current application's NSColor's colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:0.9
	set aView to current application's NSTextView's alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, aWidth, aHeight))
	aView's setRichText:true
	aView's useAllLigatures:true
	aView's setTextColor:(current application's NSColor's cyanColor()) --
	aView's setBackgroundColor:aColor
	aView's setEditable:false
	aView's setFont:(current application's NSFont's fontWithName:"Menlo" |size|:20)
	repeat with i from 15 to 1 by -1
		set my wController to current application's NSWindowController's alloc()
		(aView's setString:("            " & i & " sec"))
		set aWin to makeWinWithView(aView, aWidth, aHeight, aTitle, 0.9)
		my (wController's initWithWindow:aWin)
		my (wController's showWindow:me)
		delay 1
		my wController's |close|()
	end repeat
end displayNotification:


on makeWinWithView(aView, aWinWidth, aWinHeight, aTitle, alphaV)
	set aScreen to current application's NSScreen's mainScreen()
	set aFrame to {{0, 0}, {aWinWidth, aWinHeight}}
	set aBacking to current application's NSTitledWindowMask
	set aDefer to current application's NSBackingStoreBuffered
	set aWin to current application's NSWindow's alloc()
	(aWin's initWithContentRect:aFrame styleMask:aBacking backing:aDefer defer:false screen:aScreen)
	aWin's setTitle:notificationTitle
	aWin's setDelegate:me
	aWin's setDisplaysWhenScreenProfileChanges:true
	aWin's setHasShadow:true
	aWin's setIgnoresMouseEvents:false
	aWin's setLevel:(current application's NSNormalWindowLevel)
	aWin's setOpaque:false
	aWin's setAlphaValue:alphaV --append
	aWin's setReleasedWhenClosed:true
	aWin's |center|()
	aWin's setContentView:aView
	return aWin
end makeWinWithView

Your first script uses GUI scripting, so when you save it as application, you have to add it to applications list allowed to Control Your Computer in Security & Privacy/Accessibility pane

Also, add Tell me to activate before progress bar steps

Not sure what happened, but this morning I copied the Script Editor file over from my Intel 2014 Mac mini a second time, deleted the previously copied file, created the application . . . and it’s fully working now on both machines. I took note of the fact that there was a difference in the two file sizes, not that much, but different. Possibly, when I recreated the script on the MacBook Pro I managed to miss a line of the script, by scrolling to copy. From now on I’ll just use “Edit/Select All.”