Kirix Support Forums

icon/menu item for script

Please post any help questions, requests or other feedback here.

icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 11:11 am

I created an extension which has a script that runs and creates a form. I want to have this in my menu for users to select and run from or as an icon on my menu. Can this be done and if so, how?

thanks,
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ken on Tue Jul 03, 2012 11:41 am

Ken Kaczmarek
Kirix Support Team
User avatar
Ken
Kirix Support Team
 
Posts: 147
Joined: Mon Dec 19, 2005 10:36 am

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 12:30 pm

I was able to add the menu with the code provided in the link you gave me but am not able to add the extension the menu. Can you help? The new Menu is Called "AuditTools"
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Tue Jul 03, 2012 1:08 pm

Try this

Code: Select all
        var menu_bar = HostApp.getFrameMenu();
        var menu = new Menu("AuditTools");

        //  add items to 'menu' here
        // ...

        menu_bar.insert(menu, menu_bar.getMenuCount()-1);
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 1:28 pm

This added AuditTools as a Menu, but not my package ("Project Admin") as an Item in AuditTools Menu. Now I have 2 items in my menu bar called "AuditTools". How do i add my "Project Admin" package to the AuditTools Menu?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 1:45 pm

I've edited the script you gave me a link to and have been able to add a menu and an item to the menu but when i click on the item, it does nothing.


var tools_menu = getToolsMenu();

// if the menu item already exists, first remove the
// old menu item before we add the new menu item
var item = tools_menu.findMenuItem("Project Admin");

var item = insertToolsMenuItem("Project Admin");


function getToolsMenu()
{
// summary: returns the host application Tool menu item

// get the host application's main menu
var menu_bar = HostApp.getFrameMenu();
var tools_menu;

// get the Tool menu item; if it doesn't exist, create it
var id = menu_bar.findMenu("AuditTools");
if (id < 0)
{
tools_menu = new Menu("AuditTools");
menu_bar.insert(tools_menu, menu_bar.getMenuCount()-1);
}
else
{
tools_menu = menu_bar.getMenu(id);
}

return tools_menu;
}

function insertToolsMenuItem(name)
{
// summary: inserts a menu item with the given name in
// an appropriate location in the host application's Tool
// menu item

// get the Tool menu item
var tools_menu = getToolsMenu();

// if the menu item already exists, first remove the
// old menu item before we add the new menu item
var item = tools_menu.findMenuItem(name);
if (item)
tools_menu.remove(item);

// note: the 'click' event for this item must be connected
// after this function is called
item = new MenuItem(name);

// the index of the separator right after the 'Projects...'
// menu item
var projects_pos = tools_menu.findMenuItem("Project Admin") + 1;

// the index of the separator right before the 'Options...'
// menu item
var options_pos = tools_menu.findMenuItem("Data Extract") - 1;

// if we couldn't find one of the menu items, return
if (projects_pos < 0 || options_pos < 0)
{
tools_menu.insert(item, "Project Admin");
return;
}

// if these two menu items have the same index, we need
// to insert another separator to make the 'Extensions Group'
if (projects_pos == options_pos)
{
// this is the first extension menu item that is being
// inserted in the 'Extensions Group'
tools_menu.insertSeparator(projects_pos++);
tools_menu.insert(item, projects_pos);
}
else
{
// we've already created the 'Extensions Group', all
// we have to do is insert the menu item at the end
// of the area
tools_menu.insert(item, 1); //options_pos);
}

return item;
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Tue Jul 03, 2012 2:23 pm

It appears that you need to connect your menu item to an event handler:


If you are connecting it to a class function ("method"):
Code: Select all
my_menu_item.click.connect(this, onMyMenuItem);   


If you are connecting it to a non-class function

Code: Select all
my_menu_item.click.connect(onMyMenuItem);     


Your handler would look like this:

Code: Select all
    function onMyMenuItem()
    {
    }


Let me know.
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 2:51 pm

I get an error msg: "Script Runtime Error: Term 'item.click.connect' does not evaluate to a function."

var tools_menu = getToolsMenu();
var item = tools_menu.findMenuItem("Project Admin");
var item = insertToolsMenuItem("Project Admin");

item.click.connect(onMyMenuItem);
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Tue Jul 03, 2012 3:06 pm

Your insertToolsMenuItem() function isn't returning anything.
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 3:54 pm

ok, no error msg and menu and item get created but when i click on the item in the menu, nothing happens.

/***********************************************************************************
Menu creation for packages for forms/scripts used by Audits
***********************************************************************************/


use strict variables;
use strict semicolons;

Console.clear();

var tools_menu = getToolsMenu();
var item = tools_menu.findMenuItem("Project Admin");
var item = insertToolsMenuItem("Project Admin");

item.click.connect(onMyMenuItem);

function onMyMenuItem()
{
}

Application.run();


function getToolsMenu()
{
// summary: returns the host application Tool menu item

// get the host application's main menu
var menu_bar = HostApp.getFrameMenu();
var tools_menu;

// get the Tool menu item; if it doesn't exist, create it
var id = menu_bar.findMenu("AuditTools");
if (id < 0)
{
tools_menu = new Menu("AuditTools");
menu_bar.insert(tools_menu, menu_bar.getMenuCount()-1);
}
else
{
tools_menu = menu_bar.getMenu(id);
}

return tools_menu;
}

function insertToolsMenuItem(name)
{
// summary: inserts a menu item with the given name in
// an appropriate location in the host application's Tool
// menu item

// get the Tool menu item
var tools_menu = getToolsMenu();

// if the menu item already exists, first remove the
// old menu item before we add the new menu item
var item = tools_menu.findMenuItem(name);
if (item)
tools_menu.remove(item);

// note: the 'click' event for this item must be connected
// after this function is called
item = new MenuItem(name);

// the index of the separator right after the 'Projects...'
// menu item
var projects_pos = tools_menu.findMenuItem("Project Admin") + 1;

// the index of the separator right before the 'Options...'
// menu item
var options_pos = tools_menu.findMenuItem("Data Extract") - 1;

// if we couldn't find one of the menu items, return
if (projects_pos < 0 || options_pos < 0)
{
//tools_menu.insert(item, "Project Admin");
//return;
}

// if these two menu items have the same index, we need
// to insert another separator to make the 'Extensions Group'
if (projects_pos == options_pos)
{
// this is the first extension menu item that is being
// inserted in the 'Extensions Group'
tools_menu.insertSeparator(projects_pos++);
tools_menu.insert(item, projects_pos);
}
else
{
// we've already created the 'Extensions Group', all
// we have to do is insert the menu item at the end
// of the area
tools_menu.insert(item, options_pos); // 1); //options_pos);
}
alert(item);
return item;
}
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Tue Jul 03, 2012 4:50 pm

Change your handler to this and you'll see that it is indeed being called.

Code: Select all
function onMyMenuItem()
{
alert("onMyMenuItem()");
}
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 5:17 pm

No popup alert comes up when i do this.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 5:33 pm

when i put this alert at the end of the insertToolsMenuItem function, It returns this "[object MenuItem]". Is this correct? Just want to make sure it is returning the right thing as well.

alert(item);
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby abenitez77 on Tue Jul 03, 2012 6:31 pm

I made a change in this line from:

item.click.connect(onMyMenuItem);

to :

item.click.connect(onMyMenuItem());

and now I get the alert popup in the function...so it is working now. It added the Project Admin item to the Menu but when I click on it, it does nothing. How does it know what extension or the name of the extension to run?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Thu Jul 05, 2012 12:01 pm

For me your script required no modification. It worked out of the box. Did you add an alert to your function?

Code: Select all
function onMyMenuItem()
{
alert("onMyMenuItem()");
}


Adding() to the connection statement is not the right thing to do, as it will invoke your function immediately, not when the event is fired.

Sounds to me like your function is a class method, and not a global function! If you want a class method, change your connect invocation to

connect(this, onMyMenuItem);
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Re: icon/menu item for script

Postby abenitez77 on Thu Jul 05, 2012 1:30 pm

ok, now I get the alert msg popup after making that change. It creates the menu("AuditTools") and it creates the item ("Project Admin"). When I click on the item Project Admin it gives me the popup...but my form does not open up for me.
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby abenitez77 on Thu Jul 05, 2012 2:06 pm

Maybe I need to start over. I have two scripts that are forms. I want to have a menu for my scripts I create so that users can run them from the menu. I thought making them extensions would keep them in a central place so that I can use them from a dropdown menu and the user doesn't have to run the actual script either.

How do i do this? Was I in the right path to do this?
abenitez77
Registered User
 
Posts: 143
Joined: Fri Jan 21, 2011 12:42 pm

Re: icon/menu item for script

Postby Ben on Mon Jul 09, 2012 8:27 am

I think you are pretty close with your existing code. If you want to start over, I think it's always a healthy exercise. I would definitely make full use of Javascript classes. Every form should be contained in its own class. If the amount of code becomes large, I would start putting separate forms in separate .js files and using include statements to bind them into a single unit.

When you distribute your extension, you can then zip it all up into one extension .zip file, easing distribution.

Good luck and have fun coding!
Ben Williams
Kirix Support Team
User avatar
Ben
Kirix Support Team
 
Posts: 525
Joined: Mon Dec 19, 2005 6:29 am

Return to Strata Help & Feedback