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.
Related
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:
I have linked a livecode application to a relational database. I want to use a combobox to display values related to an ID. I other program such as msaccess this is done by having combobox with 2 columns. The first is linked to the ID and set to 0 width and the second displays the related value. Is that possible in livecode?
I have a text field and a list field and I managed to get it working using the following code on rawkeyup
global strHilitedLine,strTempHilitedLine,booAfterReturn,booFirstKeyUp
on keyup -- when your press a character key
TempHilited
end keyup
on returninfield --when user press return key on keyboard
--accept the temporary hilite
put the hilitedLine of field "lstfood" into strHilitedLine
put empty into strTempHilitedLine
--set booFirstKeyUp to true so that the keyup command will know that
-- the next keyup is the first after clicking the enter/return key
put "True" into booFirstKeyUp
--clear field txtfood
put empty into fld "txtFood"
end returninfield
on enterkey --when user press enter key on mobile(code same as on returninfield)
set the hilitedLine of field "lstfood" to strTempHilitedLine
put the hilitedLine of field "lstfood" into strHilitedLine
put empty into strTempHilitedLine
put "True" into booFirstKeyUp
put empty into fld "txtFood"
end enterkey
on TempHilited
if booFirstKeyUp="True" then
--store the value of hilitedline if this is the first keyup after
-- clicking enter(see on enterkey)
put the hilitedLine of field "lstfood" into strHilitedLine
put "False" into booFirstKeyUp
end if
--set hilitedlines to the hilitedlines just after clicking enter
set the hilitedLine of field "lstfood" to strHilitedLine
-- cleartemporary hilitedlines from previous keyup
put empty into strTempHilitedLine
-- create array from field lstFood and find if the text in txtFood
-- appears at the start of any item in the array
put field "lstfood" into arrFood
filter lines of arrFood with regex pattern "^" & me into strLineText
--create new value to temporarily hilite
if the length of strHilitedLine>0 and the length of strLineText>0 then
put strHilitedLine & "," & lineoffset (strLineText ,field
"lstfood") after strTempHilitedLine
else if the length of strHilitedLine>0 then
put strHilitedLine into strTempHilitedLine
else if the length of strLineText>0 then
put lineoffset (strLineText ,field "lstfood") into strTempHilitedLine
end if
--set temporay hilite
set the hilitedLine of field "lstfood" to strTempHilitedLine
put the length of me into mylength
--Select the part of txtFood that user did to type so that it is overwritten on the next keyup
put strlinetext into field "txtFood"
select char mylength +1 to the length of me of field "txtFood"
End TempHilited
As you can see it is a long convoluted code. Happy to hear if you have a more
-- efficient way of achieving the same.
You mentioned "Store the data in a custom property, filter to include relevant lines, put the remaining data into the field" and I believe you were alluding to another method of doing it but I have not really work out how to do that
No, menu buttons don't allow for multiple columns in LiveCode. However, it is possible to make your own combobox, e.g. by using a stack panel for a menu and adding two fields to that stack. Use the properties inspector of a menu button to assign a stack as a stack panel.
I made such a stack panel, with only one column, quite some time ago:
It would be easy to make this a two-column menu: just make the field half as wide, add another field and update the hilitedLine of the field that doesn't have focus when the hilitedLine of the focused field changes. This example isn't a real stack panel, but it works pretty much the same. I open the stack hidden as a palette, set the size and location, and show it making sure that it has focus.
(The picture is from my own website; the library is in an area available to donors only).
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.
Very simple question but how to start a script from a "choose from List"
E.g :
set issueList to {"1", "2", "2", "4"}
set selectedIssue to {choose from list issueList}
if selectedIssue is {"1"} then
display dialog "ok" buttons {"1"} default button 1
else if selectedIssue is {"Holds Charge"} then
-- do nothing
display dialog "ok" buttons {"2"} default button 1
else if selectedIssue is {"2"} then
-- do nothing
display dialog "ok" buttons {"3"} default button 1
else if selectedIssue is {"3"} then
-- do nothing
display dialog "ok" buttons {"4"} default button 1
end if
what I expected in this script its when I click on a element of the list , a notification start (or any other script) but I have no result.
Cheers
The notification returns {{"1"}}, not {"1"}. Change your code to
if item 1 of selectedIssue is {"1"} then
etc and it should work.
Hello I got the same issue with yours, but I solved it with code like this:
set mailList to {"One","Two","Three","Four"}
set mailType to choose from list mailList
if item 1 of mailType is "One" then
display dialog mailType
else if item 1 of mailType is "Two" then
display dialog mailType
else if item 1 of mailType is "Three" then
display dialog mailType
else if item 1 of mailType is "Four" then
display dialog mailType
end if
How to change this choice 1, choice 2 ,choice 3 in popu menu to special words (passing by variables from array). With out using the popup properties menu. Means: contents of array svar[2] instead of choice 1
contents of array svar[3] instead of choice 2 .. so on.
so each time the value of choice 1, choice 2 will differ.
global searchStr global replaceStr global Ftext global myArrayToBe global myArraylength global gvar on menuPick pItemName put the number of lines of (the keys of myArrayToBe) into myArraylength
repeat with i=1 to myArraylength if myArrayToBe[i] contains Ftext then put myArrayToBe[i] into Svar answer Svar split Svar by colon put Svar[2] into gvar answer gvar end if end repeat switch pItemName put gvar into pitemName case gvar answer Ftext break case "Choice 2" answer "bye" break case "Choice 3"answer "Please" break end switch end menuPick
Hard to see in your question what you are asking for, but you can set the menu options by using the text of button
If you want to change the menu on the fly when the user clicks, you can do that in the on mouseDown handler:
on mouseDown
set the text of me to "One" & return & "Two" & return & "three"
end mouseDown
if you then have a global variable sVar and would like to populate the menu just when it is about to be shown you can do that also:
on mouseDown
global sVar
put sVar into tVar # Copy array
combine tVar with return
set the text of me to tVar
end mouseDown
If you want to change the first two alternatives based on an array sVaryou can use:
put the text of button "myMenuButton" into tText
put sVar[1] into line 1 of tText
put sVar[2] into line 2 of tText
set the text of button "myMenuButton" to tText