Jump to content

rice.shawn

Member
  • Posts

    973
  • Joined

  • Last visited

  • Days Won

    57

Posts posted by rice.shawn

  1. The other option is to have  program that will allow me to run the script as another user that has Admin and Sudo Privileges so I only have to type the admin credentials once and not 3 times for each entry in my script.  

     

    You can give your regular account sudo access either with or without a password to specific commands. 

  2. Can the same be done with cURL? wget isn't installed by default.

     

    I really wish that Apple would have just installed wget by default.

     

    Anyway, here is the adapted gist to use cURL rather than wget.

    #!/bin/sh
    #login using sessions/token
    # ** Replace YOUR_USERNAME and YOUR_PASSWORD below with your Tado credentials **
    curl --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" --data="j_username=YOUR_USERNAME&j_password=YOUR_PASSWORD" https://my.tado.com/j_spring_security_check --cookie-jar /tmp/tadocookie.txt -o /tmp/tadologin.out > /dev/null 2>&1
    #Get the thermostat settings xml (inc temperatures)
    curl --header="Content-Type: application/x-www-form-urlencoded; charset=UTF-8" --header="X-Requested-With: XMLHttpRequest" --cookie /tmp/tadocookie.txt https://my.tado.com/mobile/1.4/getThermostatSettings -o /tmp/tadotemps.xml > /dev/null 2>&1
    #Get out the temps from the xml
    THERMOSTAT_TEMP=$(cat /tmp/tadotemps.xml | jq -r '.temperatures.externalTempSensor')
    CONTROL_BOX_TEMP=$(cat /tmp/tadotemps.xml | jq -r '.temperatures.box')
    #Print out temps to 1 decimal place
    printf "Kitchen: %0.1f°C\n" $CONTROL_BOX_TEMP
    printf "Living Room: %0.1f°C\n" $THERMOSTAT_TEMP
    

    It **should** work, but it's not tested. I just redid the options to change them from wget to curl.

     

    But it also looks like you need jq installed. jq is a great utility to let you work with structured data (xml, json) from a shell. If you want to make this workflow available for distribution, then you should probably use some other sort of scripting language that supports structured data with minimal headache but use the same approach via storing cookies and setting headers.

  3. Okay, so I just uploaded a new version (1.3.2) that implements the two new options:

     

    (1) Press cmd + enter on a translation to open the Google Translate page in the browser.

    (2) Press opt + enter on a translation to listen to the audio of the translation.

     

    Note: audio translations are supported to about 100 characters according to what I found on the net, so it probably won't work well if you try to listen to more than that. Also, you can't change voices for the translations (they're just audio files that are downloaded from Google).

  4.  

    Thanks for updating!!!

     

    For everyone else who has problems with the workflow after updating to v1.3:

     

    1. Open filter.php
    2. Delete the following code (line 91) 
      $trans = $alphred->get( $query, [ 'useragent' => 'alfredworkflow' ], 2419200, true );
      

      and replace it with the following code

    3. $ch = curl_init( $query );
      curl_setopt( $ch, CURLOPT_USERAGENT, 'alfredworkflow' );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
      $trans = curl_exec( $ch );
      curl_close( $ch );
      

      and you should be done.

     

     

    Did you have problems with it? What were the errors? The Alphred model is nice because it will also cache the translations.

  5. I love your workflow, yet I'm stuck with one question. Right now when you press enter, it copies the text to the clipboard. In many cases, I'm curious about the alternative translations. Would it be possible to open google translate in the browser with the picked languages so I can easily view the translations?

     

    Hm. The workflow needs to be redesigned (not necessarily completely rewritten) to accommodate this. I'll think of a way to make it happen and get back to you.

  6. Yeah. I'd simply like to call an API, get the data, and show them in the results.

    I used the Node.js workflow template previously, but I was stoked to see JS support in Alfred – but not very useful for me without the ability to call APIs.

     

    You can still call APIs and get the data and all that jazz, but you just have to do it synchronously rather than asynchronously. So, make sure that you get the data before you render any XML for a script filter.

  7. For server, I'm not thinking a long running process, but something that acts more like a web-server but without the actual server. So that might not actually affect the way that Alfred is run. By tokens, I mean more like chunked text in that the text isn't really a set of characters but a unit as a whole (this is a bad analogy and explanation).

     

    I'm pretty sure that you can fake decorators in PHP. Ruby has decorators.

     

    The URL routing mechanism that I'm now thinking of this in is Rails' router.

     

    The last time I tried to write this class in PHP, I basically tried to make the entire multi-level menu be represented as a large associative array and that each endpoint ended up being a function that would be called. But that wasn't quite the right approach.

     

     

    @shawn: You've written a lot of Ruby code recently. Are you not seriously thinking about ditching PHP?

     

    I do heart Ruby, but PHP still has a lovely place in my heart. Also, since a lot of people seem to write workflows in PHP, having a solid PHP library for them to use and abuse seems productive. I have it somewhere on a todo list to, basically, port Alphred into Ruby as well as Bash.

  8. It sounds almost like we're talking about creating a full local server for each workflow (which is a cool idea) where Alfred's search query is basically an address bar and one that has an internal history.

     

    Currently, we seem to use "autocomplete" and string matching to define the state, but this would add in something closer to tokens.

     

    I've toyed around with creating a class for Alphred that would dynamically generate multi-level menus, but the problem I ran into each time via conceptualization was the routing in that it always created more complexity than it was worth. Revisiting it with this thinking might be productive.

  9. Do you know if there'll be support for AJAX (XHR) requests at some point? 

     

    I'd say that it never will. Andrew tries to keep the size of Alfred small so that it keeps performing quickly. In order to do something like AJAX, he'd have to compile in all or part of a JS rendering engine (95% of which wouldn't apply to Alfred).

     

    What would you use the Ajax for with Alfred, anyway? Updating script filter results?

  10. The workflow breaks also when installing another global php version (e.g. with homebrew). I had to fix the workflow with your proposed one-liner-fix.

    PHP 5.6.13 (cli)
    

    I have another question regarding your nice workflow: Is it possible to trigger the workflow with a hotkey? I've tried some combinations but I didn't get it done. My desired workflow would be: select some text -> press a shortcut -> fires up trans workflow with the selected text

     

    The hotkey is a neat idea. I just added it to the workflow and push an update (1.3) to Packal.

     

    Also, I rewrote getting / fetching mechanisms that should work with all versions. The configs did change a little bit, so you might have to reset them.

  11.  

    I tried to modify this workflow to support keychain but to no avail since it seem support has been deprecated for terminal keychain access in Yosemite.  Instead I made this change to read from a field that is only accessible by me so that the password is not directly stored in the Alfred Workflow which is in my Dropbox.  It is stored in a local home directory which is hidden and only I have access to the file and directory.  A little more secure I guess

     

    on alfred_script(q)

    tell application "System Events"
    do shell script "caffeinate -u -t 1"
    key code 123
    delay 0.5
    -- Set the string in the file macpassfile
    set myfile to ("Macintosh HD:Users:mdivince:mydirectory:macpassfile")
    set passwd to (read file myfile)
    keystroke passwd
    delay 1
    keystroke return
    end tell
    end alfred_script

     

     

    You can still access the keychain in Yosemite (and El Capitan), and this workflow seems to be doing just that in the second line of the AppleScript as it uses the `security` command from the terminal, which is the command to interact with the keychain.

  12. Thank you Shawn, it works great Now I got to figure out how you did it and work on my PHP coding... 

     

    After you install the workflow, just open it up and look at the `script-filter.php`. You'll see that I've included a library called "Alphred" that I wrote that makes writing workflows in PHP much, much easier, and so that is what controls the output for the script filter. Here, the library just easily creates the XML that Alfred reads.

     

    The text to date functionality (for things like "now" or "tomorrow" or "+1 year") are built into PHP's date functionality. More specifically, they're built into the function `strtotime`, and so that does a lot of the heavy lifting.

     

    The actual conversion from Arabic to Roman numerals is done by breaking the date string (31/3/1999) into an array and then using an `array_filter` (which just applies the same function to each part). So, all we needed was a single function to convert an Arabic number to a Roman numeral.

     

    The function that does that is here:

    function toRoman( $integer ) {
        $output = '';
        $conversion = [
        	'M' => 1000,
        	'CM'=> 900,
        	'D'=>500,
        	'CD'=>400,
        	'C'=>100,
        	'XC'=>90,
        	'L'=>50,
        	'XL'=>40,
        	'X'=>10,
        	'IX'=>9,
        	'V'=>5,
        	'IV'=>4,
        	'I'=>1
        ];
    
    	while ( $integer > 0 ) :
    		foreach ( $conversion as $roman => $arabic ) :
    			if ( $integer >= $arabic ) {
    				$integer -= $arabic;
    				$output .= $roman;
    				break;
    			}
    		endforeach;
    	endwhile;
    
    	return $output;
    }
    

    So, basically it takes an associative array that defines the translations between Arabic and Roman and then just uses two loops to modify the input value while constructing an output string. Each time it adds onto the string, it removes the equivalent value from the input integer and just keeps going until the integer reaches 0 and then returns the string.

     

    Hopefully the explanation helps.

     

    I did decide to make it a script filter rather than using an Applescript input box because it makes it easier to manipulate the values quickly and see the results.

  13. Yes it is. You can just use osascript from the command line:

    osascript -e "activate application \"Pages\"" -e "delay 1" -e "tell application \"System Events\" to keystroke \"n\" using command down" 

    That script will open Pages, wait one second, and then send the keystrokes. You can change out "Pages" with the other app names, but make sure that you get them perfect because AppleScript is sensitive. You can remove the delay, but then keystrokes might be sent before the application is open.

  14. I re-coded it in PHP and dropped it in a simple workflow that you can download here: https://www.dropbox.com/s/hihr5zl054wdq5u/Roman%20Date.alfredworkflow?dl=0.

     

    It works as a script filter and uses PHP's date parser, so you can do things like "rd now" and "rd tomorrow" and "rd next friday" and "rd -1000 years" as well as "Oct 29 1993" or "10/29/1993".

     

    Currently, if you press enter, it just displays it as large text.

  15. Hi

    I just downloaded and installed the latest Caffeinate workflow v 3.3. 

    However, it does not behave as documented. I face the two following problems:

    1. If I type "caff d" it displays "Enable Caffeinate forever" instead of "Disable" (thus the same as when I type "caff e").

    2. I couldn't figure out how the options work because regardless of which option I set, I get exactly the same prompt when enabling Caffeinate after having set the options. How do  know the options are being applied?

     

    Walter

     

    Sorry for the delay, Walter.

     

    If Caffeinate is not running, then it will only show you ways to enable caffeinate (you can't disable it if it isn't running). If it is running, then you can "re-enable it" by entering a different argument, or you can disable it. Try typing `caff e` and pressing enter and then typing `caff d`, and you should see the difference.

     

    The prompts will always be the same regardless of the options you set. If you want to verify exactly what arguments are being sent then change the options and enable caffeinate via the workflow, then open a terminal and type

    ps aux|grep caffeinate|grep -v grep
    

    and you should see the command line argument sent in order to enable caffeinate. The different options are just more about what caffeinate doesn't allow to go to sleep. My assumption is that users would want to set this once and never worry about it again because it's more of a general preference in how you want to keep your computer awake. Adding those options to the prompt would have added a lot more complexity to the code as well as the user interaction.

  16. I love this workflow and use it all the time. Thanks for creating it, by the way! The only minor issue I've noticed is that the time remaining does not update. If I set it for 10 minutes, wait 9 minutes, and type caff, it will show caffeinate will be active for another 10 minutes. It's no big deal but I'm curious if it's a widespread issue or an issue on my end.

     

    Nope. That turns out not to be just on your end but on everyone's end. I had overlooked that part. Anyway, I just put in a fix. v3.03. Is available to fix that.

×
×
  • Create New...