The argument for osascript -e
can contain newlines:
osascript -e 'set x to "a"
say x'
You can also specify multiple -e
arguments:
osascript -e 'set x to "a"' -e 'say x'
Or if you use a heredoc, bash
interprets three characters (\
, $
, and `
) between <<END
and END
but no characters between <<'END'
and END
.
osascript <<'END'
set x to "a"
say x
END
Edit:
Since osascript can operate with a heredoc (ie take input from /dev/stdin) then one can just write the script as a whole file and prepend with the correct shebang line:
#!/usr/bin/env osascript
set x to "a"
say x
This also allows you to save your apple script as a actual program in ~/Applications/.app using the following procedure (changing for your script's name):
mkdir -p ~/Applications/<APP_NAME>.app/Contents/MacOS
touch ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
open -A TextEdit ~/Applications/<APP_NAME>.app/Contents/MacOS/<APP_NAME>
Ensure that both the script file in .../MacOS/ and the matches
Getting information from this answer and this answer:
- Press the Spotlight button at the top right of the screen (Cmd + Space)
- Search
Keychain Access
. Open this
- Open Preferences (Cmd + ,(comma))
- Check
Show Status in Menu Bar
- Press the Spotlight button at the top right of the screen (Cmd + Space)
- Search
Automator
. Select this
- Create new
Service
- Search
Run Applescript
next to the Actions
and Variables
buttons. Double click this
- Make sure the checkboxes are
Service receives no input in any application
- Paste this Applescript:
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "Keychain menu extra")
click
click menu item "Lock Screen" of menu 1
end tell
end tell
Save this Automator as something like Lock screen
(note: code in GIF is different!)
To make a shortcut:
- Press the Apple button at the top left of the screen
- Click
System Preferences
- Click
Keyboard
- Click
Keyboard Shortcuts
tab
- Click
Services
- Find the name of the service you just created
- Click the blank space on the right
- Type your shortcut in
Use your shortcut in any application!
OR...:
- You could buy Alfred
- You could use Cmd+Shift+Eject
Best Answer
Thanks to budakpisang for this:
In Terminal, run
to get your WiFi network device (usually it's
en0
oren1
, depending on the Mac model you have). Substitute it foren1
in the following stepsYou can turn wifi off and on with these commands
Here's a one-liner to toggle between on and off
Create a keyboard shortcut that runs a shell command
Start Automator, and create a new Service.
Set "Service receives selected: to "no input" in "any application".
Add an action named "Run Shell Script". It's in the Utilities section of the Actions Library.
Insert the bash command you want into the text box and test run it using the Run button (top right). It should do whatever the script does (off, on or toggle), and there should be green ticks below the Action.
Save it, giving it a service name you can remember.
Go to System Preferences -> Keyboard, and go to the Shortcuts tab
Go to the Services section, and scroll down to General - you should find your service there. If you select the line, you can click "add shortcut" and give it a keyboard shortcut.