Jump to content

Is there a way to map output actions to specific items without having to use action modifiers?


Recommended Posts

Is there a way to map output actions to specific items without having to use action modifiers?

 

I have a script filter that I am using in alfred and I want each item to have its own return output. 

For example, if I press enter on the first item, I want it to copy to the clipboard and post a notification.

I want the second and third item to not return anything.

I want the third item to launch terminal when the user hits enter.

I want the fourth item to launch a webpage when the user hits enter.

IP=$(host google.com | cut -c 30-)
CITY=$(curl http://ipinfo.io/$IP/city)
STATE=$(curl http://ipinfo.io/$IP/region)
PROVIDER=$(curl http://ipinfo.io/$IP/org | cut -c 8-)

//Start Display
cat<<EOB
<?xml version="1.0"?>
<items>


//Remote Server's IP
<item arg="$IP" valid="NO">
<title>Remote Server's IP: $IP</title>
<subtitle> Press Cmd + C to copy </subtitle>
<subtitle mod="cmd">Cmd + C to Copy</subtitle>
<text type="copy">$IP</text>
<icon>69288C18-2A0D-4430-837E-4AB331CD2698.png</icon>
</item>

//Location (City, State, Country)
<item arg="$CITY, $STATE" valid="NO">
<title>Location: $CITY, $STATE</title>
<subtitle>This is Location of the Server</subtitle>
<icon>icon.png</icon>
</item>

//ISP Provider
<item arg="$PROVIDER" valid="NO">
<title>Provider: $PROVIDER</title>
<subtitle>This is the ISP Provider</subtitle>
<icon>Wired_Network-100.png</icon>
</item>

//SSH into server
<item arg="$IP">
<title>SSH into Server</title>
<subtitle>Press Enter to connect</subtitle>
<icon>Connected-100.png</icon>
</item>

//Open Web Page
<item arg="$IP">
<title>Connect to Web Server</title>
<subtitle>Press Alt + Enter to connect</subtitle>
<subtitle mod="alt">Launch Webpage</subtitle>
<icon>Open in Browser-100.png</icon>
</item>


</items>

EOB 
Link to comment

Short answer: no.
 
Long answer: yes, but not like that. To perform different actions on different items, you connect them all to a single script that can perform the required actions and alter arg to specify the command as well as the argument.
 
So you change arg="$IP" to arg="copy $IP" or arg="open $IP" as appropriate. Connect that to a Run Script action and turn of Escaping. Then you can treat {query} as two arguments:
 

function perform_action() {
    local action="$1"
    local ip="$2"
    if [[ "$action" -eq "copy" ]]; then
        echo "$ip" | pbcopy
    else
        open "http://$ip"
    fi
}
 
 
perform_action {query}
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...