Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Using the automator is possible to execute a shell script that requires input from the user (a password) ? The shell script should be executed in the currently selected folder. I wrote a "Run Shell Script" action like in the following figure:

enter image description here

About the shell actions:

cd $1
~/scripts/pgpsign

First I change to the current selected folder and then I execute my script, that as far as I understand is failing since it requires processing an input before proceeding.

Below the complete code of the script (in Scala)

#!/bin/sh
exec scala "$0" "$@"
!#

import sys.process._

object PGPSign {
  def main(args: Array[String]) {
    print("Please type passphrase : ")
    val passPhrase = readLine()
    signFilesInDirectory(new java.io.File("."), passPhrase.toString)
  }

  def signFilesInDirectory(dir: java.io.File, passPhrase: String) {
    if(!dir.exists())
      throw new java.io.FileNotFoundException
    if(!dir.isDirectory())
      throw new RuntimeException("Expecting directory")
    println("Signing files in directory: " + dir.getAbsolutePath())
    for{file <- dir.listFiles 
        if !file.isDirectory //ignoring directories
        val fileName = file.getName()
        if !fileName.startsWith(".") //ignoring hidden files
        if !fileName.endsWith(".asc") //ignoring signature files
    } { 
      println("- Signing file " + file)
       ("gpg -ab --yes --batch --passphrase " + passPhrase + " " + fileName).!!
    }
  }
}
share|improve this question
    
Can you post a screenshot of your full automator workflow (or at least the full Run Shell Script action)? Also, make sure the Pass input on the Run Shell Script action is set to as arguments. –  robmathers Oct 30 '12 at 22:45
    
yes I just added a figure of the full workflow (only one action) –  Sergio Oct 30 '12 at 22:53
    
Can you post more info on the pgpsign script, specifically what input it expects? It looks like it's expecting input, but you're not giving it any. –  robmathers Oct 30 '12 at 23:22
    
it expects just a passphrase from the user. It requests this passphrase before using it to generate pgp signatures for all the files in the current folder –  Sergio Oct 30 '12 at 23:31
    
trying to understand ... :) I can refactor the script to read from an input stream indeed, but then how can I make the AppleScript to write in that stream ? . BTW could you tell me if the rest of the Automator action looks fine ? I am wondering if the "cd $1" line is really necessary or redundant. Is the default execution path of a shell script the currently selected folder ? –  Sergio Oct 30 '12 at 23:55
show 2 more comments

1 Answer

up vote 2 down vote accepted

I'm not really familiar with the Scala language at all, but it looks like the script is only built to take user input for the password, not arguments or stdin, which is what's required for Automator to pass it data.

The shell that Automator runs scripts in is non-interactive, so your script can't directly prompt the user for a password. If you can rework your script to take a password via stdin, you could use an AppleScript wrapper to get a password dialog, if that's what you're hoping for.

If you rework your pgpsign script to take a password from stdin, you can use an Applescript action (with some shell scripting embedded) to display a password dialog and get the results you desire.

Replace your Run Shell Script action in your current workflow with a Run AppleScript action, with the following code:

on run {input, parameters}
    set thePath to quoted form of POSIX path of first item of input as string
    tell application "System Events"
        display dialog "Password:" default answer "" with hidden answer
    end tell
    set pass to text returned of result
    do shell script "cd " & thePath & "; echo " & quoted form of pass & " | perl -ne 'chomp and print' | ~/scripts/pgpsign"
end run

It's not the most straightforward, so I'll run through line by line what it's doing.

on run {input, parameters}
    set thePath to quoted form of POSIX path of first item of input as string

This gets the input from the service (the folder you selected) and turns it into a string that the shell script can make use of.

tell application "System Events"
    display dialog "Password:" default answer "" with hidden answer
end tell
set pass to text returned of result

This pops up a password dialog and stores the result to the pass variable. Ordinarily in AppleScript you don't need the System Events tell portion, but because of some quirks with Automator, it's required here.

do shell script "cd " & thePath & "; echo " & quoted form of pass & " | perl -ne 'chomp and print' | ~/scripts/pgpsign"

This ties it all together and sends the required parameters to your script. First we change to the selected directory (to answer your question in the comments, as your script is written, it is necessary to first cd to the directory you want). Then echo is used to send the password to stdin, and it's piped through the perl portion to strip the trailing newline (which is an annoying characteristic of AppleScript and would otherwise cause a valid password to fail), then piped to your script.

Sorry for giving you a lot to digest, but if you want to use Automator for this, it's probably the easiest way, unless you want to hardcode your password (which is obviously inadvisable for security reasons).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.