Jump to content
pauljmartinez

Keyword to Terminal (Interactive Script)

Recommended Posts

My goal is to automate the process of updating a user's hosts file. (/etc/hosts/)

 

I have a bash script that works fine if I run it from my home directory.

  • User opens Alfred and types in the keyword "hostsup" (No arguments required)
  • Alfred opens a Terminal window and runs a script that has been added to the Workflow folder
  • The script displays some informative text for the user
  • Then the scripts ask the user if they would like to continue
  • On 'y', the terminal prompts the user for their password (Because the command uses sudo)
  • The script outputs a completion message and instructs the user to close the Terminal window

If I use "Keyword to Script", I can embed the script as part of the Workflow and run it, but it does not run interactively, so the user receives no prompts or messages.

 

If I use "Keyword to Terminal Command", I can run a simple bash "one-liner", but I can't figure out how to call the script that I've included in the Workflow folder.

 

Any help would be greatly appreciated.

Share this post


Link to post

I don't think you can do that with a Terminal Command.
 
The fundamental problem is that Alfred pastes the command in whichever directory Terminal opens at. Your script has no filepath, so it has no idea where to find the other script.
 
The only way around it is to call Terminal yourself from a script that exists on disk (so it knows where to look for the other script). This AppleScript will call a shell script in its own directory, either by full path or by changing directory.

-- Change working directory to parent dir of this script
-- before running the shell script
property cwd : true

on run (argv)
	set theScript to first item of argv
	set parentDir to POSIX path of ((path to me as text) & "::")
	if cwd is true then
		tell application "Terminal" to do script "pushd " & quoted form of parentDir & " && " & quoted form of theScript & "; popd"
	else
		set thePath to parentDir & theScript
		tell application "Terminal" to do script quoted form of thePath
		log "thePath : " & thePath
	end if
end run

If you save it as, say, run-script-in-terminal.scpt in your workflow, then you can use it in a /bin/bash Run Script Action to call other scripts:

osascript run-script-in-terminal.scpt ./myscript.sh

If you set cwd at the top of the AppleScript to true, it will tell Terminal to change to the AppleScript's own directory before calling the next script. Set it to false and it will call the script by its full path from whichever directory Terminal opens at.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...