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).
Best Answer
The example shell script code used in this answer was tested under macOS Catalina 10.15.4.
As coded the location the RAR archive is to be created/extracted to must be readable/writable to you with normal privileges, otherwise the RAR archive is created/extracted on your Desktop. If a selected file/folder to be archived is not readable it will throw an error message and an incomplete RAR archive may be created.
The two Automator Quick Actions work as advertised with the settings as shown in the images of the workflows, further below, so use the same settings!
NOTE: In order for these workflows to work, Finder must be given Full Disk Access in: System Preferences > Security & Privacy > Privacy
NOTE: If
rar
andunrar
are not properly signed it will throw an error message. Go to System Preferences > Security & Privacy > General, click the lock to make changes, enter your credentials, click the Allow button. You will still have to grant the permission the next time they run, but not there after.Create RAR Archive
Using how by default in Finder a ZIP archive is created in macOS, only recursing the relative path from within the PWD, the same applies to the example shell script code used in the first Run Shell Script action of the Automator Quick Action shown below.
In this first Automator Quick Action, as coded, the example shell script code will:
Containing the selected file(s)/folder(s).
Extract RAR Archive
Unlike how by default in Finder a ZIP archive is extracted in macOS, incrementing the name of the folder the archive is extracted to if it previously exists, the example shell script code used in this second Run Shell Script action of the Automator Quick Action shown below does not do that! If the folder named for the extracted RAR archive already exists, then it is not extracted a second time.
In you need to extract it again, rename or delete the existing extracted RAR archive, or copy the RAR archive to an alternate location, then extract it.
In this second Automator Quick Action, as coded, the example shell script code will:
Note: The example shell script code is just that and, sans existing error handling, does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.