PDA

View Full Version : Renaming menu items


davestewart
10-10-2006, 04:44 PM
Is it possible to rename menu items dynamically? I have a Macro that displays as "Open most recent file" in my File menu, which loads either the last Autobackup, or the first most-recent file (whechever is newer).

It would be nice to display, using the onEnabled handler, "Open most recent file (Dave's File 03.max)" or whatever.

If it's not straightforward (for example I have to dynamically redefine the macroscript), I may just print it to the Listener.

Any ideas would be welcome,
Thanks,
Dave

Zbuffer
10-13-2006, 12:30 AM
Hi,

funny you ask about this, I just did a little script to have a "recent script' menu added to the MaxScript menu ;)

I used the .settitle funtion to set the menu title,
if interested here is the code i wrote:


------------------------------------------------------------------------
-- FileName: Zbuffer-OpenScript.mcr
-- Version: 0.1
-- Author: Zbuffer
-- date: 28/09/2006
--
-- description: Replaces the MaxScript/Open Script menu and adds a 'Recent Scripts' menu to the MaxScript menu.
-- usage: 1. Put this script in the <MAXROOT>\UI\MacroScipt folder. (or evaluate)
-- 2. Go to the 'Customize/Customize User Interface' menu
-- 3. On the 'menu' tab, replace/add the 'Maxscript/Open Script' with this one ('Zbuffer' category).
-- Tou can modify the Maxfiles variable if you want more than 10 recent files
-------------------------------------------------------------------------

macroscript OpenScript
category:"Maxscript
buttonText:"Open Script"
(
local Maxfiles = 10 -- Number of recent files in the Menu
local ScriptDir = (getdir #scripts)+"\\" -- Always show the open file dialog in this folder.
local INI = (getdir #plugcfg)+"\\RecentScripts.ini"
local FileCount = getINIsetting INI "FileCount" "Count"
if FileCount == "" then FileCount = 0 else FileCount = FileCount as integer -- check for 1st run
local Recentfiles=#() -- array holding the recent file names
-- browse for a file to open.
local f=getOpenFileName caption:"Choose Editor File" fileName:ScriptDir types:"All Scripts(*.ms;*.mcr)|*.ms;*.mcr|MaxScript(*.ms)|*.ms|Macroscript (*.mcr)|*.mcr|Text Files (*.txt)|*.txt|All|*.*|"

if f !=undefined do
(
-- fix path string
while ((i=findstring f "\\") != undefined) do f=replace f i 1 "/"

-- Update the Recent Scripts list
for i=1 to FileCount do append Recentfiles (getINIsetting INI "FileList" ("File"+i as string))
if ((finditem Recentfiles f) == 0) then (insertItem f Recentfiles 1)
else (
deleteItem Recentfiles (finditem Recentfiles f)
insertitem f Recentfiles 1
)
if RecentFiles.count>Maxfiles do deleteItem Recentfiles (Maxfiles+1)
-- Write the list to the INI file
for i=1 to Recentfiles.count do setINIsetting INI "FileList" ("File"+i as string) Recentfiles[i]
setINIsetting INI "FileCount" "Count" (Recentfiles.count as string)

-- Build the menu
local ScriptMenu = MenuMan.findMenu "&MAXScript"
-- delete the Recent Scripts Menu if it exists
if ((Scriptmenu.getItem 1).getTitle())=="Recent Scripts" do ScriptMenu.removeItemByPosition 1
-- Create the Recent Scripts Menu
local RecentFileMenu=MenuMan.createMenu "Recent Scripts"
-- create the submenuitems
for i=1 to Recentfiles.count do
(
-- create Macroscripts for the menuitems
macros.new "Recent Scripts" ("Script"+i as string) "" (i as string+" "+Recentfiles[i]) ("(edit \""+Recentfiles[i]+"\")")
local testItem = menuMan.createActionItem ("Script"+i as string) "Recent Scripts"
testItem.setTitle Recentfiles[i]
RecentFileMenu.additem testItem -1
)
-- add the subitems to the menu
local RecentFileMenuItem = menuMan.createSubMenuItem "RecentScripts" RecentFileMenu
-- add the menu at the 1st position in the MAxscript menu
ScriptMenu.additem RecentFileMenuItem 1
menuMan.updateMenuBar()
-- finaly, open the file in the editor
edit f
)
)


I use it as a replacement for the "Maxscript/Open Script" menu.
it stores the filenames in a RecentScripts.ini file
it also creates macroscripts each time a new file is opened.

Hope you find this usefull

Note: there seams to be a mistake in the macros.new documentation:
it says:
macros.new <name_string> <category_string> <toolTip_string> <buttonText_string> <body_string>


but I think the fist 2 arguments need to be inverted:
macros.new <category_string> <name_string> <toolTip_string> <buttonText_string> <body_string>

davestewart
10-13-2006, 09:04 AM
Cool stuff man!
I have a ton of "real" (!) work today, but I'll check it out first chance I get.
Cheers,
Dave

PEN
10-13-2006, 01:02 PM
Zbuffer, Cool stuff, The only comment from me is it looks a bit messy because it creates more and more macro script files. Is there a way that you could just write the macro script lines using format to a single file so that the last used are all in one file?

Using popup menus I have populated them with an INI type file before. I wounder if the same method could be used here?

Zbuffer
10-13-2006, 06:08 PM
Is there a way that you could just write the macro script lines using format to a single file so that the last used are all in one file?


Yep, simple update, and true, it's much cleaner, so now even if you increase the MaxFile to 20, you only get 1 macroscript file called 'RecentScripts.mcr'

Thx for your comments guys, and glad i can give back to you from wich i have learned a lot !





------------------------------------------------------------------------
-- FileName: MaxScript-OpenScript.mcr
-- Version: 0.2
-- Author: Zbuffer
-- date: 28/09/2006
--
-- description: Replaces the MaxScript/Open Script menu and adds a 'Recent Scripts' menu to the MaxScript menu.
-- usage: 1. Put this script in the <MAXROOT>\UI\MacroScipt folder. (or evaluate)
-- 2. Go to the 'Customize/Customize User Interface' menu
-- 3. On the 'menu' tab, replace/add the 'Maxscript/Open Script' with this one ('MaxScript' category).
-- Tou can modify the Maxfiles variable if you want more than 10 recent files
-------------------------------------------------------------------------

macroscript OpenScript
category:"MaxScript"
buttonText:"Open Script"
(
local Maxfiles = 10 -- Number of recent files in the Menu
local ScriptDir = (getdir #scripts)+"\\" -- Always show the open file dialog in this folder.
local INI = (getdir #plugcfg)+"\\RecentScripts.ini"
local FileCount = getINIsetting INI "FileCount" "Count"
if FileCount == "" then FileCount = 0 else FileCount = FileCount as integer -- check for 1st run
local Recentfiles=#() -- array holding the recent file names
-- browse for a file to open.
local f=getOpenFileName caption:"Choose Editor File" fileName:ScriptDir types:"All Scripts(*.ms;*.mcr)|*.ms;*.mcr|MaxScript(*.ms)|*.ms|Macroscript (*.mcr)|*.mcr|Text Files (*.txt)|*.txt|All|*.*|"

if f !=undefined do
(
-- fix path string
while ((i=findstring f "\\") != undefined) do f=replace f i 1 "/"

-- Update the Recent Scripts list
for i=1 to FileCount do append Recentfiles (getINIsetting INI "FileList" ("File"+i as string))
if ((finditem Recentfiles f) == 0) then (insertItem f Recentfiles 1)
else (
deleteItem Recentfiles (finditem Recentfiles f)
insertitem f Recentfiles 1
)
if RecentFiles.count>Maxfiles do deleteItem Recentfiles (Maxfiles+1)
-- Write the list to the INI file
for i=1 to Recentfiles.count do setINIsetting INI "FileList" ("File"+i as string) Recentfiles[i]
setINIsetting INI "FileCount" "Count" (Recentfiles.count as string)

-- Build the menu
local ScriptMenu = MenuMan.findMenu "&MAXScript"
-- delete the Recent Scripts Menu if it exists
if ((Scriptmenu.getItem 1).getTitle())=="Recent Scripts" do ScriptMenu.removeItemByPosition 1
-- Create the Recent Scripts Menu
local RecentFileMenu=MenuMan.createMenu "Recent Scripts"
-- create Macroscripts for the menuitems
local MCRname=getdir #ui +"\\MacroScripts\\RecentScripts.mcr"
local MCR=openfile MCRname mode:"w"
for i=1 to Recentfiles.count do
(
format "macroscript Script% category:\"Recent Scripts\" buttonText:\"% %\"\n(edit \"%\")\n\n" i i Recentfiles[i] Recentfiles[i] to:MCR
)
close MCR
filein MCRname
-- create the submenuitems
for i=1 to Recentfiles.count do
(
local testItem = menuMan.createActionItem ("Script"+i as string) "Recent Scripts"
testItem.setTitle Recentfiles[i]
RecentFileMenu.additem testItem -1
)
-- add the subitems to the menu
local RecentFileMenuItem = menuMan.createSubMenuItem "RecentScripts" RecentFileMenu
-- add the menu at the 1st position in the MAxscript menu
ScriptMenu.additem RecentFileMenuItem 1
menuMan.updateMenuBar()
-- finaly, open the file in the editor
edit f
)
)

CGTalk Moderation
10-13-2006, 06:08 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.