pauljmartinez Posted October 20, 2015 Share Posted October 20, 2015 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. Link to comment
deanishe Posted October 22, 2015 Share Posted October 22, 2015 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. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now