Jump to content
agileadam

Splitting a query into two variables

Recommended Posts

Hello,

 

I'm working on a workflow with a script filer that passes args like this: "12345^^My project name" (where 12345 is the project ID). What's the best way to explode that arg into two separate variables? Surely there's a better way than what I'm doing.

Currently this is the only way I could get it to work:

 

  47oPCUW.png

 

Where the python scripts look like:

import os
import sys
sys.stdout.write(os.environ['project'].split('^^')[0])

Share this post


Link to post

Hi agileadam,

please don't take seriously my way to do it, that's of little use and purely 'academic', and a way to play a little with Alfred to learn new things, but I'm posting it anyway to ask me too if there's a better way...

 

here the picture ( no code indeed! ) :

 

cTStr6Z.png

 

1) the keyword object accepts keyword and input ( something like 123^^progetto )

2) Args and vars stores the input on variable 'project'

3) the replace perform a regex on input (.+)\^\^(.+) and returns $1 ( the first part )

4) Args and vars stores the input on variable 'idp'

5) Args and vars simply outputs again {var:project}

6) the replace perform same regex on input (.+)\^\^(.+) but returns $2 ( the second part )

7) Args and vars stores the input on variable 'name'

8) largetype outputs {var:idp} - {var:name}

 

could this be optimized, or maybe could it be performed in parallel, ot there's a way to set variables from code that we are missing? ( I tryed to put two replace in parallel receiving the same  string and outputting the two different strings to put on the two different variables but I have problems with this approach... )

 

     Giulio

Edited by juliosecco

Share this post


Link to post

Solved   :) (well, it seems to...)

 

here you have the workflow:

 

UbYIzcM.png

 

since a way to set multiple variables seems to be the 'JSON Config' object, I have simply setup a script that outputs a 'JSON config' object, after handling the input and splitting the query as desired...

 

so my working workflow now has;

 

1) a keyword object for the input, so you launch it calling on alfred something like prova2 idproj^^projectName;

2) a script tha handles {query}, splits it with ^^ delimiter and then outputs the JSON to set the variables:

 

sorry, it is php at the moment, it is the language I find more quick along with javascript, here the code:

 

<?php
$query = $argv[1];
$vars=explode('^^',$query);
echo '{
  "alfredworkflow" : {
    "arg" : "{query}",
    "config" : {
    },
    "variables" : {
      "idp": "'.$vars[0].'",
      "name": "'.$vars[1].'"
    }
  }
}'
?>
 
the code simply sets $query to the input,
then puts the splitted values on an array $vars
then echo ( outputs ) the JSON setting the variables
 
3) the large type shows the variables, outputting {var:idp}-{var:name}
 
I'm sure it can be easily rearranged in phyton     :)
 
obviously it should be refined, for example to correctly escape single quotes on project name, or other things that doesn't come in mind at this hour, but it works...
 
now the question is:
am I doing something that works for casuality, or is it a good and stable approach?
 
regards,
 
     Giulio
Edited by juliosecco

Share this post


Link to post

Hello,

 

I'm working on a workflow with a script filer that passes args like this: "12345^^My project name" (where 12345 is the project ID). What's the best way to explode that arg into two separate variables? Surely there's a better way than what I'm doing.

Currently this is the only way I could get it to work:

 

  47oPCUW.png

 

Where the python scripts look like:

import os
import sys
sys.stdout.write(os.environ['project'].split('^^')[0])

That way's perfectly fine, imo, as long as your delimiter isn't going to crop up in the values.

For the sake of "correctness", I usually use JSON, however. You can use Alfred's feedback mechanism to set and use workflow/environment variables, like juliosecco above.

If you don't actually need the workflow variables, and just want to pass more than one argument to the next action, just encode your arguments as JSON and output that (no need for the Alfred-specific format). Alfred will pass the JSON to your next action as {query}, and you decode it back into Python objects.

Share this post


Link to post

I would probably add a check for each value, if its "" to prevent mistakes. For that you can add an easy JSON feedback that is shown in alfred for any case.

 

Other than that like deanishe said, any way is possible. You could even use Applescript, if you work with it in your next step anyway.

There you just have to keep in mind that you cannot work with varg from alfred. Therefore you have to get the {query} and split it in the script itself which isnt a problem tho.

Share this post


Link to post

There you just have to keep in mind that you cannot work with varg from alfred.

You can't? Is this an AppleScript/osascript bug?

Share this post


Link to post

What doesn't work exactly? ARGV and workflow/environment variables are working fine for me with osascript (AS).

Share this post


Link to post

If you set a variable for instance {var:filename}, you can't access it in applescript itself. 

For instance you set first var:filename and then var:destination in one stream, then you want to access it in applescript via {var:filename}

or as varg $filename. It simply doesn't work, or me and also andrew have done something wrong lol

Could also be that this has been fixed now in another version oO 

 

How do you access multiple vars that have been set but at not set as environment Variable?

Like anything else can access the vars that i have set, but not applescript. I solved this by combining the vars a step before 

with a delimiter which i simply split in the applescript itself.

Share this post


Link to post

Effectively, even in other language like php I can't find the way to read a variable from code...

 

if I setup a php script after an input such as:

 

$query = "{query}";
echo $query;
 
I can see the input argument,
 
but if after setting a variable like {var:name}
 
I put a php script script like
 
$query = "{var:name}";
echo $query;
 
what I see as output is not the content of the variable, but the effective string "{var:name}"
 
same thing in javascript
I'm confused at this point
 

Share this post


Link to post

I put a php script script like

 
$query = "{var:name}";
echo $query;

 

Within scripts, Alfred's workflow variables are set as environment variables. This means you don't have to worry about escaping, or modifying the script if switching to using an external editor... in PHP, I believe you can use getenv(varname) to get an environment variable.

 

I'd also recommend you to move away from using the {query} mode and using the argv mode instead for passing in the input, as this is generally much cleaner.

 

Cheers,

Andrew

Share this post


Link to post

Within scripts, Alfred's workflow variables are set as environment variables. This means you don't have to worry about escaping, or modifying the script if switching to using an external editor... in PHP, I believe you can use getenv(varname) to get an environment variable.

 

I'd also recommend you to move away from using the {query} mode and using the argv mode instead for passing in the input, as this is generally much cleaner.

 

Cheers,

Andrew

 

Have you changed something since we've talked about it? 

Nevertheless i can now access environment vars in applescript via this:

set x to (system attribute "filename")

That would be a set "{var:filename}"

 

Awesome that this works now tho! Or that i know this now haha, no workaround anymore :)

Share this post


Link to post

Within scripts, Alfred's workflow variables are set as environment variables. This means you don't have to worry about escaping, or modifying the script if switching to using an external editor... in PHP, I believe you can use getenv(varname) to get an environment variable.

 

Thanks Andrew,

that I was missing...

I have seen that in php I can get the variable value using $_ENV["variableName"] 

 

I'm looking around also for osascript/javascript, but I have the suspect that's not possible, since javascript, AFAIK, does not have any 'Environment variables' access

 

I'd also recommend you to move away from using the {query} mode and using the argv mode instead for passing in the input, as this is generally much cleaner.

 

OK, I'll follow your suggestion

 

thank you!

 

     Giulio

Edited by juliosecco

Share this post


Link to post

Have you changed something since we've talked about it? 

Nevertheless i can now access environment vars in applescript via this:

set x to (system attribute "filename")

That would be a set "{var:filename}"

 

Awesome that this works now tho! Or that i know this now haha, no workaround anymore :)

 

:D I was wondering why I seem to have a magic version of Alfred where AppleScript works just fine…

Edited by deanishe

Share this post


Link to post

I'm looking around also for osascript/javascript, but I have the suspect that's not possible, since javascript, AFAIK, does not have any 'Environment variables' access

 

For the record, in AppleScript: 

set myVar to (system attribute "VARIABLE_NAME")

 

In JavaScript: 

ObjC.import('stdlib')
var myVar = $.getenv('VARIABLE_NAME')

Share this post


Link to post

 

 

For the record, in AppleScript: 

set myVar to (system attribute "VARIABLE_NAME")

 

In JavaScript: 

ObjC.import('stdlib')
var myVar = $.getenv('VARIABLE_NAME')

 

wonderful.

thanks as usual, deanishe!

 

    Giulio

Share this post


Link to post

Thanks for the input everyone.

 

Dean, your post with instructions to "Setting variables" via JSON is exactly what I was googling for before I posted to the forum. I'd much rather set the variables the way you've described than chain together things as I showed originally.

 

I'll play around with the techniques you explained.

 

Thanks again!

Share this post


Link to post

I tend to write my workflows as command-line programs. Combining variables with a library that binds environment vars to command-line options (e.g. envvar VERBOSE=1 is equivalent to --verbose) is a very useful technique.

It allows you to really keep the number of objects and connections in your workflow to a minimum.

Share this post


Link to post

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
×