How to Turn on/off Wifi using Keyboard shortcut - syntax

I tried creating an automator to create a shortcut to Toggle Wifi using keyboard shortcut.
But I get this error message:
Syntax Error
A “{” can’t go after this “)”.
This is the script I ran:
set_wifi_on_or_off() {
networksetup -getairportpower en${n} | grep ": ${1}";
if test $? -eq 0;
then
echo WiFi interface found: en${n};
eval "networksetup -setairportpower en${n} ${2}"
return 0;
fi
return 1;
}
for n in $(seq 0 10);
do
if set_wifi_on_or_off "On" "off"; then break; fi;
if set_wifi_on_or_off "Off" "on"; then break; fi;
done
please help

The script you've supplied is written in shell script (more specifically, bash or a close relation). It would be more helpful to post a screen shot of your Automator workflow, given that the script itself seems syntactically fine. However, you've tagged this question with applescript, so here's an AppleScript solution:
use framework "CoreWLAN"
on WiFiOn:state
tell my CWWiFiClient's sharedWiFiClient()'s interface()
if (state as {boolean, anything}) ¬
is not in [true, false] ¬
then return powerOn()
setPower_error_(state, [])
end tell
WiFiOn_(null)
end WiFiOn:
This handler takes a single parameter, state. If the value passed to it is some equivalent of the boolean values true or false (these include true, yes, and 1; and false, no, and 0), then this sets the state of the WiFi to on (true) or off (false). Any other value passed to the handler performs a query on the state of the WiFi, returning true or false to indicate whether the WiFi is currently on (true) or off (false).
Therefore, to perform a toggle, you can first perform a query, then set the state to the boolean negation of the result:
set currentState to WiFi_(null)
WiFi_(not the currentState)
This handler and these two lines above need to be pasted into a single Run AppleScript action. There should be no other actions in the workflow. The workflow should be set to receive No Input in any application.
Save the workflow as a Quick Action (Service), and assign it the shortcut key combo of your choice.

Related

Catch multiple exceptions with specific error messages leading to one exit command

I'm trying to create a program that prompts the user for their weight in pounds as a float, when entered I need to check the entry to ensure that it is not a string or below the value of 10. It needs to be done in an if statement and not a loop, as well I need to be able to end multiple exceptions with one quit() statement instead of multiple quit() statements for each exception and then continue the program further if the user entered the input in the correct parameters.
This is what I have so far:
isvalid = float,int
try:
weight_pounds = float(input("Enter your weight in pounds: ")
if weight_pounds != isvalid:
print("Cannot be alphabetical.")
elif weight_pounds < 10:
print("Cannot be less than 10.")
else:
input("Press enter to exit...")
quit()
I am still learning basic functions of python and I may be missing something simple here but I've tried many ways to get this to work and I can't seem to get it working without either a dead end or ValueErrors.

how to know if a GTK window is minimized in ruby

i want to know if the window is minimized or not. i have connected window-state-event signal from GtkWidget to this function
def on_main_window_hide(object, event)
if event.changed_mask & Gdk::WindowState::ICONIFIED
if event.new_window_state & Gdk::WindowState::ICONIFIED
puts("minimize" + $counter.to_s)
$counter+=1
else
puts ("unminimize")
end
end
end
and even after doing minimizing and unminimizing couple of times .. it never prints if the window is unminimized, here is the output
minimize0
minimize1
minimize2
minimize3
minimize4
plus, minimizing gives the window-state-event signal twice, like if minimize0 is initial value then on minimizing it becomes minimize2
how can i detect properly if a window is minimized ?
I think you got confused by the callback name. The window-state-event signal gets emitted for every state event change, so the callback should be called on_main_window_state_event as it will be emitted not only when the window hides. Then you can check the event mask for changes.
Your callback has a redundant if condition. From the API, the changed_mask is a:
mask specifying what flags have changed
And window_new_state is also a mask but contains:
the new window state, a combination of GdkWindowState bits
This means that the second if condition will always evaluate as true if the first one also evaluates as true but the else branch will never happen. If you want your "unminimize" print to occur then you must remove the second if condition and move the print to the else branch of the first if condition. Another option would be to check for the bit mask and really check the status.
Your code should be:
def on_main_window_state_event(object, event)
if event.changed_mask & Gdk::WindowState::ICONIFIED
puts("minimize" + $counter.to_s)
$counter+=1
else
puts ("unminimize")
end
end

Play sound if space is not pressed in Psychopy

I am trying to get a sound to play if 'Space' is not pressed AND if time is >1s, and to stop it if 'Space' is pressed
alarm = sound.Sound(700, secs = 5)
if (len(event.getKeys())==0) & (time1.getTime() > 1):
alarm.play()
elif event.getKeys(keyList = 'space'):
alarm.stop()
However, when I do this, I cannot press 'space' to stop the alarm.
Can anyone tell me what I am doing wrong here? Is there any thing wrong with the '(len(event.getKeys())==0)' portion?
In Matlab, I can just write
if ~KbCheck && .....
but I am not sure how to do it in Psychopy.
Thanks!
Use event.getKeys(keyList=['space']). The keyList is a list, not a string. Also, change the order of your logic since the first call to event.getKeys() clears the keyboard buffer for all keys, since no keyList was provided so that nothing is left to be registered in your elif.
So this should do the trick:
if event.getKeys(keyList = ['space']):
alarm.stop()
elif time1.getTime() > 1 and not event.getKeys(): # if another key was pressed... #added brackets here
alarm.play()
As you can see, I also simplified the syntax for one of the tests.

Powershell validation of read-host data entry in while loop with nonewline

I have a function which inserts a Y or N menu when called within my Powershell scripts. It uses a while loop to validate that either a Y or N value is entered. Everything works fine, however a new line is created each time an error is made. I could use cls and redisplay everything, but this is not the most ideal solution. Instead, I would like to find a way to redisplay the read-host prompt on the same line while clearing any previously entered answer. Here is my existing code:
# Begin function to display yes or no menu
function ynmenu {
$global:ans = $null
Write-Host -ForegroundColor Cyan "`n Y. [Yes]"
Write-Host -ForegroundColor Cyan "N. [No]`n"
While ($ans -ne "y" -and $ans -ne "n"){
$global:ans = Read-Host "Please select Y or N"
}
}
# End function ynmenu
I have a few other dynamically populated menus which leverage this methodology. Finding a solution to this would resolve the issue with those as well.
I don't think there's any simple way to do that.
But for a yes/no response, you can use $PSCmdlet.ShouldContinue($Query, $Caption) instead, as long as the scope you're in (function, script, etc.) defined the attribute [CmdletBinding(SupportsShouldProcess=$true)]. This shows an appropriate yes/no prompt in ISE and in the console host and avoids manual processing.

How to have procedural code wait for user input from a GUI before continuing?

In my program there is a complex calculation that requires the user to evaluate intermediate results. This works well in a command line application (which is what my code looks like now) because the interactive prompt halts the program execution until the user hits enter. The command line code looks something like this:
def calculate(starting):
result1 = initial_calculation(starting)
user_input1 = input("What is your choice for " + result1 + "?")
result2 = calculate1(user_input1)
user_input2 = input("What is your choice for " + result2 + "?")
result2 = calculate2(user_input2)
#...etc
I would like to provide a more intuitive interface than is possible with the command line by using a GUI and allowing the user to click on a button indicating their choice instead of typing it in. I'm picturing something like this:
def do_something(starting):
result1 = initial_calculation(starting)
#wait for user to press a button indicating their choice?
result2 = calculate1(clicked_button.user_input1)
label.text("What is your choice for " + result1 + "?")
#wait for user again
result2 = calculate2(clicked_button.user_input2)
#...etc
Is there a way I can pause the execution of the procedural code that does the calculations and then resume the code after the user clicks a button? I'm not really sure how to raise or handle events here because normally in a GUI the control callbacks are the starting point for code execution, but here, the code starts elsewhere and needs to yield to and resume from the GUI.
(If it makes a difference, I am using Python and wxPython.)
General solution:
flag = false;
your thread:
while (!flag); // wait here, consume cpu doing nothing
gui thread:
void OnInputEvent() { flag = true; }
I'm no python programmer, but in general that means you'll need to occasionally wait for the GUI thread until that thread has received input from the user.
There may well be a more succinct, pythonesque api available, but you can certainly use raw python events.

Resources