LiquidIce Posted December 15, 2014 Share Posted December 15, 2014 Hello everyone, I am trying to create a new alfred workflow that what it basically does is: Alfred has an array stored with NAME and VALUE, I then type in a keyword + NAME, i want it to open an URL with the VALUE of that NAME. For example: Lets example a wikipedia search: I type in openup linux and what this should do is: Check array --> NAME = linux? Check the value of linux, VALUE = Linux --> Open URL "http://en.wikipedia.org/wiki/Linux" It may seem hard to explain but its quite simple in mind hehe, Anyone have any ideas? Kind regards, Link to comment
Vero Posted December 15, 2014 Share Posted December 15, 2014 Hello everyone, I am trying to create a new alfred workflow that what it basically does is: Alfred has an array stored with NAME and VALUE, I then type in a keyword + NAME, i want it to open an URL with the VALUE of that NAME. Hi there, could you please fill in your Powerpack email address in your forum profile first, as you're asking for Powerpack-related help? Thanks, Vero Link to comment
vitor Posted December 15, 2014 Share Posted December 15, 2014 Not sure about php, but in ruby you could do something quick like keyword = '{query}'.downcase urls = { 'linux' => 'http://en.wikipedia.org/wiki/Linux', 'ruby' => 'https://www.ruby-lang.org/' } system 'open', urls[keyword] Link to comment
rice.shawn Posted December 15, 2014 Share Posted December 15, 2014 Here's Vítor's Ruby code as PHP: $keyword = strtolower( "{query}" ); $urls = [ 'linux' => 'http://en.wikipedia.org/wiki/Linux', 'ruby' => 'https://www.ruby-lang.org/' ]; if ( array_key_exists( $keyword, $urls ) ) { exec( "open {$urls[$keyword]}" ); } The only addition is a check to make sure that the array key exists before opening it. LiquidIce 1 Link to comment
LiquidIce Posted December 16, 2014 Author Share Posted December 16, 2014 Thanks for that guys! A quick question though, is it possible to save the array into a file and read it? I have PHP knowledge but I don't know what limitations alfred may have when it comes to syntax, php functions, etc... Kind regards Link to comment
rice.shawn Posted December 16, 2014 Share Posted December 16, 2014 If you want to save it, you should probably just save it as JSON. So, the JSON for the above array would look like: { "linux": "http://en.wikipedia.org/wiki/Linux", "ruby": "https://www.ruby-lang.org/" } If you want to save an array as a JSON file with PHP code: $array = [ 'linux' => 'http://en.wikipedia.org/wiki/Linux', 'ruby' => 'https://www.ruby-lang.org/' ]; file_put_contents( '/path/to/my/file.json', json_encode( $array, JSON_PRETTY_PRINT ) ); If you want to read a JSON file into an array: $array = json_decode( file_get_contents( '/path/to/my/file.json' ), true ); They're all standard functions, so it's not stretching too far. The `JSON_PRETTY_PRINT` flag in the `json_encode` function just makes it easier to read the JSON when it's in a file. If you're going to save anything dynamically, then make sure you save it to your workflow's data directory. Luckily, you can access this without remembering the full path with: $_SERVER['alfred_workflow_data'] which will work for any workflow run from Alfred. Just make sure that you do: if ( ! file_exists( $_SERVER['alfred_workflow_data'] ) ) { mkdir( $_SERVER['alfred_workflow_data'], 0775, true ); } before you try to save anything there. Link to comment
rice.shawn Posted December 16, 2014 Share Posted December 16, 2014 As a quick follow-up: Alfred has full range of the php-cli binary, so anything that you can do with php from the command line, you can do with Alfred. Remember that command line scripts receive their arguments from the `$argv` array. There are a few quirks with OS X's PHP binaries (php 5.4 for Mavericks, php 5.5 for Yosemite) and its layout. You probably don't need any of the `pnctl` functions, but they aren't there (if you need to fork processes, then you can do some hacky shite with Bash from inside PHP). You are likely to find out that there is no real default php.ini file, and, then, it usually doesn't have things set, such as the default timezone. If you want to use any date functions, then PHP is going to throw a hissy fit. To fix that you need to either (1) create a php.ini file at `/etc/php.ini` (or `/private/etc/php.ini` — more accurately ), or (2) throw this code into the workflow whenever you want to use anything date related that isn't `gm_date()`: // Set date/time to avoid warnings/errors. if ( ! ini_get( 'date.timezone' ) ) { $timezone = exec( 'tz=`ls -l /etc/localtime` && echo ${tz#*/zoneinfo/}' ); ini_set( 'date.timezone', $timezone ); } That code will read the computer's current timezone and set it for PHP at runtime, but it won't override any settings. Link to comment
LiquidIce Posted December 16, 2014 Author Share Posted December 16, 2014 Thanks for the help Shawn, I figured out another way to do this but as it turns out, if the name has a space, alfred doesn't know how to get the value of that text... Regards, Link to comment
rice.shawn Posted December 16, 2014 Share Posted December 16, 2014 Thanks for the help Shawn, I figured out another way to do this but as it turns out, if the name has a space, alfred doesn't know how to get the value of that text... Regards, It does know how to do that; but you just need to quote the query correctly. So, if you're launching the php script via a bash script action: php myscript.php {query} and the "{query}" is two words, then what it really sends is: php myscript.php one two Then, in `$argv`, you'll have : 0 => myscript.php 1 => one 2 => two If you instead launch it with: php myscript.php "{query}" then `$argv` will be: 0 => myscript.php 1 => one two If there is a space in the filename, then you'll have to do similar: php "my path/with spaces/myscript.php" "{query}" Make sure that you put the quotes around "{query}" Link to comment
LiquidIce Posted December 16, 2014 Author Share Posted December 16, 2014 Hello Shawn, I have passed the values in "", here is what the code is like: $keyword = strtolower ("{query}"); $urls = [ 'developers' => '1', 'analytics viewer' => '2' ]; if ( array_key_exists( $keyword, $urls ) ) { exec( "open urlhere?id={$urls[$keyword]}" ); } There is more code below it and I have changed the URL because its private, but that part works. Things with 1 entire word works fine, but if I put analytics viewer in alfred it does nothing. Regards, 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