Tyler Eich Posted February 14, 2013 Posted February 14, 2013 I have been reading the documentation for David Ferguson's workflow class (awesome work, BTW). I can't figure out how to write to the cache. I have tried $xml_string = 'Lorem ipsum dolor sit amet'; $path = $w->cache().'/data.xml'; $w->write($xml_string, $path); which wrote nothing. Then I tried $xml_string = 'Lorem ipsum dolor sit amet'; $w->write($xml_string, '/data.xml'); which wrote to ~/Library/Application Support/Alfred 2/Workflow Data Then I tried $xml_string = 'Lorem ipsum dolor sit amet'; $w->write($xml_string,'data.xml'); which wrote to ~/Library/Application Support/Alfred 2/Workflow Data/my.bundle.id Any suggestions?
jdfwarrior Posted February 15, 2013 Posted February 15, 2013 Appears to be an error on my part in the logic of which location to select. I'll work on fixing it and push it back to github.. Sorry
Tyler Eich Posted February 15, 2013 Author Posted February 15, 2013 No worries Its benefits and ease of use FAR outweigh any issue(s?) it may have. Thanks for your hard work!
jdfwarrior Posted February 15, 2013 Posted February 15, 2013 Ok I think I figured it out. I got mixed up on my logic for selecting paths. What it does is.. when you pass it a path to write to, it checks to see if that path exists, if it does, that means it could be in the current workflow folder, OR its a full path. I needed to add an extra check to see if it was full path. If it isn't then it prepends the path to the workflow. So, just for clarification.. now.. when you try to read a file for instance, it will 1. Check to see if the file given exists 1a. If so, check to see if its the workflow (current) path, or a fully specified path. If its in current path (just the filename was passed), then it prepends the full path to that file. 2. If not.. checks to see if the file exists in the data folder, if so, prepends data path to the front of it. 3. If not.. checks to see if the file exists in the cache folder, if so, prepends the cache path to the front. 4. If not.. fails. Same applies for the write/set. 1. Checks to see if it exists in current. 1a. If it is in current, prepends full path to that, otherwise, assumes its full path already and leaves it alone 2. If not.. checks data path 3. If not.. checks cache path 4. If not.. falls back and writes to data path. Make sense? So if you don't use a path.. for writes.. it checks locally, checks to see if it exists in data, then cache, and if not, just writes to data. For reads.. it checks local or assumes full, if not it checks in data, then cache, if the file wasn't found, it fails (returns false). Download the update on Github
jdfwarrior Posted February 15, 2013 Posted February 15, 2013 No worries Its benefits and ease of use FAR outweigh any issue(s?) it may have. Thanks for your hard work! And yeah... the library makes it SOOOO quick and easy to do stuff and create a workflow. Did all the documentation make sense to you?
Tyler Eich Posted February 15, 2013 Author Posted February 15, 2013 And yeah... the library makes it SOOOO quick and easy to do stuff and create a workflow. Did all the documentation make sense to you? Yes, once I found it. I had a hard time finding your new blog (dferg.us), but the documentation was easy to understand.
jdfwarrior Posted February 15, 2013 Posted February 15, 2013 Unfortunately I thought I had fixed this but I hadn't, it should be fixed now I believe
Tyler Eich Posted February 15, 2013 Author Posted February 15, 2013 Thanks for the update . Write operations are performing as expected. Now I have another problem: I can't get it to automatically detect my bundle id. My 'workflows.php' is in the '~/Application Support/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.really-long-uid' folder. I checked the info.plist and it does contain the bundle id. In the meantime, I'm providing the bundle id manually: $w = new Workflows('my.bundle.id'); Thanks!
Tyler Eich Posted February 16, 2013 Author Posted February 16, 2013 I also noticed that the cache won't be written to unless the file it is attempting to write to exists already. If it doesn't exist, it will prepend the path to the data folder, which can be undesirable. My original code: $cache = $w->cache().'/cache.xml'; $w->write($xml_string, $cache);I've added some defensive code (I thinks that's what it's called) to create a 'cache.xml' if it does not exist already:$cache = $w->cache().'/cache.xml'; if(!$w->read($cache)){ file_put_contents($cache,''); } $w->write($xml_string, $cache);That fixed it for me .Perhaps a similar function could be included in the code? I was thinking of an optional logic switch for write() that disables its path logic: $w->write($content, $path, false); //Do not use logic; just write the file $w->write($content, $path, true); //Use logic to determine appropriate file path $w->write($content, $path); //Use logic to determine appropriate file pathOr maybe letting cache() and data() optionally accept arguments that allow files to be written to the cache, instead of just returning the path to it?$w->cache($content,$file); //Writes file to ~/Library/Caches/ ... /bundle.id/$file $w->cache(); //Returns path to cache (like it does now)Please forgive my PHP; I learned by reading Alfred extension source code
jdfwarrior Posted February 16, 2013 Posted February 16, 2013 Ah yeah, I missed that.. If the file doesn't exist, it falls back to writing to the data folder instead. Yeah I'll have to find a way to fix that
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