I have a script like this:
export P4PORT=192.168.1.132:1666
passwd=`security find-generic-password -ws Exchange | /usr/bin/perl -pe "s/\n//g"`
/usr/bin/expect -c "spawn /Users/myname/bin/p4 login; expect \"Enter password:\"; send \"$passwd\r\"; interact"
res=`/Users/myname/bin/p4 login -s`
osascript <<EOF
tell app "System Events" to display dialog "$res" buttons "OK" default button 1
EOF
which runs perfectly when run as a simple shell script.
When I run it inside Automator on Mac OS X, however, it fails to log in.
The bit that seems to fail is the expect one. If I substitute it with doing things inside terminal in applescript the script works fine. this however is quite an ugly solution - it pops up a terminal and does all the typing in there.
Is the issue really with expect? and can it be worked around?
Since this would have to be implemented as a service, it would be neat to keep it that way.
Thanks.
Turns out the problem is similar to the one listed here:
You need to use
expect eof
exit
instead of
interact
It works, but I don't know why.
Related
I want to write a script for Ubuntu, which open a terminal-emulator, which only allows users interact with it only once. After finish running user's first command typed in, the terminal close on itself automatically, which is kind of like Win+R on windows OS.
How should I do that?
I try script like gnome-terminal -- bash -c "read cmd && $cmd", but there's two problem:
No auto-complete on user inputting commands;
Commands from .bashrc, .bash_aliases are not recognized.
You can try :
gnome-terminal -- bash --rcfile <(cat ~/.bashrc; echo 'PROMPT_COMMAND="PROMPT_COMMAND=exit"')
I don't have Ubuntu to test at the moment, but bash ... part worked.
I need to open the Mac Terminal and run some commands with os.execute in Lua
export VAMP_PATH=/path/to/plugin/directory
cd /path/to/script
./sonic-annotator -l
EDIT: got it to work without terminal with this
os.execute('export VAMP_PATH="'..script_path..'sonic/mac64/vamp"; cd "'..script_path..'sonic/mac64/"; ./sonic-annotator -d vamp:qm-vamp-plugins:qm-barbeattracker:beats -w csv "'..filename..'"')
To answer your actual question, you can start a Terminal and run some bash commands in it like this:
os.execute("osascript -e 'tell application \"Terminal\" to do script \"cd /Users/mark && ls\"'")
But, as I said in the comments, you don't necessarily need a Terminal to run a script, so you can just run a command like this:
os.execute("export V=fred; cd /Users/mark && ./SomeScript.sh")
If you are running a script because you just want the user to see the output of the script, it is often easier and involves far less quoting if you run your command and pipe the result to open -f like this, which displays the output in a text editor:
os.execute("cd /Users/mark; ls | open -f")
First of all, sorry for my english, not a native speaker.
Since I'm a bit fed up opening Termninal and ^C I want to create a shortcut for activating caffeinate -d on my Mac OS X 10.11
I've been trying a simple Automator Service but with my noob skills it doesn't work.
This is the settings:
I chose Service as type of document
Library->Utilities->Run Shell Script
Service receives: no input
in: any application
Shell: /bin/bash
Pass input: to stdin
CAFFEINATECHECK=`ps | grep caffeinate | cut -d ' ' -f7,8`
if [ $CAFFEINATECHECK == 'caffeinate -d' ]; then
killall caffeinate
else
caffeinate -d
fi
Problem is that such a short scrip keeps running with no response and I have to stop it after a minute
I hope someone could give me any tips necessary
I got the solution in a duplicate on Ask Different
In short, just replacing mine code with a more elegant command lines:
if [[ $(pgrep caffeinate) == "" ]]; then
caffeinate -d &
else
pkill caffeinate
fi
The only change I made inside user3439894's solution was to remove the '&' (usually useful at the end of a command line to run it in background) because apparently Automator doesn't work well with it (shame I can't remember where I read/heard that..!) .
After saving the Service I just had to
Open System Preferences Keyboard->Shortcuts->Services
Bind my shortcut with my saved workflow found under ▼General in the right pannel
This way a cog gear wheel ⚙ appears on the menubar when caffeinate shortcut is activated and spins till I deactive it .
Google suggests
echo "input" | osascript filename.scpt
with filename.scpt
set stdin to do shell script "cat"
display dialog stdin
However, I could get only blank dialog: it has no text. How can I get stdin from AppleScript at the version?
My OS version is OSX 10.8 Mountain Lion.
Sorry, not enough reputation to comment on answers, but I think there's something important worth pointing out here...
The solution in #regulus6633's answer is not the same as piping data into osascript. It's simply stuffing the entire pipe contents (in this case, echo output) into a variable and passing that to osascript as a commandline argument.
This solution may not work as expected depending on what's in your pipe (maybe your shell also plays a part?)... for example, if there are null (\0) characters in there:
$ var=$(echo -en 'ABC\0DEF')
Now you might think var contains the strings "ABC" and "DEF" delimited by a null character, but it doesn't. The null character is gone:
$ echo -n "$var" | wc -c
6
However, using #phs's answer (a true pipe), you get your zero:
$ echo -en 'ABC\0DEF' | osascript 3<&0 <<EOF
> on run argv
> return length of (do shell script "cat 0<&3")
> end run
>EOF
7
But that's just using zeros. Try passing some random binary data into osascript as a commandline argument:
$ var=$(head -c8 /dev/random)
$ osascript - "$var" <<EOF
> on run argv
> return length of (item 1 of argv)
> end run
>EOF
execution error: Can’t make some data into the expected type. (-1700)
Once again, #phs's answer will handle this fine:
$ head -c8 /dev/random | osascript 3<&0 <<EOF
> on run argv
> return length of (do shell script "cat 0<&3")
> end run
>EOF
8
According to this thread, as of 10.8 AppleScript now aggressively closes standard in. By sliding it out of the way to an unused file descriptor, it can be saved. Here's an example of doing that in bash.
Here we get at it again with a cat subprocess reading from the magic fd.
echo world | osascript 3<&0 <<'APPLESCRIPT'
on run argv
set stdin to do shell script "cat 0<&3"
return "hello, " & stdin
end run
APPLESCRIPT
Will give you:
hello, world
I know that "set stdin to do shell script "cat"" used to work. I can't get it to work in 10.8 though and I'm not sure when it stopped working. Anyway, you basically need to get the echo command output into a variable which can then be used as an argument in the osascript command. Your applescript needs to handle arguments too (on run argv). And finally, when you use osascript you must tell an application to "display dialog" otherwise it will error.
So with all that said here's a simple applescript which handles arguments. Make this the code of filename.scpt.
on run argv
repeat with i from 1 to count of argv
tell application "Finder"
activate
display dialog (item i of argv)
end tell
end repeat
end run
Here's the shell command to run...
var=$(echo "sending some text to an applescript"); osascript ~/Desktop/filename.scpt "$var"
I hope that helps. Good luck.
Late to this, but the original AppleScript seems to try to do something not allowed with osascript.
If in the original filename.scpt this line:
display dialog stdin
Is changed to:
tell application "System Events" to display dialog stdin
Then passing a value via stdin (as opposed to command line arguments) definitely still works in 10.7.5 Lion, maybe 10.8 Mountain Lion too.
I am running Ubuntu 11.10 (Unity interface) and I created a Bash script that uses 'gnome-open' to open a series of web pages I use every morning. When I manually execute the script in the Terminal, the bash script works just fine. Here's a sample of the script (it's all the same so I've shortened it):
#!/bin/bash
gnome-open 'https://docs.google.com';
gnome-open 'https://mail.google.com';
Since it seemed to be working well, I added a job to my crontab (mine, not root's) to execute every weekday at a specific time.
Here's the crontab entry:
30 10 * * 1,2,3,4,5 ~/bin/webcheck.sh
The problem is this error gets returned for every single 'gnome-open' command in the bash script:
GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
GConf Error: No D-BUS daemon running
Error: no display specified
I did some searching to try and figure this out. The first thing I tried was relaunching the daemon using SIGHUP:
killall -s SIGHUP gconfd-2
That didn't work so I tried launching the dbus-daemon using this code from the manpage for dbus-launch:
## test for an existing bus daemon, just to be safe
if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
## if not found, launch a new one
eval `dbus-launch --sh-syntax --exit-with-session`
echo "D-Bus per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
fi
But that didn't do anything.
I tried adding simply 'dbus-launch' at the top of my bash script and that didn't work either.
I also tried editing the crontab to include the path to Bash, because I saw that suggestion on another thread but that didn't work.
Any ideas on how I can get this up and running?
Here is how the problem was solved. It turns out the issue was primarily caused by Bash not having access to an X window session (or at least that's how I understood it). So my problem was solved by editing my crontab like so:
30 10 * * 1,2,3,4,5 export DISPLAY=:0 && ~/bin/webcheck.sh
The "export DISPLAY=:0" statement told cron which display to use. I found the answer on this archived Ubuntu forum after searching for "no display specified" or something like that:
http://ubuntuforums.org/archive/index.php/t-105250.html
So now, whenever I'm logged in, exactly at 10:30 my system will automatically launch a series of webpages that I need to look at every day. Saves me having to go through the arduous process of typing in my three-letter alias every time :)
Glad you asked!
It depends on when it is run.
If the Gnome GDM Greeter is live, you can use the DBUS session from the logon dialog, if you will. You can, e.g., use this to send notifications to the logon screen, if no-one is logged in:
function do_notification
{
for pid in $(pgrep gnome-session); do
unset COOKIE
COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ|cut -d= -f2-)"
GNUSER="$(ps --no-heading -o uname $pid)"
echo "Notifying user $GNUSER (gnome-session $pid) with '$#'"
sudo -u "$GNUSER" DBUS_SESSION_BUS_ADDRESS="$COOKIE" /usr/bin/notify-send -c "From CRON:" "$#"
done
unset COOKIE
}
As you can see the above code simply runs the same command (notify-send) on all available gnome-sessions, when called like:
do_notification "I wanted to let you guys know"
You can probably pick this apart and put it to use for your own purposes.