Using a variable in a message - Lingo - lingo

Can anyone tell me if there is a way for me to use a variable within a lump of code, so that code can be looped to send messages to multiple objects?
For example, if I have 10 buttons and want each to send a variation of the same command 'sendCommandX', with X being the number of the button.
Right now I have 10 separate messages, and each button calls its own, like
on mouseUp
sendCommand1
end
on mouseUp
sendCommand2
end
Each of these 10 sendCommand# messages do the same thing, just with a different number in them.
It would be great if I could use a variable within the call, so I could have one reusable message. Like:
on mouseUp
sendCommandX (X being the number of the button clicked)
end
and then the sendCommandX could use the same variable within, like
on sendCommandX
echo "you clicked button X:
end

send the number as a parameter:
-- on Button 1
on mouseUp
sendCommand 1
end
-- on Button 2
on mouseUp
sendCommand 2
end
-- movie script!
on sendCommand which
-- use 'which' here, e.g.
put "You pressed button " & which
end
I guess your button scripts are cast member scripts?
This code would be better as a behavior, because then you'd only need one script. But it will work ok like this.

Related

Make Logitech script alt tab

Trying to get my random mouse clicker script to Alt tab randomly to another window and follow the first random click, then randomly alt tab back. This is the script im using currently.
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if IsKeyLockOn("capslock")then
if IsMouseButtonPressed(5)then
repeat
PressMouseButton(3)
Sleep(math.random(2000, 940000))
ReleaseMouseButton(3)
until IsMouseButtonPressed(4)
end
end
end
Also its not the end of the world but i cannot shut this script off, with mouse button 4 which is what i wrote it to do. im fairly new to LUA so excuse my noobness. Thanks.
I keep getting script errors when adding another press function
The script checks button4 state very rarely - you should keep button4 pressed for 940 seconds to be sure the script turned off.
To be more responsive, the script should run wait-a-bit + check-button4 loop:
EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
if IsKeyLockOn("capslock")then
if IsMouseButtonPressed(5) then
repeat
PressMouseButton(3)
local tm = GetRunningTime() + math.random(2000, 940000)
repeat
Sleep(10)
until GetRunningTime() > tm or IsMouseButtonPressed(4)
ReleaseMouseButton(3)
until IsMouseButtonPressed(4)
end
end
end
I don't see any attempt to Alt tab simulation in your script.
Post your code.

AppleScript produces no output inside conditional statement

I'm trying to write an AppleScript that tells if application has a specific menubar item. I've found this snippet on the internet, and tried to run, but it doesn't produce any effect. I've spliced into it some debug statements to check the control flow, and it seems not to enter the conditional 'if' at all.
No error message, no output, nothing!
on menuItemExists({appName, menuName1, menuItem1})
display notification "1"
tell application "System Events"
display notification "2"
tell application process appName
display notification "3"
if menu item menuItem1 of menu 1 of menu bar item menuName1 of menu bar 1 exists then
display notification "4"
return true
else
return false
end if
end tell
display notification "5"
end tell
end menuItemExists
if menuItemExists({"timeEdition", "Extras", "Start Recording"}) then
display dialog "hoda"
end if
If we assume that the AppleScript is functioning as it should, then this leads to one conclusion: at least one of those menu or menu item GUI objects does not exist, and therefore the script has nothing to return.
If that conditional statement passed—and only if it passed—then notification 4 would be displayed, and the function menuItemExists would return true. If this function returns true—and only if it returns true—then a dialog box would appear displaying the message "hoda".
However, no such dialog appeared, which implies that menuItemExists did not return true. If it did not return true, then it must have returned false. And if it returned false, this means the condition of some menu item’s existence failed, so no notification after number 3 would have been seen.
I believe you were expecting some output even in the event of failure. However, there is no error in the script, and the only statements that return values are all inside a function handler. That returns its value (which we’ve now deduced was the boolean value false) to the parent script that called it; and it’s only the parent script that would return any value—if it had a value to return—to the output window you were expecting to display a result.
Without having the application you’re using on my computer, it’s not possible for me to determine where in that conditional clause the assertion fails. My advice is to test each object individually, from menu bar 1 through to the menu item in question, and I’m confident the culprit will emerge.
The AppleScript code in your OP is only going to return true and trigger the display dialog "hoda" if and only if timeEdition is running and is not actively recording. When it's actively recording the menu shows Stop Recording. If timeEdition is not running, you can add tell application "timeEdition" to activate to the top of the script if you want the handler to result in true and display "hoda" in a dialog box when it's running and not actively recording.
display notification "5" will never trigger because of the use of return with both true and or false triggering prior to it.
I installed timeEdition and tested the AppleScript code in your OP and it does return false in Script Editor if it's not running. If running and not actively recording, it does trigger display dialog "hoda".
So, the AppleScript code in your OP actually works just as coded, although how it's coded is not of any practical use other then some testing, until its recoded for a real world use case scenario.

How to open a Glade-created window and wait for it to close, in Ruby?

I want to make my own window, using Glade (3.14.2).
At a certain point in my program, I want to
1) Put up the window and let the user do stuff
2) Wait for it to close
3) Get values from the window object
4) Continue on in my code
So basically, I want to treat the window like a modal dialog - but one that I write and control.
I've tried for a few hours. The window appears just fine, as designed in Glade. The user can interact with it.
When the window closes, code that's been connected with signal_connect('destroy') executes.
But the code that invoked the window's show() method... does not continue executing after the window closes.
class GrammarNodeEditor
#this makes the class visual:
include GladeGUI
def initialize(raw_node = nil, &close_block)
#raw_node = raw_node || {type: :Sequence, data: []}
#original_data = #raw_node[:data]
#close_block = close_block
end
def show
puts "GNE Window Opening"
load_glade(__FILE__)
#builder["window1"].title = "Edit/Create Grammar Node"
#builder["window1"].signal_connect('destroy') {|*args|
#close_block.call(self)
puts "GNE WINDOW DESTROY"
}
show_window()
puts "Done showing window"
end
Here is how I invoke it:
rhs_editor = GrammarNodeEditor.new {|obj|
puts "In closeblck, obj is #{obj.inspect}"
#rhs = obj.raw_node
}
puts "About to call show in GR:Init"
rhs_editor.show
puts "Back from calling show in GR:Init"
Here is the output:
About to call show in GR:Init
GNE Window Opening
In closeblck, obj is #<GrammarNodeEditor:0x7b82a88 #raw_node={:type=>:Sequence, :data=>[]}, [more junk here]>
GNE WINDOW DESTROY
The first two lines of output appear after I open the window. The 3rd and 4th appear when I close the window.
Note that "Done showing window" and "Back from calling show in GR:Init" are not printed at all.
Just to make this a little more interesting, I want to be able to do this from within code that puts up another window. My top-level window has a button to create a new Rule. The Rule must be initialized with a Node, and then the Rule must be edited. So first I need to put up a Node-definition window (as shown above) and then, when I have a Node defined, I want to put up a Rule window that uses that Node.
So I think I need to call this code within either the initialize() or the show() method of the GrammarRuleWindow class (another Glade-defined window).
Can someone explain why my puts's aren't being printed, and how to make the control flow go on through them?
Thanks!
...So it turned out the problem was that I had created the window's .glade file directly in Glade, rather than using the VisualRuby IDE.
Creating the .glade in VR adds some stuff to the file that VR needs. Specifically, the file needs to contain the line
<signal name="destroy" handler="destroy_window" swapped="no"/>
before the first <child...> tag.

How to return to same place in for loop with multiple windows in Cocoa

I have a for loop editing an array of model objects, some of which require user input and some of which do not. I would like to be able to open a second window as needed to display multiple choices to the user, receive the user's selection, close the second window, and then return to the place I was in the original loop. In AppleScript, this could be done simply with a "choose from list" dialog. But I haven't figured out how to do it in Cocoa since the NSNotificationCenter architecture requires me to process the second window's response in a different selector. Thus the loop is broken and I can't process the rest of the array. I also tried doing it with delegation to no avail. Are panels and alerts the only way to do this?

Removing a slot on a child button click

I've been trying to remove a slot from a child button click, but I can't seem to get it to work. E.G.
flow do
button("X") {parent.remove}
end
Any suggestions?
Shoes' blocks are sometimes tricky. The key here is to ask yourself, what is the parent method being called upon? self, of course. And self references the app (or window, or dialog), not the button.
There are two similar ways to get around this. First, you can create a reference to the button to use in the block:
flow do
a = button("X") {a.parent.remove}
end
Or, you could just reference the slot itself:
b = flow do
button("X") {b.remove}
end

Resources