Jump to content

Accessing Data Directory


Recommended Posts

Could anyone tell me why this doesn't work?

 




<?php
require_once('workflows.php');
$w = new Workflows;

# Navigate to data directory
$data_dir = $w->home() . '/Library/Application\ Support/Alfred\ 2/Workflow\ Data';

# lists all the files inside the data directory
$data_dir = glob("$data_dir" . '/*');

# grab bundle id from project folder
$plist_dir = $query . "/info.plist";

$info_plist = simplexml_load_file( $plist_dir );
$bundle_id = $info_plist->dict->string;

// # Grab the data sub-directory's name to compare to the bundle id
foreach ($data_dir as $data_subdir) {

// grab the end of the file path (the name of bundle_id)
$data_subdir = basename($data_subdir).PHP_EOL

    // if name of data directory matches the bundle_id
if ($data_subdir === $bundle_id) {

        // then open the data directory in Finder
        $cmd = 'open -a Finder "$data_dir"';
        exec("$cmd");
    }
}


Edited by AleXander
Link to comment

You have the single/double quotes backward on this line:

        $cmd = 'open -a Finder "$data_dir"';
You probably need a semi-colon on this line:
$data_subdir = basename( $data_subdir ).PHP_EOL

Depending on what is being returned, you might need equivalence (==) rather than equality (===) on this line:

if ( $data_subdir === $bundle_id )
You need to use the "{query}" variable from Alfred not the "$query" variable on this line:
$plist_dir = $query . "/info.plist";
The following line isn't the data directory.
$data_dir = $w->home() . '/Library/Application\ Support/Alfred\ 2/Workflow\ Data';
If you want to grab all the data directories for Alfred Workflows, then you could just make it easier:
$data_dirs = scandir($w->data() . "../");
It also might be easier/more reliable to get the bundle id from each plist by doing:
$cmd =" /usr/libexec/PlistBuddy  -c 'Print :bundleid' '$data_dir/info.plist'";
$bundle = exec($cmd);
 
I'm assuming that you want to grab all of the files out a some matching workflow's data directory. If you just want to grab all the files out of the current workflow's workflow directory, then you just need to do:
$files = scandir(".");
Link to comment
  • 2 weeks later...

Thanks Shawn!

 

 

You have the single/double quotes backward on this line:

        $cmd = 'open -a Finder "$data_dir"';
You probably need a semi-colon on this line:
$data_subdir = basename( $data_subdir ).PHP_EOL

Depending on what is being returned, you might need equivalence (==) rather than equality (===) on this line:

if ( $data_subdir === $bundle_id )
You need to use the "{query}" variable from Alfred not the "$query" variable on this line:
$plist_dir = $query . "/info.plist";
The following line isn't the data directory.
$data_dir = $w->home() . '/Library/Application\ Support/Alfred\ 2/Workflow\ Data';
If you want to grab all the data directories for Alfred Workflows, then you could just make it easier:
$data_dirs = scandir($w->data() . "../");
It also might be easier/more reliable to get the bundle id from each plist by doing:
$cmd =" /usr/libexec/PlistBuddy  -c 'Print :bundleid' '$data_dir/info.plist'";
$bundle = exec($cmd);
 
I'm assuming that you want to grab all of the files out a some matching workflow's data directory. If you just want to grab all the files out of the current workflow's workflow directory, then you just need to do:
$files = scandir(".");

 

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