How to refresh item in a popup menu in Livecode - livecode

I have a pop-up menu and items are read from an array and I want to refresh when I press the right mouse button. Also, is there any way to disable/enable the items of the pop-up menu?

First create a pop-up menu and give it a name, e.g. "List A". Now create a control with a mouseDown handler or add a mouseDown handler to the card script.
on mouseDown theMouseButton
if theMouseButton is 3 then
put "One item,Another item,A third item,The last item" into myList
replace comma with cr in myList
put myList into btn "List A"
popup btn "List A"
end if
end mouseDown
Set the script of the pop-up menu button to the following:
on menuPick theItem
switch theItem
case "One item"
answer "Congrats"
break
default
beep
answer "Not implented yet"
break
end switch
end menuPick
You can disable individual items by preceding them with a parenthesis:
put "One item,Another item,(A third item,The last item" into myList
This will disable the third item in the menu.
There is no need to use arrays.

Related

Make Menu Item Do Something AppleScript

I'm writing a program in AppleScript that creates a menu in the menu bar on MacOS. This is my code:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown
property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"
property theList : "Settings Battery Quit"
-- example list for the menu items that can be used. Ideally you will have your list created dynamically
(*MENU ITEMS
- Settings
- Battery Stats
----------
- Quit
*)
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
on menuNeedsUpdate:(menu)
(* NSMenu's delegates method, when the menu is clicked this is called.
We use it here to call the method makeMenus(). Which removes the old menuItems and builds new ones.
This means the menu items can be changed dynamically.
*)
my makeMenus()
end menuNeedsUpdate:
on makeMenus()
newMenu's removeAllItems() -- remove existing menu items
-----< (* this is just to show in this example a dynamic list for the menu items
set allMenuItems to {"Settings", "Battery Stats", "Quit"}
---- <
repeat with i from 1 to number of items in allMenuItems
set this_item to item i of allMenuItems
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me) -- required for enabling the menu item
if i is equal to 2 then
(newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
end if
end repeat
end makeMenus
--menuItems action is requied for the menu to be enabled
on someAction:sender
MenuItem --do some thing
end someAction:
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
-- set up the initial NSMenu of the statusbar
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
newMenu's setDelegate:me (*
Requied delegation for when the Status bar Menu is clicked the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.
*)
StatusItem's setMenu:newMenu
end makeStatusBar
my makeStatusBar()
The function
on someAction:sender
--MenuItem --do some thing
end someAction:
is what I need to change. I don't know how to run specific tasks based on which menu item is clicked. When I put code straight in the function:
on someAction:sender
--MenuItem --do some thing
display dialog "This runs on menu item click"
end someAction:
then the code runs whenever ANY menu item is clicked. I want it to run specific tasks for specific menu items. The menu items are:
Settings
Battery Stats
Quit
I tried this:
on someAction:sender
--MenuItem --do some thing
if sender is equal to "Settings"
--Code here!
end if
end someAction:
However, nothing happened when I clicked "Settings" in the menu.
Any help would be appreciated!
You can run specific tasks for specific menu items by checking the sender title:
on someAction:sender
set theTitle to title of sender as string
if theTitle is "Settings" then
display dialog "This runs when menu item \"Settings\" clicked"
else if theTitle is "Battery Stats" then
display dialog "This runs when menu item \"Battery Stats\" clicked"
else if theTitle is "Quit" then
quit
else
-- do nothing
end if
end actionHandler:

How to prevent change in a listbox selected item when users click and move mouse before mouseup

I am using the following code in a scrolling field. It works fine in both windows and android. The only problem is that the selected item changes as the mouse mouses up and down the list. I would like the original highlighted line to remain highlighted during the scroll and only change if the mouse have not moves move than 11 units(row height is 22) from initial vertical position. How do I modify this code to achieve that?
local lmousev,lvscroll,lscrolling
on mousedown
--If we don't use lscrolling the list will scroll when the mouse hovers over it
put true into lscrolling
--Set the initial vertical position of the cursor
put the mousev into lmousev
--Set the inital position of the vertical scroll
put the dgvscroll of me into lvscroll
end mousedown
on mousemove x,y
if lscrolling=true then --lscrolling is only true after mousedown so no scroll when mouse hovers over
--adjust the scroll position based on the vertical distance that the mouse has moved since mousedown
set the dgvscroll of me to lvscroll -(y - lmousev)
end if
end mousemove
on mouseUp
--stop scrolling when mouse hovers
put false into lscrolling
If abs( the mousev-lmousev)<11 then --If vertival position of mouse has not moved far from vertical position at mousedown
--selectlist command is in the group. It insert list selection into the textbox
selectlist
end if
end mouseUp
on mouserelease
--stop scrolling when mouse is release outside the listbox and there is no mouseup
put false into lscrolling
end mouserelease
The button,textbox and listbox that forms the combobox are grouped.The code to show/hide listbox and add selected to textbox is in the group script as follows:
Additional Info
local ldrop=false
on buttonclick --show/hide the listbox
--Get the name of the group,textbox & listbox.
--Naming convention required:- textboxname=txt & groupName,listboxname=lst & groupname
--Using quote as deliminater allows us to get the groupname from group "groupname"
set the itemdel to quote
put item 2 of the name of me into groupname
put "lst" & groupname into lstName
put "txt" & groupname into txtName
--status of ldrop let us know if the listbox is visible
if ldrop=false then
--show listbox
set the visible of field lstName to true
put true into ldrop
else
--hide listbox
set the visible of field lstName to false
put false into ldrop
end if
end buttonclick
on selectlist
--Get the name of the group,textbox & listbox.
--Naming convention required:- textboxname=txt & groupName,listboxname=lst & groupname
--Using quote as deliminater allows us to get the groupname from group "groupname"
set the itemdel to quote
put item 2 of the name of me into groupname
put "lst" & groupname into lstName
put "txt" & groupname into txtName
put the hilitedLine of field lstName into lhilitedLine
put line lhilitedLine of field lstName into field txtName
set the visible of field lstName to false
put false into ldrop
end selectlist
I think you have to make your own control that acts like a list so that you can control all the messages it sends and receives. You are otherwise stuck with some of those basic functions of LC's built-in controls. seems like you'd need an engine-level tweak to the control.

How to limit the items in popup?

How to limit the items in the popup. "Sar" array contains lot of words, with respect to the selection. if my first popup contains 15 items and did the change. when going to the next popup, if it contains 10 items, the remains 5 items from the first also added to the second popup options.
How to kill that buffer in popup menu items? Iam using this code
global jar,myjar,sam,mySam,Dic,Sar
on mouseUp pMouseButton
put the selectedText of field "MytextField" into Ftext
if pMouseButton = 3 then
put the number of lines of (the keys of sam) into mySam
repeat with i = 1 to mySam
if sam[i] contains Ftext then
put sam[i] into Sar
split Sar by comma
end if
end repeat
put the text of button "M" into tText
put the number of words of (the keys of Sar) into mylength
repeat with x = 1 to mylength
put Sar[x] into line x of tText
end repeat
put mylength into kk---now iam using this code for delete the buffer
----repeat with j = kk to 100
---put "" into line j of tText
---end repeat
set the text of button "M" to tText
popup button "M" at the clickLoc
else
--popup button "M" at the clickLoc
-- do other stuff here
end if
end mouseUp
Is any option for limit the items in popup. Example:- if popup contains 15 items, i want to see only 10 items, remaining 5 could be see only clicking the bottom button in the popup
You are using globals which might not be what you want.
Try emptying the global with put empty into Sar before adding some new values or use locals which will be emptied after your code is done.

I lose the selected text while clicking the popup menu

I lose the selected text while clicking the popup menu (clicking the right button) in the textfield "MytextField". I am using this code. Choice 2 & 3 works fine because they not need the selected option, but choice 1 doesn't works. Is there any alternate way to select?.
on menuPick pItemName
put the selectedText of field "MytextField" into Ftext
switch pItemName
case "Choice 1"
answer Ftext
break
case "Choice 2"
answer "bye"
break
case "Choice 3"
answer "Please"
break
end switch
end menuPick
Set the traversalon of the popup button to false.

mouseUp on an empty row in the Data Grid with native mobile scroller is causing an error

I'm using a native scroller for a Data Grid displaying about 700 records. When the number of visible records is small (1-2) and they do not cover the whole height of the Data Grid rectangle then when executing mouseUp on an empty row in the Data Grid I'm getting this error:
executing at 2:57:26 PM
Type Chunk: can't find background
Object: main
Line: send mouseUp to field "Label" of group "Row Template 0003" of group "dgList" of group "dgListMask" of group "DataGrid 1" of card "main"
Hint: mouseUp
The line above is a part of the scroller script in the card script enabling simple tapping on the row when not scrolling the DG:
on mouseUp
if not isScrolling then
send mouseUp to field "Label" of group "Row Template 0003" of group "dgList" of group "dgListMask" of group "DataGrid 1" of card "main"
end if
end mouseUp
To fix the error I changed it to:
on mouseUp
if gCurrentIndex = empty then -- added this line in the hope that it will fix it but it does not
exit to top -- added this line in the hope that it will fix it but it does not
else -- added this line in the hope that it will fix it but it does not
if not isScrolling then
send mouseUp to field "Label" of group "Row Template 0003" of group "dgList" of group "dgListMask" of group "DataGrid 1" of card "main"
end if
end if
end mouseUp
but it did not solve he problem.
The code in the Row Template Label is:
global gCurrentView,gCurrentLine,gCurrentIndex
on mouseUp
put the text of me into fld "foneline" of cd "oneline"
put the dgText of grp "DataGrid 1" into gCurrentView
put the dgHilitedLines of grp "DataGrid 1" into gCurrentLine
put the dgHilitedIndexes of grp "DataGrid 1" into gCurrentIndex
lock screen for visual effect
unlock screen with visual effect reveal right slow
go cd "oneline"
end mouseUp
To replicate the error follow these steps:
open the stack
click on "My Selection" button
click on OK on the message "Sorry..."
click on the grey star below the quote (select just 1 quote)
click on "Dashboard" button
click on "My Selection" button
click on the light yellow area below the quote
the Error window will show up
if this first time the error does not show then continue
click on the text quoete above the the light yellow area
click on the "Entire selection" button
click on the light yellow area below the quote - this time error will come up for sure.
The above steps may look complex but it's very simple once you see the stack which is here: DG-empty row error.zip
How to fix this error?
Not sure if gCurrentIndex has been declared as a global in the second block of code ...
global gCurrentIndex
on mouseUp
if gCurrentIndex = empty then -- added this line in the hope that it will fix it but it does not
exit to top -- added this line in the hope that it will fix it but it does not
else -- added this line in the hope that it will fix it but it does not
if not isScrolling then
send mouseUp to field "Label" of group "Row Template 0003" of group "dgList" of group "dgListMask" of group "DataGrid 1" of card "main"
end if
end if
end mouseUp
The solution was adding this block of code to the text field of the Row Template of the DG instead of to the scroller code:
if gCurrentIndex = empty then
exit to top
else
lock screen for visual effect
unlock screen with visual effect reveal right slow
go cd "oneline"
end if
Re this line:
send mouseUp to field "Label" of group "Row Template 0003" of group "dgList" of group "dgListMask" of group "DataGrid 1" of card "main"
and its alternative:
send mouseUp to field "Label"
I tested the scrolling and tapping on the row with and without this line and there is no difference, it works. I don't remember where from I got that line, probably from someone on the LC Forum. The idea was to make sure that when touching the row to scroll the DG it does not go to the card "oneline". I'll keep watching how it works now.

Resources