I am THAT close ...

When I add a particular menu item to one of my menus, I call:


property globalProp : globalValue

script myScript -- at the top of the applescript code
property scriptProp : scriptValue
--
on setProp(newValue)
	set scriptProp to newValue
end setProp
end script

-- then, here's the call
set theFile to ¬
	make new menu item at the beginning of the menu items of gMyMenu ¬
		with properties {name:theName, title:theTitle, enabled:true}
set script of theFile to myScript

Later, on, I routinely change globalProp … so I wish to change scriptProp to match, so every time I change globalProp, I call:


my setScriptProp(globalProp)

-- where

on setScriptProp(newValue)

	set countItems to count menu items of gMyMenu
	
	repeat with rf from 1 to countItems
		-- tell me to display dialog (name of menu item rf of gRecentMenu)  as string -- this displays OK
		tell script of (menu item rf of gMyMenu) to setProp(newValue)
	end repeat

end setScriptProp

With tell script of (menu item rf of gMyMenu) to setProp(newValue), I get the following error:

script of <> 1 of <> of <> id 7 of <> doesn’t understand the setProp message

???

No error if tell script of (menu item rf of gMyMenu) to set scriptProp to newValue

As my title states “I am THAT close …”

Sounds like a scope problem.

Calling Handlers in a tell Statement

Thanks, Bruce for your very prompt reply.

I totally agree that it sounds like a scope problem … but, the setProp Handler is part of the myScript script; so wouldn’t using my indicate belonging to the parent script (aka, the app)?

When I use tell script of … to set scriptProp to newValue every thing works. Applescript recognizes the property to be part of myScript, just not the Handler??

Hi John,

serious question: Is this script object stuff really necessary?

Yes.

I don’t know.

Check out the examples for the menu class.

I leave that answer to you, based on the following:


property allFiles : {}
-- more properties here

-- next .. right below all the properties
script openRecentItem
	
	property theFiles : {}
	
	on update menu item theMenuItem
		return true
	end update menu item
	
	on choose menu item theFile
		my OpenRecentFile(theFile)
	end choose menu item
	
	on setScriptFiles(newFiles)
		set theFiles to newFiles
	end setScriptFiles
	
	on getScriptFiles()
		return theFiles
	end getScriptFiles
	
end script

on OpenRecentFile(theRecentFile)
	
	set allFiles to openRecentItem's getScriptFiles() -- or, set allFiles to theFiles of openRecentItem
	set windowCount to (count allFiles)
	-- etc
end OpenRecentFile

-- every time I create a new menu item ...

set aFile to ¬
	make new menu item at the beginning of the menu items of gRecentMenu ¬
		with properties {name:aName, title:aTitle, enabled:true}
set script of aFile to openRecentItem

-- and every time I change the global array, allFiles via adding or deleting ...

tell openRecentItem to setScriptFiles(allFiles)

-- other stuff

… and it all works until OpenRecentFile calls:

set allFiles to openRecentItem's getScriptFiles()

and here count allFiles = 0, in spite of the fact that, as above, I call tell openRecentItem to setScriptFiles(allFiles).

It’s as if when I call set script of aFile to openRecentItem, a new instance of openRecentItem is created, which would explain ??? why count allFiles turns up = 0.

In the last 30 minutes, I looked up: a reference to allFiles and put that in openRecentItem, as so:


script openRecentItem
	
	property theFiles : a reference to allFiles
	-- etc
end script

… but all that generates is a spinning beach ball …

By the way, before I forget the good news, Happy Easter!!!

This is one of those many times that suggestions lead me down many paths, each one of which teaches me a lot … eventually the combined learning results in a different solution which would never have been found if it were not for each separate path travelled.

Thanks Stefan, thanks Bruce and another loud round of applause for others from MacOSXHints … the first individual calls him/herself “Red Menace” … dunno about the “Menace”, but definitely super smart!! … and the second is “TW”.

Problem solved by using saved Library file whose source is simply:

property theFiles : {}

Here’s the pertinent portions of the main code …


property filesScriptRef : ""
property filesScriptObj : null
property allFiles : {}

on InitScript()
	
	script openRecentItem
		
		on update menu item theMenuItem
			return true
		end update menu item
		
		on choose menu item theFile
			my OpenRecentFile(theFile)
		end choose menu item
		
	end script
	
	return openRecentItem
	
end InitScript

on loadFilesScript()
	
	tell application "Finder"
		set myScriptFile to (path to application support folder from user domain as Unicode text) & ¬
			"Excel Medical:allFiles.scpt"
		set libraryExists to (file myScriptFile exists)
		
		if not libraryExists then
			set itsContent to "property theFiles : {}"
			
			-- create it

		end if
		
		set filesScriptRef to file myScriptFile as alias
		set filesScriptObj to load script filesScriptRef
	end tell
	
end loadFilesScript

-- my on launched Handler calls:
my loadFilesScript()
set ori to my InitScript()

on setScriptFiles(newFiles)
	
	set theFiles of filesScriptObj to newFiles
	store script filesScriptObj in filesScriptRef replacing yes
	
end setScriptFiles

-- each time I change the global allFiles, I call [b]my setScriptFiles(allFiles)[/b]

on getScriptFiles()
	
	my loadFilesScript() -- reload "allFiles.scpt"
	return (theFiles of filesScriptObj) as list
	
end getScriptFiles
 
on OpenRecentFile(theRecentFile)
	
	set allFiles to my getScriptFiles()
	set windowCount to (count allFiles)
	-- etc

It works … and did I learn bunches of neat stuff about POSIX paths, files, colons, slashes, child objects … again, thanks Stefan, thanks Bruce and definitely the “Red Manace” and “TW” from MacOSXHints!!