How could I pass shell script output to notification? - macos

I would like to show result of shell script run (exiftool -k -P -overwrite_original_in_place -ImageDescription= "$1"/*.ARW) in notification. I've tried to use $1 as text in the subject, but it shows exactly $1 instead of script output.

There needs to be a Set Value of Variable action in between the script and notification actions.
Set Value of Variable accepts input from previous action and assigns it to a variable with the name of your choice.
This variable is then available for other actions. Just start typing the name of the variable in a field and it should pop-up in the auto-suggestion.

In 10.9 or later, you can use the display notification AppleScript command:
osascript -e 'on run {t}' -e 'display notification "" with title "t"' -e end title
In 10.8 you can use terminal-notifier:
terminal-notifier -title title -message ''
terminal-notifier can be installed with sudo gem install terminal-notifier.

Related

Displaying an Xcode variable in a shell script in an alert fails to display the alert

I have a shell script that I run from Xcode to open the root folder in the Terminal. It works as expected when invoked by a function key in Xcode and opens a Terminal window pointing to the value of $SRCROOT as defined in Xcode and an alert with the expected message appears before that.
#! /bin/bash
osascript -e 'display dialog "message" with title "Hi"'
open -a Terminal "$SRCROOT"
Yet when I try to replace "message" to display the contents of $SRCROOT, the dialog doesn't display at all. I've tried all of the approaches listed in the solutions here: osascript using bash variable with a space
Using any of those approaches results in the alert not displaying at all.
I even tried to display the contents of $SRCROOT in a notification with various methods of escaping it but that just displays an empty notification.
Any ideas? TIA.
Save following in test.sh :
#!/usr/bin/env bash
SRCROOT=~/Developer/SwiftVC
osascript \
-e "on run(argv)" \
-e 'display dialog item 1 of argv with title "Hi"' \
-e "end" \
-- "$SRCROOT"
open -a Terminal "$SRCROOT"
and run it with :
chmod +x test.sh
./test.sh

Send echo command to an external xTerm

I have a bash script, and I want to be able to keep a log in an xterm, and be able to send echo to it anytime.
How would I do this?
Check the GPG_TTY variable in your xterm session. It should have the value similar to
GPG_TTY=/dev/pts/2
This method should be available for terminals that support GNU Pinentry.
Another option to determine the current terminal name is to use
readlink /proc/self/fd/0
The last method applies only to Linux
Now if your bash script implements a command
echo "Hello, world!" > /dev/pts/2
This line should appear on the xterm screen.
I managed to make a console by running an xterm with a while loop clearing the screen, reading the contents of the log file, pauseing for a second, then looping again. Here was the command:
xterm -T Console -e "while true: do cls && cat ${0}-LOG.txt && sleep 1; done"
Then to send something to the console:
echo -e "\e[91;1mTest" >> ${0}-LOG.txt
And the console will update each second.

OSX Bash Shell Script Does Not Run

I'm having a pretty basic shell script that's supposed to run at user login. To achieve this, I followed the guide for Automator from the first answer in this topic: Running script upon login mac
But somehow nothing happens. I also tried to create an application using the script editor with do shell script /blabla/git_credentials.sh which responded with a permission denied.
I don't see whats wrong here.
Oh, here's the script:
echo ""
echo "Setup Git Credentials"
echo "*********************"
echo "Please enter your first name: "
read fname
echo "Please enter your last name: "
read lname
echo "Please enter your mail address: "
read email
git config --global --remove-section user
git config --global user.name "$fname $lname"
git config --global user.email $email
echo "Credentials set."
Edit: I just found out that the script is being run at login, but it neither opens up a terminal nor waits for my user inputs, I have just an empty Git config after every startup. I 'achieved' this using the script editor with do shell script "$HOME/git_credentials.sh" and saving it as an application, then putting it into the login items.
The problem is that your Automator's shell script isn't connected to STDIN (i.e. your keyboard). It may run the shell script, but there's no way to pass it input since there's no terminal.
What you need to do is run the Automator action: Ask for Text to get your input.
What I found I had to do was Ask for Text, and then Set the Value of the Variable. I do this for each variable I want as input.
Once I get all of the variables I want, I then run Get the Value of the Variable for each of the variables. This puts the variables into $* for the shell script to pull up.
Now, you can execute the Automator action Run Shell Script with Pass Input as arguments. You can refer to them as $1, $2, etc.
I suggest to try this with a simple script and see if it then works. The problem is that the whole thing may execute in a sub-shell, so once the automator action ends, you lose the values of the variables you've set. I simply don't have enough experience with Automator to know exactly how it works.
I suspect you script is not currently executable.
Try fixing this by running:
chmod +x /blabla/git_credentials.sh
or
chmod 755 /blabla/git_credentials.sh
Or you are missing #!/bin/bash at the top of your script? Or is this just a part of it?

How AppleScript can get STDIN inside the code?

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.

How to tell OS X not to "wait" during a bash script?

I've set up this script:
#!/bin/bash
/Applications/NameChanger.app/Contents/MacOS/NameChanger "$#"
osascript -e "delay 1" -e "tell application \"NameChanger\" to activate"
I'm using it to pass file names to NameChanager. Without the second line it loads NameChanger unfocused. I thought I should use a delay and then activate with applescript to get it focused.
Unfortunately the script is "waiting" for NameChanger to run and then to exit before executing the applescript bit. How can I change that?
Alternatively you can use the open command to launch NameChanger. This should also automatically bring NameChanger to the foreground:
#!/bin/bash
open /Applications/NameChanger.app --args "$#"
Append a & at the end of commands in a shell script that you want to run in the background.
/Applications/NameChanger.app/Contents/MacOS/NameChanger "$#" &

Resources