Jump to content

workflows.php—How to write to workflow cache

Recommended Posts

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';

which wrote to ~/Library/Application Support/Alfred 2/Workflow Data/my.bundle.id


:wacko: Any suggestions?

Link to comment

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

Link to comment

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.  :huh:


In the meantime, I'm providing the bundle id manually:


$w = new Workflows('my.bundle.id');



Link to comment

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';
$w->write($xml_string, $cache);
That fixed it for me :lol:.

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 path
Or 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 :P
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...